Day05-Python Operators # 1
Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise
PART # 1
Python Operators:
Operators are used to perform operations on variables and values.
Example: In the example below, we use the + operator to add together two values. Here:
'+' is the operator.
5 and 2 are operant.
5+2 are operations.
print(10 + 5)
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Arithmetic Operators:
The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.
Operator | Operator Name | Example |
+ | Addition | 15+7 |
- | Subtraction | 15-7 |
* | Multiplication | 15*7 |
/ | Division | 15/7 |
% | Modulus | 15%7 |
** | Exponential | 15**7 |
// | Floor Division | 15//7 |
Example:
print(2 + 7, "Addition")
print(7 - 2, "Subtraction")
print(7 * 2, "Multiplication")
print(7 / 2, "Division")
print(11 % 3, "Modulus") #modulus is the remainder in division
print(2 ** 2, "Exponent")
print(7 // 2, "Floor division") #it only tells value before point.
Output:
9 Addition
5 Subtraction
14 Multiplication
3.5 Division
2 Modulus
4 Exponent
3 Floor division
Assignment Operators:
Assignment operators are used to assign values to variables.
Example:
- Equal To (=):
x = 5 #equal to
print("Result of = opeartor is:",x) #output=5
- Plus Equal To (+=):
x = 5
x += 3 #5+3= 8
print("Result of += opeartor is:", x) #ouput=8
- Minus Equal To (-=):
x = 5
x -= 3 #5-3=2
print("Result of -= opeartor is:", x) #ouput=2
- Multiply Equal To (*=):
x = 5
x *= 3 #5*3=15
print("Result of *= opeartor is:", x) #output=15
- Divide Equal To (/=):
x = 5
x /= 3 #5/3=1.6666666666666667
print("Result of /= opeartor is:",x) #output=1.6666666666666667
- Modulus Equal To (%=):
x = 5
x%=3 #5/3 = 1.66 => remainder=2
print("Result of %= opeartor is:", x) #output=2
- Floor Division Equal To ( //= ):
x = 5
x//=3 #5/3=1.666 => approximately=1
print("Result of //= opeartor is:", x) #output=1
- Exponential Equal To (**=):
x = 5
x **= 3 #5*5*5 =125
print("Result of **= opeartor is:", x) #output=125
- End Equal To (&=):
x = 5
x &= 3 # & perform bitwise operation
print("Result of &= opeartor is:", x) #output=1
- Vertical bar Equal To ( |= ):
x = 5 #value of 5 in binary = 101
x |= 3 #value of 5 in binary = 011
#OR operation b/w 101 and 011 = 111 => that is equal to 7 in binary
print("Result of |= opeartor is:", x) #output=7
In this case, x
is initially assigned the value of 5. When x ^= 3
is executed, the binary representation of x
is 101
, and the binary representation of 3 is 011. The XOR operation between these two binary numbers results in 110, which is equal to decimal value 6. Therefore, after executing the expression, the value of x
becomes 6.
- Cap Equal To (^=):
x = 5 #value of 5 in binary = 101
x ^= 3 #value of 5 in binary = 011
#XOR operation b/w 101 and 011 = 110 => that is equal to 6 in binary
print("Result of ^= opeartor is:", x) #ouput=6
In this case, x
is initially assigned the value of 5. When x |= 3
is executed, the binary representation of x
is 101
, and the binary representation of 3 is 011. The OR operation between these two binary numbers results in 111, which is equal to decimal value 7. Therefore, after executing the expression, the value of x
becomes 7.
- Greater than Equal To (>>=):
x = 5 #value of 5 in binary = 101
x >>= 3 #right shift operation by 3 places =000 => equal to 0 in binary
print("Result of >>= opeartor is:", x) #output = 0
In this case, x
is initially assigned the value of 5. When x >>= 3
is executed, the binary representation of x
is 101
, and the right shift operation by 3 places results in 000, which is equal to decimal value 0. Therefore, after executing the expression, the value of x
becomes 0.
- Less than Equal To (<<=):
x = 5 #value of 5 in binary = 101
x <<= 3 #left shift operation by 3 places =101000 => equal to 40 in binary
print("Result of <<= opeartor is:", x)
In this case, x
is initially assigned the value of 5. When x <<= 3
is executed, the binary representation of x
is 101
, and the left shift operation by 3 places results in 101000
, which is equal to decimal value 40. Therefore, after executing the expression, the value of x
becomes 40.
Note: Above names are given by me, for better understanding.. these are not official names
Comparison Operators:
Comparison operators are used to compare two values. It tells whether the statement is True or False.
Operator | Name | 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 |
- Equal (=):
a = 7
b = 9
print(a == b) # returns False => because 7 is not equal to 9
x = 7
y = 7
print(x == y) # returns True => because 7 is equal to 9
- Not equal (!=):
a = 5
b = 3
print(a != b) # returns True => because 5 is not equal to 3
x = 5
y = 5
print(x != y) # returns False => because 5 is equal to 5
- Greater than (>):
a = 7
b = 9
print(a > b) # returns False => because 7 is not greater than 9
x = 9
y = 7
print(x > y) # returns True => because 9 is greater than 7
- Less than (<):
a = 7
b = 9
print(a < b) # returns True => because 7 is not greater than 9
x = 9
y = 7
print(x < y) # returns False => because 9 is greater than 7
- Greater than or equal to (>=):
a = 10
b = 9
print(a >= b) # returns True => because 10 is greater than 9
x = 10
y = 10
print(x >= y) # returns True => because 10 is equal to 10
- Less than or equal to (<=):
a = 9
b = 9
print(a <= b) # returns True => because 9 is equal to 9
x = 9
y = 10
print(x <= y) # returns True => because 9 is less than 10
To Be Continued..
<< Previously -------------------------------------------------- Next Lesson >>