What are the 7 arithmetic operators in Python?

Crystal 25 Published: 09/17/2024

What are the 7 arithmetic operators in Python?

I'd be happy to explain the 7 arithmetic operators in Python!

Python provides a set of basic arithmetic operators that allow you to perform calculations with numbers. These operators can be categorized into two main groups: binary operators, which take two operands (left and right), and unary operators, which operate on a single value.

Addition (+): The addition operator is used to add the values of two numbers together. This operator returns the sum of the left-hand operand and the right-hand operand.

Example: a = 5; b = 3; c = a + b; print(c) will output 8.

Subtraction (-): The subtraction operator is used to subtract the value of one number from another. This operator returns the difference between the left-hand operand and the right-hand operand.

Example: a = 5; b = 3; c = a - b; print(c) will output 2.

Multiplication (*): The multiplication operator is used to multiply the values of two numbers together. This operator returns the product of the left-hand operand and the right-hand operand.

Example: a = 5; b = 3; c = a * b; print(c) will output 15.

Division (/): The division operator is used to divide one number by another. If you're using integers, any remainder will be discarded (integer division). This operator returns the quotient of the left-hand operand divided by the right-hand operand.

Example: a = 5; b = 2; c = a / b; print(c) will output 2.5.

Note that in Python 3.x and later, the / operator performs "true division", which means it always returns a float, even if the operands are integers. To get integer division, you can use the // operator (see below).

Floor Division (//): The floor division operator is used to divide one number by another and return the largest possible integer quotient.

Example: a = 5; b = 2; c = a // b; print(c) will output 2.

Modulus (%): The modulus operator is used to find the remainder of an integer division operation.

Example: a = 17; b = 5; c = a % b; print(c) will output 2, because 17 divided by 5 leaves a remainder of 2.

Exponentiation ()**: The exponentiation operator is used to raise one number to the power of another.

Example: a = 2; b = 3; c = a ** b; print(c) will output 8, because 2 raised to the power of 3 equals 8.

These arithmetic operators are essential for performing calculations in Python, and you'll likely use them frequently in your programming endeavors!

Bitwise operators in Python Exercises

I'll make sure to respond in English and provide exercises on bitwise operators in Python.

What are Bitwise Operators?

Bitwise operators are used to manipulate the binary representation of a number, which is composed of bits (0s and 1s). These operators are used to perform bit-level operations, such as shifting, ANDing, ORing, XORing, and NOTing. In Python, bitwise operators can be applied to integers.

Bitwise Operators in Python

Here are the bitwise operators available in Python:

& (AND): Returns an integer with bits set where both operands have 1s.

Example: 5 & 3 returns 1, because only the least significant bit is common to both numbers (101 and 011).

| (OR): Returns an integer with bits set where either operand has a 1.

Example: 5 | 3 returns 7 (101 or 011).

^ (XOR): Returns an integer with bits set where the operands have different values.

Example: 5 ^ 3 returns 6, because the least significant bit is set in one number and not in the other (101 and 011).

~ (NOT): Inverts the bits of the operand.

Example: ~5 returns -6, because the binary representation of 5 is 101, which becomes 010 when inverted.

<< (Left Shift): Shifts the bits of the number to the left, filling with zeros on the right.

Example: 5 << 1 returns 10, because the least significant bit is shifted one position to the left (101 becomes 0110).

>> (Right Shift): Shifts the bits of the number to the right, filling with zeros on the left.

Example: 5 >> 1 returns 2, because the most significant bit is shifted one position to the right (101 becomes 010).

Exercises

Now that you have a basic understanding of bitwise operators in Python, let's try some exercises:

Write a program that uses bitwise AND to determine whether two numbers are both even.
def are_even(a, b):

return (a & b) == 0

print(are_even(4, 6)) # Should print True

print(are_even(3, 5)) # Should print False

Write a program that uses bitwise OR to determine whether two numbers have at least one common bit set.
def have_common_bit(a, b):

return (a | b) != 0

print(have_common_bit(4, 6)) # Should print True

print(have_common_bit(3, 5)) # Should print False

Write a program that uses bitwise XOR to determine whether two numbers have exactly one different bit set.
def have_one_difference(a, b):

return (a ^ b) == 1

print(have_one_difference(4, 6)) # Should print True

print(have_one_difference(3, 5)) # Should print False

Write a program that uses bitwise NOT to invert the bits of a number.
def invert_bits(n):

return ~n

print(invert_bits(5)) # Should print -6

Write a program that uses bitwise left and right shifts to shift the bits of a number and determine whether it is even or odd.
def is_even_odd(n):

if (n << 1) == n: # Shifted number equals original, so even

return "even"

elif (n >> 1) == n // 2: # Shifted number equals half the original, so odd

return "odd"

else:

return "neither"

print(is_even_odd(4)) # Should print "even"

print(is_even_odd(3)) # Should print "odd"

I hope these exercises help you better understand bitwise operators in Python. Happy coding!