0% found this document useful (0 votes)
10 views8 pages

Python Operators Explained

The document provides an overview of Python operators, categorizing them into various types including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each category is explained with examples demonstrating their usage and functionality. Additionally, it discusses operator precedence, which dictates the order of evaluation in expressions.

Uploaded by

rvijistephen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Python Operators Explained

The document provides an overview of Python operators, categorizing them into various types including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Each category is explained with examples demonstrating their usage and functionality. Additionally, it discusses operator precedence, which dictates the order of evaluation in expressions.

Uploaded by

rvijistephen
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Home Whiteboard AI Assistant Online Compilers Jobs Tools Articles

Chapters Categories

SQL HTML CSS Javascript Python Java C C++ PHP Scala


Types of Python Operators
Python operators are categorized in the following categories −

Python - Operators
Arithmetic Operators

Comparison (Relational) Operators

Assignment Operators

Logical Operators

Bitwise Operators
Python Operators
Membership Operators
Python operators are special symbols used to perform specific operations on one or
Identity Operators
more operands. The variables, values, or expressions can be used as operands. For
example, Python's addition operator (+) is used to perform addition operations on two
variables, values, or expressions. Let us have a look at all the operators one by one.

The following are some of the terms related to Python operators:


Python Arithmetic Operators
Unary operators: Python operators that require one operand to perform a Python Arithmetic operators are used to perform basic mathematical operations such
specific operation are known as unary operators. as addition, subtraction, multiplication, etc.

Binary operators: Python operators that require two operands to perform a


The following table contains all arithmetic operators with their symbols, names, and
specific operation are known as binary operators.
examples (assume that the values of a and b are 10 and 20, respectively) −
Operands: Variables, values, or expressions that are used with the operator
to perform a specific operation. Operator Name Example

+ Addition a + b = 30

- Subtraction a b = -10

* Multiplication a * b = 200

/ Division b/a=2
Advertisement Advertisement
- -
% Modulus b%a=0

** Exponent a**b =10**20

// Floor Division 9//2 = 4


a: 2 b: 3 a**b: 8
Example of Python Arithmetic Operators a: 10 b: 5 a//b: 2

Open Compiler

Python Comparison Operators


a = 21 Python Comparison operators compare the values on either side of them and decide
b = 10 the relation among them. They are also called Relational operators.
c = 0
The following table contains all comparison operators with their symbols, names, and
c = a + b examples (assume that the values of a and b are 10 and 20, respectively) −

print ("a: {} b: {} a+b: {}".format(a,b,c))


Operator Name Example
c = a - b
== Equal (a == b) is not true.
print ("a: {} b: {} a-b: {}".format(a,b,c) )
!= Not equal (a != b) is true.
c = a * b
print ("a: {} b: {} a*b: {}".format(a,b,c)) > Greater than (a > b) is not true.

< Less than (a < b) is true.


c = a / b
print ("a: {} b: {} a/b: {}".format(a,b,c)) >= Greater than or equal to (a >= b) is not true.

<= Less than or equal to (a <= b) is true.


c = a % b
print ("a: {} b: {} a%b: {}".format(a,b,c))
Example of Python Comparison Operators
a = 2
b = 3
Open Compiler
c = a**b
print ("a: {} b: {} a**b: {}".format(a,b,c))
a = 21
a = 10 b = 10
b = 5 if ( a == b ):
c = a//b print ("Line 1 - a is equal to b")
print ("a: {} b: {} a//b: {}".format(a,b,c)) else:
print ("Line 1 - a is not equal to b")
Advertisement Advertisement
Output if ( a != b ):
- -
print ("Line 2 - a is not equal to b")
a: 21 b: 10 a+b: 31 else:
a: 21 b: 10 a-b: 11 print ("Line 2 - a is equal to b")
a: 21 b: 10 a*b: 210
a: 21 b: 10 a/b: 2.1 if ( a < b ):
a: 21 b: 10 a%b: 1 print ("Line 3 - a is less than b" )
else:
+= a += 30 a = a + 30
print ("Line 3 - a is not less than b")
-= a -= 15 a = a - 15
if ( a > b ):
*= a *= 10 a = a * 10
print ("Line 4 - a is greater than b")
else: /= a /= 5 a=a/5
print ("Line 4 - a is not greater than b")
%= a %= 5 a=a%5
a,b=b,a #values of a and b swapped. a becomes 10, b becomes 21
**= a **= 4 a = a ** 4

if ( a <= b ): //= a //= 5 a = a // 5


print ("Line 5 - a is either less than or equal to b")
else: &= a &= 5 a=a&5
print ("Line 5 - a is neither less than nor equal to b")
|= a |= 5 a=a|5

if ( b >= a ): ^= a ^= 5 a=a^5
print ("Line 6 - b is either greater than or equal to b")
>>= a >>= 5 a = a >> 5
else:
print ("Line 6 - b is neither greater than nor equal to b") <<= a <<= 5 a = a << 5

Output Example of Python Assignment Operators

Line 1 - a is not equal to b Open Compiler


Line 2 - a is not equal to b
Line 3 - a is not less than b
Line 4 - a is greater than b a = 21

Line 5 - a is either less than or equal to b b = 10

Line 6 - b is either greater than or equal to b c = 0


print ("a: {} b: {} c : {}".format(a,b,c))
c = a + b
Python Assignment Operators print ("a: {} c = a + b: {}".format(a,c))

Python Assignment operators are used to assign values to variables. Following is a c += a


table which shows all Python assignment operators. print ("a: {} c += a: {}".format(a,c))
Advertisement Advertisement
The following table contains all assignment operators with their symbols, names, and
- c *= a -
examples −
print ("a: {} c *= a: {}".format(a,c))

Operator Example Same As


c /= a
= a = 10 a = 10 print ("a: {} c /= a : {}".format(a,c))
c = 2
>> Signed right shift a >> 3
print ("a: {} b: {} c : {}".format(a,b,c))
c %= a
print ("a: {} c %= a: {}".format(a,c)) Example of Python Bitwise Operators

c **= a Open Compiler


print ("a: {} c **= a: {}".format(a,c))

a = 20
c //= a
b = 10
print ("a: {} c //= a: {}".format(a,c))

print ('a=',a,':',bin(a),'b=',b,':',bin(b))
Output c = 0

a: 21 b: 10 c : 0 c = a & b;
a: 21 c = a + b: 31 print ("result of AND is ", c,':',bin(c))
a: 21 c += a: 52
a: 21 c *= a: 1092 c = a | b;
a: 21 c /= a : 52.0 print ("result of OR is ", c,':',bin(c))
a: 21 b: 10 c : 2
a: 21 c %= a: 2 c = a ^ b;
a: 21 c **= a: 2097152 print ("result of EXOR is ", c,':',bin(c))
a: 21 c //= a: 99864
c = ~a;
print ("result of COMPLEMENT is ", c,':',bin(c))
Python Bitwise Operators
c = a << 2;
Python Bitwise operator works on bits and performs bit by bit operation. These print ("result of LEFT SHIFT is ", c,':',bin(c))
operators are used to compare binary numbers.
c = a >> 2;
The following table contains all bitwise operators with their symbols, names, and
examples − print ("result of RIGHT SHIFT is ", c,':',bin(c))

Operator Name Example


Output
& AND a&b
Advertisement a= 20 : 0b10100Advertisement
b= 10 : 0b1010
| OR a|b result of AND is 0 : 0b0
- -
result of OR is 30 : 0b11110
^ XOR a^b
result of EXOR is 30 : 0b11110
~ NOT ~a result of COMPLEMENT is -21 : -0b10101
result of LEFT SHIFT is 80 : 0b1010000
<< Zero fill left shift a << 3
result of RIGHT SHIFT is 5 : 0b101
Python Logical Operators Returns True if it finds a variable in
in the specified sequence, false a in b
Python logical operators are used to combile two or more conditions and check the otherwise.
final result. There are following logical operators supported by Python language.
Assume variable a holds 10 and variable b holds 20 then returns True if it does not finds a
not in variable in the specified sequence a not in b
The following table contains all logical operators with their symbols, names, and and false otherwise.
examples −

Operator Name Example


Example of Python Membership Operators

and AND a and b Open Compiler

or OR a or b
a = 10
not NOT not(a)
b = 20
list = [1, 2, 3, 4, 5 ]
Example of Python Logical Operators
print ("a:", a, "b:", b, "list:", list)

Open Compiler
if ( a in list ):
print ("a is present in the given list")
var = 5 else:
print ("a is not present in the given list")
print(var > 3 and var < 10)
print(var > 3 or var < 4) if ( b not in list ):
print(not (var > 3 and var < 10)) print ("b is not present in the given list")
else:
print ("b is present in the given list")
Output

c=b/a
True
print ("c:", c, "list:", list)
True
if ( c in list ):
False
print ("c is available in the given list")
else:
print ("c is not available in the given list")
Python Membership Operators
Advertisement Advertisement
- -
Python's membership operators test for membership in a sequence, such as strings,
Output
lists, or tuples.

There are two membership operators as explained below − a: 10 b: 20 list: [1, 2, 3, 4, 5]


a is not present in the given list
Operator Description Example b is not present in the given list
c: 2.0 list: [1, 2, 3, 4, 5] Operators precedence decides the order of the evaluation in which an operator is
c is available in the given list evaluated. Python operators have different levels of precedence. The following table
contains the list of operators having highest to lowest precedence −

The following table lists all operators from highest precedence to lowest.
Python Identity Operators
Python identity operators compare the memory locations of two objects. [Link]. Operator & Description

There are two Identity operators explained below − **


1
Exponentiation (raise to the power)
Operator Description Example
~+-
Returns True if both variables are 2 Complement, unary plus and minus (method names for the last two are
is a is b +@ and -@)
the same object and false otherwise.

Returns True if both variables are * / % //


3
is not not the same object and false a is not b Multiply, divide, modulo and floor division
otherwise.
+-
4
Addition and subtraction
Example of Python Identity Operators
>> <<
5
Right and left bitwise shift
Open Compiler
&
6
Bitwise 'AND'
a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5] ^|
7
c = a Bitwise exclusive `OR' and regular `OR'

<= < > >=


print(a is c) 8
Comparison operators
print(a is b)
<> == !=
9
print(a is not c) Equality operators
print(a is not b)
= %= /= //= -= += *= **=
10
Assignment operators
Output
is is not
Advertisement
11 Advertisement
Identity operators
True - -
False in not in
12
False Membership operators
True
not or and
13
Logical operators

Python Operators Precedence


Read more about the Python operators precedence here: Python operators precedence CERTIFICATIONS

Business Analytics Certification

Java & Spring Boot Advanced Certification


Data Science Advanced Certification
TOP TUTORIALS
Cloud Computing And DevOps
Python Tutorial
Advanced Certification In Business Analytics
Java Tutorial
Artificial Intelligence And Machine Learning
C++ Tutorial
DevOps Certification
C Programming Tutorial
Game Development Certification
C# Tutorial
Front-End Developer Certification
PHP Tutorial
AWS Certification Training
R Tutorial
Python Programming Certification
HTML Tutorial

CSS Tutorial COMPILERS & EDITORS


JavaScript Tutorial
Online Java Compiler
SQL Tutorial
Online Python Compiler

Online Go Compiler
TRENDING TECHNOLOGIES
Online C Compiler
Cloud Computing Tutorial
Online C++ Compiler
Amazon Web Services Tutorial
Online C# Compiler
Microsoft Azure Tutorial
Online PHP Compiler
Git Tutorial
Online MATLAB Compiler
Ethical Hacking Tutorial
Online Bash Compiler
Docker Tutorial
Online SQL Compiler
Kubernetes Tutorial
Online Html Editor
DSA Tutorial
Spring Boot Tutorial
ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |
SDLC Tutorial
PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S
Unix Tutorial

Advertisement Advertisement
- -
Tutorials Point is a leading Ed Tech company striving to provide the best learning material
on technical and non-technical subjects.

© Copyright 2025. All Rights Reserved.

Advertisement
-

Common questions

Powered by AI

Python operators are categorized as arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. Arithmetic operators perform basic math operations like addition and subtraction ; comparison operators compare values and determine relational logic ; assignment operators assign values to variables ; logical operators combine conditional statements ; bitwise operators work on bits to perform binary operations ; membership operators test membership of a value in a sequence ; identity operators check if two variables point to the same object in memory .

Bitwise operators manipulate individual bits of integer values in Python, performing operations such as AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). For instance, the AND operator compares corresponding bits of two numbers; a & b results in bits that are set to 1 only if both corresponding bits are 1 . The OR operator sets a bit to 1 if at least one of the compared bits is 1. Similarly, the XOR operator sets a bit to 1 if the compared bits are different. The left shift operator shifts bits to the left by a specified number of positions, doubling the number for each shift, while the right shift operator does the opposite .

Logical operators in Python, such as 'and', 'or', and 'not', are used to combine logical statements. The 'and' operator returns True only if both conditions are true; the result is False otherwise. For example, 'a and b' is True if both a and b are true . The 'or' operator returns True if at least one of the conditions is true. The 'not' operator is used to reverse the logical state of its operand, turning a true condition to false and vice versa. These operators are essential in controlling program flow based on multiple conditions .

The floor division operator '//' in Python is used to divide two numbers and round down to the nearest whole number, discarding any fractional result. It is particularly useful in integer computations where a precise integer result without decimal values is needed, such as determining how many complete groups can be formed from a total number of items. For instance, '9 // 2' results in '4', as it calculates the number of whole times 2 fits into 9 without exceeding it . This makes it essential in tasks requiring precise division results in integer-only contexts.

Python's assignment operators are used to assign values to variables. Beyond the basic '=' operator, Python supports compound assignments like '+=', '-=', '*=', '/=', which combine an arithmetic operation with assignment. For example, 'a += 10' is equivalent to 'a = a + 10', saving space and enhancing readability . Compound assignments modify the variable in place, facilitating efficient re-assignments without explicitly repeating the variable. This utility becomes notably useful in loops or dynamic calculations where variable values change frequently.

Membership operators 'in' and 'not in' are used to test whether a value or variable exists within a sequence such as lists, tuples, or strings. The 'in' operator returns True if the specified value is found in the sequence, while 'not in' returns True if the value is not found. For example, if list = [1, 2, 3], then 2 in list evaluates to True as 2 is an element of the list, while 5 not in list also evaluates to True as 5 is absent . These operators simplify checking for value presence in collections.

Identity operators 'is' and 'is not' in Python check whether two variables reference the same object in memory, not just if their values are equal. For example, if a = [1, 2] and b = a, then a is b returns True because both a and b refer to the same list object. However, a = [1, 2] and b = [1, 2] would cause a is b to return False, even though a == b would return True because both lists have the same content . This distinguishes identity operators from equality operators, which only compare values' similarity.

Bitwise operators in Python perform low-level operations directly on the bits of integer values, offering advantages such as speed and memory efficiency in certain computations. They're beneficial in scenarios like managing flags in binary format or optimizing performance in embedded systems where resources are constrained. However, bitwise operations can be less intuitive and harder to understand than higher-level operations, often leading to code that's more difficult to maintain. They also lack the safety and abstraction provided by higher-level operations that handle complex data structures . Therefore, while powerful, their use is typically reserved for situations where performance is critical and complexity can be managed.

Online compilers are critical in programming education as they provide immediate access to code execution environments without requiring local installation of development tools. This facilitates learning new programming languages by allowing students to experiment with code, see instantaneous results, and debug with instant feedback . They support a wide range of languages, making them versatile educational tools that can accommodate various learning needs. Furthermore, online compilers facilitate collaborative learning and sharing, which can enhance the educational experience significantly in a learning context.

Operator precedence in Python determines the order in which parts of a mathematical or logical expression are evaluated. Operators with higher precedence are evaluated before those with lower precedence. For example, exponentiation has the highest precedence, followed by unary operations, then multiplication/division/modulus/floor division, followed by addition/subtraction, bit shifts, bitwise AND, XOR, and OR, comparison, equality, assignment, identity, membership, and finally logical operators . This order affects the grouping and result of the evaluation significantly.

You might also like