Operator
Operator
Operator
Python Programming
• 1. Arithmetic Operators
• 2. Comparison
• 3. Logical
• 4. Bitwise
• 5. Assignment
• 6. Identity Operators and Membership
Operators
• Division Operators
• Division Operators allow you to divide two numbers and
return a quotient, i.e., the first number or number at the left
is divided by the second number or number at the right and
returns the quotient.
• There are two types of division operators:
• Float division
• Floor division
• # Addition of numbers
• add = a + b
• # Subtraction of numbers
• sub = a - b
• # Multiplication of number
• mul = a * b
• # Power
• p = a ** b
• In python, the comparison operators have lower precedence than the arithmetic
operators. All the operators within comparison operators have same precedence
order.
• Example of Comparison Operators in Python
• # a > b is False
• print(a > b)
• # a < b is True
• print(a < b)
• # a != b is True
• print(a != b)
• # a >= b is False
• print(a >= b)
• # a <= b is True
• print(a <= b)
• # Print a or b is True
• print(a or b)
| Bitwise OR x|y
~ Bitwise NOT ~x
= Assign the value of the right side of the expression to the left side operand x=y+z
Add AND: Add right-side operand with left-side operand and then assign to left
+= a+=b a=a+b
operand
Subtract AND: Subtract right operand from left operand and then assign to left
-= a-=b a=a-b
operand
Multiply AND: Multiply right operand with left operand and then assign to left
*= a*=b a=a*b
operand
/= Divide AND: Divide left operand with right operand and then assign to left operand a/=b a=a/b
Modulus AND: Takes modulus using left and right operands and assign the result
%= a%=b a=a%b
to left operand
Divide(floor) AND: Divide left operand with right operand and then assign the
//= a//=b a=a//b
value(floor) to left operand
Exponent AND: Calculate exponent(raise power) value using operands and assign
**= a**=b a=a**b
value to left operand
&= Performs Bitwise AND on operands and assign value to left operand a&=b a=a&b
|= Performs Bitwise OR on operands and assign value to left operand a|=b a=a|b
^= Performs Bitwise xOR on operands and assign value to left operand a^=b a=a^b
>>= Performs Bitwise right shift on operands and assign value to left operand a>>=b a=a>>b
<<= Performs Bitwise left shift on operands and assign value to left operand a <<= b a= a << b
• # Assign value
• b=a
• print(b)
• if (x not in list):
• print("x is NOT present in given list")
• else:
• print("x is present in given list")
• if (y in list):
• print("y is present in given list")
• else:
• print("y is NOT present in given list")
• # Left-right associativity
• # 5 - 2 + 3 is calculated as
• # (5 - 2) + 3 and not
• # as 5 - (2 + 3)
• print(5 - 2 + 3)
• # left-right associativity
• print(5 - (2 + 3))
• # right-left associativity
• # 2 ** 3 ** 2 is calculated as
• # 2 ** (3 ** 2) and not
• # as (2 ** 3) ** 2
• print(2 ** 3 ** 2)