In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types ofPython operators.
- OPERATORS:These are the special symbols. Eg- + , * , /, etc.
- OPERAND:It is the value on which the operator is applied.
Types of Operators in Python

Arithmetic Operators in Python
PythonArithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication anddivision.
In Python 3.x the result of division is a floating-point while in Python 2.x division of 2 integers was an integer. To obtain an integer result in Python 3.x floored (// integer) is used.
Example of Arithmetic Operators in Python:
Python# Variablesa=15b=4# Additionprint("Addition:",a+b)# Subtractionprint("Subtraction:",a-b)# Multiplicationprint("Multiplication:",a*b)# Divisionprint("Division:",a/b)# Floor Divisionprint("Floor Division:",a//b)# Modulusprint("Modulus:",a%b)# Exponentiationprint("Exponentiation:",a**b)
OutputAddition: 19Subtraction: 11Multiplication: 60Division: 3.75Floor Division: 3Modulus: 3Exponentiation: 50625
Note: Refer toDifferences between / and //for some interesting facts about these two Python operators.
Comparison of Python Operators
In PythonComparisonof Relational operators compares the values. It either returnsTrue orFalse according to the condition.
Example of Comparison Operators in Python
Let's see an example of Comparison Operators in Python.
Pythona=13b=33print(a>b)print(a<b)print(a==b)print(a!=b)print(a>=b)print(a<=b)
OutputFalseTrueFalseTrueFalseTrue
Logical Operators in Python
PythonLogical operators performLogical AND,Logical OR and Logical NOT operations. It is used to combine conditional statements.
The precedence of Logical Operators in Python is as follows:
- Logical not
- logical and
- logical or
Example of Logical Operators in Python:
Pythona=Trueb=Falseprint(aandb)print(aorb)print(nota)
Bitwise Operators in Python
PythonBitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers.
Bitwise Operators in Python are as follows:
- Bitwise NOT
- Bitwise Shift
- Bitwise AND
- Bitwise XOR
- Bitwise OR
Example of Bitwise Operators in Python:
Pythona=10b=4print(a&b)print(a|b)print(~a)print(a^b)print(a>>2)print(a<<2)
Assignment Operators in Python
PythonAssignment operators are used to assign values to the variables. This operator is used to assign the value of the right side of the expression to the left side operand.
Example of Assignment Operators in Python:
Pythona=10b=aprint(b)b+=aprint(b)b-=aprint(b)b*=aprint(b)b<<=aprint(b)
Identity Operators in Python
In Python,is andis not are theidentity 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 of Identity Operators in Python:
Pythona=10b=20c=aprint(aisnotb)print(aisc)
Membership Operators in Python
In Python,in andnot in are the membership operators that are 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
Examples of Membership Operators in Python:
Pythonx=24y=20list=[10,20,30,40,50]if(xnotinlist):print("x is NOT present in given list")else:print("x is present in given list")if(yinlist):print("y is present in given list")else:print("y is NOT present in given list")
Outputx is NOT present in given listy is present in given list
Ternary Operator in Python
in Python,Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5.
It simply allows testing a condition in asingle line replacing the multiline if-else making the code compact.
Syntax : [on_true] if [expression] else [on_false]
Examples of Ternary Operator in Python:
Pythona,b=10,20min=aifa<belsebprint(min)
Precedence and Associativity of Operators in Python
In Python,Operator precedence and associativity determine the priorities of the operator.
Operator Precedence in Python
This is used in an expression with more than one operator with different precedence to determine which operation to perform first.
Example:
Pythonexpr=10+20*30print(expr)name="Alex"age=0ifname=="Alex"orname=="John"andage>=2:print("Hello! Welcome.")else:print("Good Bye!!")
Operator Associativity in Python
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:
Pythonprint(100/10*10)print(5-2+3)print(5-(2+3))print(2**3**2)
To try your knowledge of Python Operators, you can take out thequiz on Operators in Python.
Python Operator Exercise Questions
Below are two Exercise Questions on Python Operators. We have covered arithmetic operators and comparison operators in these exercise questions. For more exercises on Python Operators visit the page mentioned below.
Q1.Code to implement basic arithmetic operations on integers
Pythonnum1=5num2=2sum=num1+num2difference=num1-num2product=num1*num2quotient=num1/num2remainder=num1%num2print("Sum:",sum)print("Difference:",difference)print("Product:",product)print("Quotient:",quotient)print("Remainder:",remainder)
OutputSum: 7Difference: 3Product: 10Quotient: 2.5Remainder: 1
Q2.Code to implement Comparison operations on integers
Pythonnum1=30num2=35ifnum1>num2:print("The first number is greater.")elifnum1<num2:print("The second number is greater.")else:print("The numbers are equal.")
OutputThe second number is greater.
Quiz:
Related Posts:
Recommended Problems:
Explore more Exercises:Practice Exercise on Operators in Python

Arithmetic Operators in Python

Logical Operators in Python

Identity Comparison Operators in Python

Bitwise Operators in Python Part 1

Bitwise Operators in Python Part 2