What are the 7 arithmetic operators in Python?
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
.
Example: a = 5; b = 3; c = a - b; print(c)
will output 2
.
Example: a = 5; b = 3; c = a * b; print(c)
will output 15
.
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).
Example: a = 5; b = 2; c = a // b; print(c)
will output 2
.
Example: a = 17; b = 5; c = a % b; print(c)
will output 2
, because 17 divided by 5 leaves a remainder of 2.
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.Write a program that uses bitwise OR to determine whether two numbers have at least one common bit set.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 XOR to determine whether two numbers have exactly one different 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 NOT to invert the bits of a number.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 left and right shifts to shift the bits of a number and determine whether it is even or odd.def invert_bits(n):
return ~n
print(invert_bits(5)) # Should print -6
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!