0% found this document useful (0 votes)
35 views15 pages

Python Operators - GeeksforGeeks

1234

Uploaded by

ap6902557
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)
35 views15 pages

Python Operators - GeeksforGeeks

1234

Uploaded by

ap6902557
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
You are on page 1/ 15

6/10/25, 10:01 AM Python Operators - GeeksforGeeks

Search...

Python Course Python Tutorial Interview Questions Python Quiz Python Glossary Python Proje

Python Operators
Last Updated : 17 May, 2025

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 of Python 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


1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Identity Operators and Membership Operators

Arithmetic Operators in Python


Python Arithmetic operators are used to perform basic mathematical
operations like addition, subtraction, multiplication and division.
https://www.geeksforgeeks.org/python-operators/ 1/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

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:

# Variables
a = 15
b = 4

# Addition
print("Addition:", a + b)

# Subtraction
print("Subtraction:", a - b)

# Multiplication
print("Multiplication:", a * b)

# Division
print("Division:", a / b)

# Floor Division
print("Floor Division:", a // b)

# Modulus
print("Modulus:", a % b)

# Exponentiation
print("Exponentiation:", a ** b)

Output

Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Floor Division: 3
Modulus: 3
Exponentiation: 50625

https://www.geeksforgeeks.org/python-operators/ 2/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

Note: Refer to Differences between / and // for some interesting facts


about these two Python operators.

Comparison of Python Operators


In Python Comparison of Relational operators compares the values. It
either returns True or False according to the condition.

Example of Comparison Operators in Python

Let's see an example of Comparison Operators in Python.

a = 13
b = 33

print(a > b)
print(a < b)
print(a == b)
print(a != b)
print(a >= b)
print(a <= b)

Output

False
True
False
True
False
True

Logical Operators in Python


Python Logical operators perform Logical AND, Logical OR and Logical
NOT operations. It is used to combine conditional statements.

https://www.geeksforgeeks.org/python-operators/ 3/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

The precedence of Logical Operators in Python is as follows:

1. Logical not
2. logical and
3. logical or

Example of Logical Operators in Python:

a = True
b = False
print(a and b)
print(a or b)
print(not a)

Output

False
True
False

Bitwise Operators in Python


Python Bitwise 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:

1. Bitwise NOT
2. Bitwise Shift

https://www.geeksforgeeks.org/python-operators/ 4/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

3. Bitwise AND
4. Bitwise XOR
5. Bitwise OR

Example of Bitwise Operators in Python:

a = 10
b = 4

print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)

Output

0
14
-11
14
2
40

Assignment Operators in Python


Python Assignment 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:

a = 10
b = a
print(b)
b += a
print(b)
b -= a
print(b)
b *= a
https://www.geeksforgeeks.org/python-operators/ 5/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

print(b)
b <<= a
print(b)

Output

10
20
10
100
102400

Identity Operators in Python


In Python, 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 of Identity Operators in Python:

a = 10
b = 20
c = a

print(a is not b)
print(a is c)

Output

True
True

Membership Operators in Python


In Python, in and not 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

https://www.geeksforgeeks.org/python-operators/ 6/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

not in True if value is not found in the sequence

Examples of Membership Operators in Python:

x = 24
y = 20
list = [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")

Output

x is NOT present in given list


y 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 a single line replacing the


multiline if-else making the code compact.

Syntax : [on_true] if [expression] else [on_false]

Examples of Ternary Operator in Python:

a, b = 10, 20
min = a if a < b else b

https://www.geeksforgeeks.org/python-operators/ 7/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

print(min)

Output

10

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:

expr = 10 + 20 * 30
print(expr)
name = "Alex"
age = 0

if name == "Alex" or name == "John" and age >= 2:


print("Hello! Welcome.")
else:
print("Good Bye!!")

Output

610
Hello! Welcome.

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:

print(100 / 10 * 10)
https://www.geeksforgeeks.org/python-operators/ 8/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks
p ( / )
print(5 - 2 + 3)
print(5 - (2 + 3))
print(2 ** 3 ** 2)

Output

100.0
6
0
512

To try your knowledge of Python Operators, you can take out the quiz
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

num1 = 5
num2 = 2

sum = num1 + num2


difference = num1 - num2
product = num1 * num2
quotient = num1 / num2
remainder = num1 % num2

print("Sum:", sum)
print("Difference:", difference)
print("Product:", product)
print("Quotient:", quotient)
print("Remainder:", remainder)

Output

Sum: 7
Difference: 3

https://www.geeksforgeeks.org/python-operators/ 9/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

Product: 10
Quotient: 2.5
Remainder: 1

Q2. Code to implement Comparison operations on integers

num1 = 30
num2 = 35

if num1 > num2:


print("The first number is greater.")
elif num1 < num2:
print("The second number is greater.")
else:
print("The numbers are equal.")

Output

The second number is greater.

Quiz:

Python Operators Quiz

Related Posts:

Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Identity Operators and Membership Operators
Modulo Operator
Division Operator
Ternary Operator
Operator Overloading
OR operator
Why are there no ++ and - Operator in Python
How to do Math in Python 3 with Operators

https://www.geeksforgeeks.org/python-operators/ 10/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

Difference between == and is Operator in Python

Recommended Problems:

Arithmetic Operators
Logical Operators
Bitwise Operators
The Modulo Task
Last Digit of a number
Sum of N Numbers
GCD
LCM
Armstrong Number
Count Set Bits
Evaluate Formulae
AP Term
Geometric Progression
Celsius to Fahrenheit Conversion
Sum of AP series
LCM And GCD
Factorial of Large number
Count trailing zeroes
Last Non-Zero digit

Explore more Exercises: Practice Exercise on Operators in Python

Comment More info


Next Article

Campus Training Program


Python Keywords

Similar Reads

Python Features
Python is a dynamic, high-level, free open source, and interpreted
programming language. It supports object-oriented programming as wel…
https://www.geeksforgeeks.org/python-operators/ 11/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

15+ min read

Python Crash Course


If you are aware of programming languages and ready to unlock the
power of Python, enter the world of programming with this free Python…

15+ min read

Python Arrays
Lists in Python are the most flexible and commonly used data structure
for sequential storage. They are similar to arrays in other languages but…

15+ min read

Python Docstrings
When it comes to writing clean, well-documented code, Python
developers have a secret weapon at their disposal – docstrings.…

15+ min read

Python Modules
Python Module is a file that contains built-in functions, classes,its and
variables. There are many Python modules, each with its specific work.In…

15+ min read

Python List methods


Python list methods are built-in functions that allow us to perform various
operations on lists, such as adding, removing, or modifying elements. In…

15+ min read

History of Python
Python is a widely used general-purpose, high-level programming
language. It was initially designed by Guido van Rossum in 1991 and…

15+ min read

https://www.geeksforgeeks.org/python-operators/ 12/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

Python vs Cpython
Python is a high-level, interpreted programming language favored for its
readability and versatility. It's widely used in web development, data…

15+ min read

Python 3.13 New Features


Nearly annually, Python releases a new version. The most recent version,
Python 3.13, will be available on May 8, 2024, following Python 3.12 in…

15+ min read

Python Naming Conventions


Python, known for its simplicity and readability, places a strong emphasis
on writing clean and maintainable code. One of the key aspects…

15+ min read

Corporate & Communications Address:


A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)

Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305

Advertise with us

Company Explore
About Us Job-A-Thon

https://www.geeksforgeeks.org/python-operators/ 13/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

Legal Offline Classroom Program


Privacy Policy DSA in JAVA/C++
Careers Master System Design
In Media Master CP
Contact Us Videos
Corporate Solution
Campus Training Program

Tutorials DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android

Data Science & ML Web Technologies


Data Science With Python HTML
Machine Learning CSS
ML Maths JavaScript
Data Visualisation TypeScript
Pandas ReactJS
NumPy NextJS
NLP NodeJs
Deep Learning Bootstrap
Tailwind CSS

Python Tutorial Computer Science


Python Examples GATE CS Notes
Django Tutorial Operating Systems
Python Projects Computer Network
Python Tkinter Database Management System
Web Scraping Software Engineering
OpenCV Tutorial Digital Logic Design
Python Interview Question Engineering Maths

DevOps System Design


Git High Level Design
AWS Low Level Design
Docker UML Diagrams
Kubernetes Interview Guide
Azure Design Patterns
GCP OOAD
DevOps Roadmap System Design Bootcamp
Interview Questions

School Subjects Databases

https://www.geeksforgeeks.org/python-operators/ 14/15
6/10/25, 10:01 AM Python Operators - GeeksforGeeks

Mathematics SQL
Physics MYSQL
Chemistry PostgreSQL
Biology PL/SQL
Social Science MongoDB
English Grammar

Preparation Corner More Tutorials


Company-Wise Recruitment Process Software Development
Aptitude Preparation Software Testing
Puzzles Product Management
Company-Wise Preparation Project Management
Linux
Excel
All Cheat Sheets

Courses Programming Languages


IBM Certification Courses C Programming with Data Structures
DSA and Placements C++ Programming Course
Web Development Java Programming Course
Data Science Python Full Course
Programming Languages
DevOps & Cloud

Clouds/Devops GATE 2026


DevOps Engineering GATE CS Rank Booster
AWS Solutions Architect Certification GATE DA Rank Booster
Salesforce Certified Administrator Course GATE CS & IT Course - 2026
GATE DA Course 2026
GATE Rank Predictor

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

https://www.geeksforgeeks.org/python-operators/ 15/15

You might also like