Python Operators: Types, Examples & Precedence

Python Operators Tutorial for Beginners to Learn

This tutorial provides an in-depth overview of Python operators. There are various kinds of operators in Python including Arithmetic, Comparison, Assignment, Logical, Bitwise, Identity, and Membership operators. Here, you’ll learn their syntax and practice with numerous examples. You must clearly understand their meaning in Python in order to write better programs. Let’s explore what are they, what purpose they cater to, and how to use them.

What Is an Operator in Python?

Python operator is a symbol represented by a special character, gets the input from one or more operands, and performs a specific task.

Like many programming languages, Python reserves some special characters for acting as operators. Every operator carries out some operations such as addition, and multiplication to manipulate data and variables. The variables passed as input to an operator are known as operands.

>>> 7%4
3

In the above case, ‘%’ is the modulus operator that calculates the remainder of the division. The numbers ‘7’ and ‘4’ passed as input are the operands, whereas the number ‘3’ is the result of the action performed. We also recommend you read about keywords in Python.

Arithmetic Operators

With arithmetic operators, we can do various arithmetic operations like addition, subtraction, multiplication, division, modulus, exponent, etc. Python provides multiple ways for arithmetic calculations like eval function, declare variable & calculate, or call functions. The table below outlines the built-in arithmetic operators in Python.

OperatorUsagePurpose
+a + bAddition – Sum of two operands
a – bSubtraction – Difference between the two operands
*a * bMultiplication – Product of the two operands
/a / bFloat Division – Quotient of the two operands
//a // bFloor Division – Quotient of the two operands (Without fractional part)
%a % bModulus – Integer remainder after division of ‘a’ by ‘b.’
Note: It is also known as the percent operator.
**a ** bExponent – Product of ‘a’ by itself ‘b’ times (a to the power of b)
Note: It is also known as the exponentiation operator.
The List of Arithmetic operators and their Meaning in Python

Example-

a=7
b=4

print('Sum : ', a+b)
print('Subtraction : ', a-b)
print('Multiplication : ', a*b)
print('Division (float) : ', a/b)
print('Division (floor) : ', a//b)
print('Modulus : ', a%b)
print('Exponent : ', a**b)

Output-

Sum : 11
Subtraction : 3
Multiplication : 28
Division (float) : 1.75
Division (floor) : 1
Modulus : 3
Exponent : 2401

Comparison Operators

In Python programming, comparison operators allow us to determine whether two values are equal or if one is higher than the other and then make a decision based on the result. The table below outlines the built-in comparison operators in Python.

OperatorUsagePurpose
>a > bGreater than – if the left operand is greater, then it returns true.
<a < bLess than – if the left operand is less, then it returns true.
==a == bEqual to – if two operands are equal, then it returns true.
!=a != bNot equal to – if two operands are not equal, then it returns true.
>=a >= bGreater than or equal – True if the left operand is greater or equal.
<=a <= bLess than or equal – True if the left operand is less than or equal.
Comparison Operators and their meaning in Python

Example-

a=7
b=4

print('a > b is',a>b)

print('a < b is',a<b)

print('a == b is',a==b)

print('a != b is',a!=b)

print('a >= b is',a>=b)

print('a <= b is',a<=b)

Output-

a > b is True
a < b is False
a == b is False
a != b is True
a >= b is True
a <= b is False

Logical Operators

Logical Python operators enable us to make decisions based on multiple conditions. The operands act as conditions that can result in a true or false value. The outcome of such an operation is either true or false (i.e., a Boolean value).

However, not all of these operators return a boolean result. The ‘and’ and ‘or’ operators do return one of their operands instead of a pure boolean value. Whereas the ‘not’ operator always gives a real boolean outcome. Check the table and the example to know how these operators work in Python.

OperatorUsagePurpose
anda and bif ‘a’ is false, then ‘a’, else ‘b’
ora or bif ‘a’ is false, then ‘b’, else ‘a’
notnot aif ‘a’ is false, then True, else False
Logical Operators in Python

Example-

a=7
b=4

# Result: a and b is 4
print('a and b is',a and b)

# Result: a or b is 7
print('a or b is',a or b)

# Result: not a is False
print('not a is',not a)

Output-

a and b is 4
a or b is 7
not a is False

Bitwise Operators

Bitwise Python operators process the individual bits of integer values. They treat them as sequences of binary bits.

We can use bitwise operators to check whether a particular bit is set. For example, IoT applications read data from the sensors based on whether a specific bit is set or not. In such a situation, these operators can help.

OperatorUsagePurpose
&a & bBitwise AND – compares two operands on a bit level.
It returns 1 if both the corresponding bits are 1
|a | bBitwise OR – compares two operands on a bit level.
It returns 1 if any of the corresponding bits is 1
~~aBitwise NOT – inverts all of the bits in a single operand.
^a ^ bBitwise XOR – compares two operands on a bit level.
It returns 1 if any of the corresponding bits is 1, but not both
>>a >> bRight shift – shifts the bits of ‘a’ to the right by ‘b’ no. of times.
<<a << bLeft shift – shifts the bits of ‘a’ to the left by ‘b’ no. of times.
Bitwise operators and their meaning in Python

Out of the above bitwise operators, Python xor has various applications such as swapping two numbers, in cryptography to generate checksum and hash.

Example-

Let’s consider the numbers 4 and 6 whose binary representations are ‘00000100’ and ‘00000110’. Now, we’ll perform the AND operation on these numbers.

a=4
b=6

#Bitwise AND: The result of 'a & b' is 4
print('a & b is',a & b)

Output-

a & b is 4

The above result is the outcome of the following AND (‘&’) operation.

 0 0 0 0 0 1 0 0 &
 0 0 0 0 0 1 1 0
 ------------------
 0 0 0 0 0 1 0 0 (the binary representation of the number 4)

Assignment Operators

In Python, we can use assignment operators to set values into variables. The instruction a = 4 uses a primitive assignment operator that assigns the value 4 to the left operand.

Below is the list of available compound operators in Python. For example, the statement a += 4 adds to the variable and then assigns the same. It will evaluate to a = a + 4.

OperatorExampleSimilar to
= a = 4 a = 4
+= a += 4 a = a + 4
-= a -= 4 a = a – 4
*= a *= 4 a = a * 4
/= a /= 4 a = a / 4
%= a %= 4 a = a % 4
**= a **= 4 a = a ** 4
&= a &= 4 a = a & 4
|= a |= 4 a = a | 4
^= a ^= 4 a = a ^ 4
>>= a >>= 4 a = a >> 4
<<= a <<= 4 a = a << 4
List of assignments operators in Python

Advanced Python Operators

Python also bundles a few operators for special purposes. These are known as advanced Python operators like the identity operator or the membership operator.

Identity

These operators enable us to compare the memory locations of two Python objects/variables. They can let us find if the objects share the same memory address. The variables holding equal values are not necessarily identical.

Alternatively, we can use these operators to determine whether a value is of a specific class or type. Refer to the below table to understand more about them.

OperatorPurposeUsage
isTrue – if both the operands refer to the same object, else returns Falsea is b (True if id(a) and id(b) are the same)
is notTrue – if the operands refer to different objects, False otherwisea is not b  (True if id(a) and id(b) are different)
Identity operators and their meaning in Python

Example-

# Using 'is' identity operator
a = 7
if (type(a) is int):
 print("true")
else:
 print("false")

# Using 'is not' identity operator
b = 7.5
if (type(b) is not int):
 print("true")
else:
 print("false")

Output-

true
true

Membership

Membership operators enable us to test whether a value is a member of other Python objects such as strings, lists, or tuples.

In C, a membership test requires iterating through a sequence and checking each value. But Python makes it very easy to establish membership as compared to C.

Also, note that this operator can also test against a Python dictionary but only for the key, not the value.

OperatorPurposeUsage
inTrue – if the value exists in the sequence7 in [3, 7, 9]
not inTrue – if the value doesn’t found in the sequence7 not in [3, 5, 9]
Python membership operator

Example-

# Using Membership operator
str = 'Python operators'
dict = {6:'June',12:'Dec'}

print('P' in str) 
print('Python' in str)
print('python' not in str)
print(6 in dict) 
print('Dec' in dict)

Output-

True
True
True
True
False

Python Operators – Frequently Asked Questions

Can Python operators be used with custom objects?

Yes, you can define operator behavior for custom objects by implementing special methods like __add__, __eq__, or __and__.

What happens when operators are used with mixed types?

Python usually performs type conversion when possible (e.g., int + float results in float). Some operations may raise TypeError if types are incompatible.

Are all operators in Python evaluated left to right?

Not always. Operator precedence determines the order of evaluation. For example, * and / have higher precedence than + and -.

Can bitwise operators be used with booleans?

Yes, since booleans are subclasses of integers, bitwise operators work. For example, True & False evaluates to False.

What are common pitfalls with logical operators?

Unlike other languages, and and or return one of the operands, not always a boolean. Use bool() if a strict True/False value is required.

Do assignment operators like += or &= modify the variable in place?

Yes, in-place operators update the variable directly when possible, which can be more memory-efficient for mutable types like lists.

Can membership and identity operators be combined with logical operators?

Yes, you can combine them. For example: if x in list and x is not None: is valid and commonly used.

Key Takeaways – Python Operators

This tutorial covered various Python operators, and their syntax, and described their operation with examples. Hence, it should now be easier for you to use operators in Python.

If you find something new to learn today, then do share it with others. And, follow us on our social media accounts to get notified of more such tutorials.

Best,
TechBeamers

Share This Article
I’m Meenakshi Agarwal, founder of TechBeamers.com and ex-Tech Lead at Aricent (10+ years). I built the Python online compiler, code checker, Selenium labs, SQL quizzes, and tutorials to help students and working professionals.
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *