Python Operators
Python Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators.
Arithmetic Operators
Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
| Operator | Description | Syntax |
|---|---|---|
| + | Addition: adds two operands | x + y |
| – | Subtraction: subtracts two operands | x – y |
| * | Multiplication: multiplies two operands | x * y |
| / | Division (float): divides the first operand by the second | x / y |
| // | Division (floor): divides the first operand by the second | x // y |
| % | Modulus: returns the remainder when the first operand is divided by the second | x % y |
| ** | Power: Returns first raised to power second | x ** y |
Example: Arithmetic operators in Python
Python3
# Examples of Arithmetic Operatora = 9b = 4# Addition of numbersadd = a + b# Subtraction of numberssub = a - b# Multiplication of numbermul = a * b# Division(float) of numberdiv1 = a / b# Division(floor) of numberdiv2 = a // b# Modulo of both numbermod = a % b# Powerp = a ** b# print resultsprint(add)print(sub)print(mul)print(div1)print(div2)print(mod)print(p) |
13 5 36 2.25 2 1 6561
Note: Refer to Differences between / and // for some interesting facts about these two operators.
Comparison Operators
Comparison of Relational operators compares the values. It either returns True or False according to the condition.Operator Description Syntax > Greater than: True if the left operand is greater than the right x > y < Less than: True if the left operand is less than the right x < y == Equal to: True if both operands are equal x == y != Not equal to – True if operands are not equal x != y >= Greater than or equal to True if the left operand is greater than or equal to the right x >= y <= Less than or equal to True if the left operand is less than or equal to the right x <= y
Example: Comparison Operators in Python
Python3
# Examples of Relational Operatorsa = 13b = 33# a > b is Falseprint(a > b)# a < b is Trueprint(a < b)# a == b is Falseprint(a == b)# a != b is Trueprint(a != b)# a >= b is Falseprint(a >= b)# a <= b is Trueprint(a <= b) |
False True False True False True
Logical Operators
Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It is used to combine conditional statements.Operator Description Syntax and Logical AND: True if both the operands are true x and y or Logical OR: True if either of the operands is true x or y not Logical NOT: True if the operand is false not x
Example: Logical Operators in Python
Python3
# Examples of Logical Operatora = Trueb = False# Print a and b is Falseprint(a and b)# Print a or b is Trueprint(a or b)# Print not a is Falseprint(not a) |
False True False
Bitwise Operators
Bitwise operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.Operator Description Syntax & Bitwise AND x & y | Bitwise OR x | y ~ Bitwise NOT ~x ^ Bitwise XOR x ^ y >> Bitwise right shift x>> << Bitwise left shift x<<
Example: Bitwise Operators in Python
Python3
# Examples of Bitwise operatorsa = 10b = 4# Print bitwise AND operationprint(a & b)# Print bitwise OR operationprint(a | b)# Print bitwise NOT operationprint(~a)# print bitwise XOR operationprint(a ^ b)# print bitwise right shift operationprint(a >> 2)# print bitwise left shift operationprint(a << 2) |
0 14 -11 14 2 40
Assignment Operators
Assignment operators are used to assigning values to the variables.
| Operator | Description | Syntax |
|---|---|---|
| = | Assign value of right side of expression to left side operand | x = y + z |
| += | Add AND: Add right-side operand with left side operand and then assign to left operand | a+=b a=a+b |
| -= | Subtract AND: Subtract right operand from left operand and then assign to left operand | a-=b a=a-b |
| *= | Multiply AND: Multiply right operand with left operand and then assign to left operand | a*=b a=a*b |
| /= | 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 to left operand | a%=b a=a%b |
| //= | Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand | a//=b a=a//b |
| **= | Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand | a**=b a=a**b |
| &= | 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 |
Example: Assignment Operators in Python
Python3
# Examples of Assignment Operatorsa = 10# Assign valueb = aprint(b)# Add and assign valueb += aprint(b)# Subtract and assign valueb -= aprint(b)# multiply and assignb *= aprint(b)# bitwise lishift operatorb <<= aprint(b) |
10 20 10 100 102400
Identity Operators
is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical.
is True if the operands are identical is not True if the operands are not identical
Example: Identity Operator
Python3
a = 10b = 20c = aprint(a is not b)print(a is c) |
True True
Membership Operators
in and not in are the membership operators; used to test whether a value or variable is in a sequence.
in True if value is found in the sequence not in True if value is not found in the sequence
Example: Membership Operator
Python3
# Python program to illustrate# not 'in' operatorx = 24y = 20list = [10, 20, 30, 40, 50]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") |
x is NOT present in given list y is present in given list
Precedence and Associativity of Operators
Precedence and Associativity of Operators: Operator precedence and associativity determine the priorities of the operator.
Operator Precedence
This is used in an expression with more than one operator with different precedence to determine which operation to perform first.
Example: Operator Precedence
Python3
# Examples of Operator Precedence# Precedence of '+' & '*'expr = 10 + 20 * 30print(expr)# Precedence of 'or' & 'and'name = "Alex"age = 0if name == "Alex" or name == "John" and age >= 2: print("Hello! Welcome.")else: print("Good Bye!!") |
610 Hello! Welcome.
Operator Associativity
If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.
Example: Operator Associativity
Python3
# Examples of Operator Associativity# Left-right associativity# 100 / 10 * 10 is calculated as# (100 / 10) * 10 and not# as 100 / (10 * 10)print(100 / 10 * 10)# Left-right associativity# 5 - 2 + 3 is calculated as# (5 - 2) + 3 and not# as 5 - (2 + 3)print(5 - 2 + 3)# left-right associativityprint(5 - (2 + 3))# right-left associativity# 2 ** 3 ** 2 is calculated as# 2 ** (3 ** 2) and not# as (2 ** 3) ** 2print(2 ** 3 ** 2) |
100.0 6 0 512

