- Operators in Python are symbols used to perform operations on variables and values. Python supports a wide range of operators for different purposes, including arithmetic operations, comparison operations, logical operations, and more.
- For example, In the expression 3 + 2 = 5. Here, 4 and 5 are called operands and + is called operator.
- Arithmetic operators
- Comparison operators
- Assignment operators
- Logical operators
- Membership operators
- Identity operators
- Bitwise operators
Arithmetic Operators¶
Name | Operator | Example |
---|---|---|
Addition | + | x + y |
Subtraction | - | x - y |
Multiplication | * | x * y |
Division | / | x / y |
Modulus | % | x % y |
Exponentiation | ** | x ** y |
Floor division | // | x // y |
In [2]:
x = 5
y = 10
print("Addition :", x + y)
print("Subtraction :", x - y)
print("Multiplication :", x * y)
print("Division :", x / y)
print("Modulus :", x % y)
print("Exponentiation :", x**y)
print("Floor_division :", x//y)
Addition : 15 Subtraction : -5 Multiplication : 50 Division : 0.5 Modulus : 5 Exponentiation : 9765625 Floor_division : 0
Comparison(Relational) Operators¶
Name | Operator | Example |
---|---|---|
Equal | == | x == y |
Not equal | != | x != y |
Greater than | > | x > y |
Less than | < | x < y |
Greater than or equal to | >= | x >= y |
Less than or equal to | <= | x <= y |
In [6]:
x = 10
y = 5
print("Equal :", x == y)
print("Not equal:", x != y)
print("Greater than :", x > y)
print("Less than :", x < y)
print("Greater than or equal to :", x >= y)
print("Less than or equal to :", x <= y)
Equal : False Not equal: True Greater than : True Less than : False Greater than or equal to : True Less than or equal to : False
Assignment Operators¶
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x ^ 5 |
>>= | x >>= 5 | x = x >> 5 |
<<= | x <<= 5 | x = x << 5 |
In [20]:
x = 5
print("= :" , x)
x = 5
x += 10
print("+= :", x)
x = 5
x -= 1
print("-= :", x)
x = 5
x *= 3
print("*= :", x)
x = 5
x /= 2
print("/= :", x)
x = 5
x %= 2
print("%= :", x)
x = 5
x //= 2
print("//= :",x)
x = 5
x **= 2
print("**= :",x)
x = 5
x &= 2
print("&= :",x)
x = 5
x |= 2
print("|= :",x)
x = 5
x ^= 3
print("^= :",x)
x = 5
x >>= 3
print(">>= :",x)
x = 5
x <<= 3
print("<<= :",x)
= : 5 += : 15 -= : 4 *= : 15 /= : 2.5 %= : 1 //= : 2 **= : 25 &= : 0 |= : 7 ^= : 6 >>= : 0 <<= : 40
Logical Operators¶
- It is used to combine conditional statements
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 |
or | Returns True if one of the statements is true | x < 5 or x < 4 |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
In [21]:
x = 7
print(x > 3 and x < 10)
print(x > 3 or x < 4)
print(not(x > 3 and x < 10))
True True False
Membership Operators¶
- It is used to test if a sequence is presented in an object
Operator | Description | Example |
---|---|---|
in | Returns True if a sequence with the specified value is present in the object | x in y |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
In [22]:
# returns True because a sequence with the value "Bus" is in the list
x = ["Train", "Bus"]
print("Bus" in x)
# returns True because a sequence with the value "Boat" is not in the list
print("Boat" not in x)
True True
Identity Operators¶
- It is used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
In [29]:
a = ["Train", "Bus"]
b = ["Train", "Bus"]
c = a
# returns True because c is the same object as a
print(a is c)
# returns False because a is not the same object as b, even if they have the same content
print(a is b)
# to demonstrate the difference betweeen "is" and "==": this comparison returns True because a is equal to b
print(a == b)
# returns False because c is the same object as a
print(a is not c)
# returns True because a is not the same object as b, even if they have the same content
print(a is not b)
# to demonstrate the difference betweeen "is not" and "!=": this comparison returns False because a is equal to b
print(a != b)
True False True False True False
Bitwise Operators¶
- Bitwise operators in Python allow you to manipulate individual bits of integers at a binary level.
Operator | Name | Description | Example |
---|---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 | x & y |
| | OR | Sets each bit to 1 if one of two bits is 1 | x | y |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y |
~ | NOT | Inverts all the bits | ~x |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | x << 2 |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | x >> 2 |
AND¶
5 = 101
3 = 011
-------
1 = 001
In [3]:
print(5 & 3)
1
OR¶
5 = 101
3 = 011
-------
7 = 111
In [5]:
print(5 | 3)
7
XOR¶
5 = 101
3 = 011
-------
6 = 110
In [7]:
print(5 ^ 3)
6
NOT¶
Inverted 3 becomes -4:
3 = 0000000000000011
-4 = 1111111111111100
In [8]:
print(~3)
-4
Signed right shift¶
- The >> operator moves each bit the specified number of times to the right.
- Empty holes at the left are filled with 0's.
If you move each bit 2 times to the right, 8 becomes 2:
8 = 1000
becomes
2 = 0010
In [13]:
print(8 >> 2)
2
Zero fill left shift¶
- The << operator inserts the specified number of 0's (in this case 2) from the right and let the same amount of leftmost bits fall off.
If you push 00 in from the left:
3 = 0011
becomes
12 = 1100
In [14]:
print(3 << 2)
12
Operator Precedence in Python¶
- Operator precedence in Python refers to the order in which different operators are evaluated in an expression.
- Understanding operator precedence is crucial for writing correct and predictable code. Python follows a specific set of rules to determine the order in which operators are evaluated. Here's a brief overview of operator precedence in Python, from highest to lowest precedence:
Operator | Description |
---|---|
() |
Parentheses |
** |
Exponentiation |
+x -x ~x |
Unary plus, unary minus, and bitwise NOT |
* / // % |
Multiplication, division, floor division, and modulus |
+ - |
Addition and subtraction |
<< >> |
Bitwise left and right shifts |
& |
Bitwise AND |
^ |
Bitwise XOR |
| |
Bitwise OR |
== != > >= < <= is is not in not in |
Comparisons, identity, and membership operators |
not |
Logical NOT |
and |
Logical AND |
or |
Logical OR |
In [20]:
result = 2 + 3 * 4 # Multiplication has higher precedence, so 3 * 4 is evaluated first.
print(result)# result will be 14
result = (2 + 3) * 4 # Parentheses force addition to be evaluated first.
print(result)# result will be 20
result = 2 ** 3 ** 2 # Exponentiation is right-associative, so 3 ** 2 is evaluated first.
print(result)# result will be 512
# If two operators have the same precedence, the expression is evaluated from left to right.
# Addition + and subtraction - has the same precedence, and therefor we evaluate the expression from left to right:
print(5 + 4 - 7 + 3)
14 20 512 5