PDF 24
PDF 24
Python Java JavaScript SQL C++ HTML CSS React
← prev next →
Python Operators
In the following tutorial, we will discuss about the operators used in the Python
programming language.
1. Arithmetic Operators
2. Comparison Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Let us now discuss these operators used in Python in the following sections.
Arithmetic Operators
Python Arithmetic Operators are used on two operands to perform basic
mathematical operators like addition, subtraction, multiplication, and division. There
are different types of arithmetic operators available in Python including the '+'
operator for addition, '-' operator for subtraction, '*' for multiplication, '/' for division,
'%' for modulus, '**' for exponent and '//' for floor division.
Let us consider the following table of arithmetic operators for a detailed explanation.
Program Code:
Now we give code examples of arithmetic operators in Python. The code is given
below -
Example
Execute Now
Output:
Now we compile the above code in Python, and after successful compilation, we run it.
Then the output is given below -
For a = 46 and b = 4
Calculate the following:
1. Addition of two numbers: a + b = 50
2. Subtraction of two numbers: a - b = 42
3. Multiplication of two numbers: a * b = 184
4. Division of two numbers: a / b = 11.5
5. Floor division of two numbers: a // b = 11
6. Reminder of two numbers: a mod b = 2
7. Exponent of two numbers: a ^ b = 4477456
Comparison Operators
Python Comparison operators are mainly used for the purpose of comparing two
values or variables (operands) and return a Boolean value as either True or False
accordingly. There are various types of comparison operators available in Python
including the '==', '!=', '<=', '>=', '<', and '>'.
Program Code:
Now we give code examples of Comparison operators in Python. The code is given
below -
Example
Execute Now
Output:
Now we compile the above code in Python, and after successful compilation, we run it.
Then the output is given below -
For a = 46 and b = 4
Check the following:
1. Two numbers are equal or not: False
2. Two numbers are not equal or not: True
3. a is less than or equal to b: False
4. a is greater than or equal to b: True
5. a is greater b: True
6. a is less than b: False
Assignment Operators
Using the assignment operators, the right expression's value is assigned to the left
operand. Python offers different assignment operators to assign values to the
variable. These assignment operators include '=', '+=', '-=', '*=', '/=', '%=', '//=', '**=', '&=',
'|=', '^=', '>>=', and '<<='.
Let us consider the following table of some commonly used assignment operators for
a detailed explanation.
Program Code:
Now we give code examples of Assignment operators in Python. The code is given
below -
Example
Execute Now
Output:
Now we compile the above code in Python, and after successful compilation, we run it.
Then the output is given below -
a += b: 40
a -= b: 28
a *= b: 204
a /= b: 5.666666666666667
a %= b: 4
a **= b: 1544804416
a //= b: 5
Bitwise Operators
The two operands' values are processed bit by bit by the bitwise operators. There are
various Bitwise operators used in Python, such as bitwise OR (|), bitwise AND (&),
bitwise XOR (^), negation (~), Left shift (<<), and Right shift (>>).
Let us consider the following table of bitwise operators for a detailed explanation.
S. No. Operator Syntax Description
Program Code:
Now we give code examples of Bitwise operators in Python. The code is given below -
Example
Execute Now
Output:
Now we compile the above code in Python, and after successful compilation, we run it.
Then the output is given below -
a & b : 0
a | b : 15
a ^ b : 15
~a : -8
a << b : 1792
a >> b : 0
Logical Operators
The assessment of expressions to make decisions typically uses logical operators.
Python offers different types of logical operators such as and, or, and not. In the case
of the logical AND, if the first one is 0, it does not depend upon the second one. In the
case of the logical OR, if the first one is 1, it does not depend on the second one.
Let us consider the following table of the logical operators used in Python for a
detailed explanation.
Program Code:
Now we give code examples of Logical operators in Python. The code is given below -
Example
Execute Now
Output:
Now we compile the above code in Python, and after successful compilation, we run it.
Then the output is given below -
Membership Operators
We can verify the membership of a value inside a Python data structure using the
Python membership operators. The result is said to be true if the value or variable is in
the sequence (list, tuple, or dictionary); otherwise, it returns false.
Program Code:
Now we give code examples of Membership operators in Python. The code is given
below -
Example
# initializing a list
myList = [12, 22, 28, 35, 42, 49, 54, 65, 92, 103, 245, 874]
Execute Now
Output:
Now we compile the above code in Python, and after successful compilation, we run it.
Then the output is given below -
Given List: [12, 22, 28, 35, 42, 49, 54, 65, 92, 103, 245, 874]
x = 31 is NOT present in the given list.
y = 28 is present in the given list.
Identity Operators
Python offers two identity operators as is and is not, that 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.
Program Code:
Now we give code examples of Identity operators in Python. The code is given below -
Example
Execute Now
Output:
Now we compile the above code in python, and after successful compilation, we run it.
Then the output is given below -
a is c => True
a is not c => False
a is b => False
a is not b => True
a == b => True
a != b => False
Operator Precedence
The order in which the operators are examined is crucial to understand since it tells us
which operator needs to be considered first. Below is a list of the Python operators'
precedence tables.
Conclusion:
So, in this article, we are discussing all the Python Operators. We briefly discuss how
they work and share the program code using each operator in Python.
← prev next →
Related Posts
Python Tutorial
| Python Programming Language Python is one of the most popular and widely
used programming language in nowadays, because of its simplicity, extensive
features and support of libraries. Python also have clean and simple syntax
which makes it beginner-friendly, while it also provides powerful libraries and
frameworks...
4 min read
History of Python
Programming Language Python ranks among the most used and flexible
coding languages of the present day. It is also incredibly easy to read, write,
implement, and is well-supported with a variety of libraries. Based on these
factors, Python is renowned for numerous functions like building...
5 min read
4 min read
Python Comments
We'll study how to write comments in our program in this article. We'll also learn
about single-line comments, multi-line comments, documentation strings, and
other Python comments. Introduction to We may wish to describe the code we
develop. We might wish to take notes of why a...
3 min read
Python Features
Features of Python From the development of Ada Lovelace's machine algorithm
in 1843 to the latest and popular high-level programming languages like C++,
Python, and Java, a wide range of programming languages have been
developed by programmers in order to solve real-world challenging problems.
However, not...
13 min read
How to Install Python?
Introduction: Python is among the most widely used programming languages.
Due to its user-friendliness, readability, and extensive library ecosystem, whether
you are a novice or an expert developer, the first step to accomplish this is to
install Python on Windows if you wish to write and...
4 min read
Python Applications
Applications of Python Programming Python is known for its general-purpose
nature that makes it applicable in almost every domain of software
development. Python makes its presence in every emerging field. It is the
fastest-growing programming language and can develop any application. Top 10
Here, we are specifying...
3 min read
Python Keywords
Every scripting language has designated words or keywords, with particular
definitions and usage guidelines. Python is no exception. The fundamental
constituent elements of any Python program are Python keywords. This tutorial
will give you a basic overview of all Python keywords and a detailed discussion
of...
13 min read
Python Literals
can be defined as data that is given in a variable or constant. Python supports
the following literals: 1. String literals: String literals can be formed by enclosing a
text in the quotes. We can use both single as well as double quotes to create a
string. Example: "Aman" ,...
3 min read
Javascript HTML
Database PHP
C++ React
B.Tech / MCA
Data
DBMS
Structures
Operating
DAA
System
Computer Compiler
Network Design
Computer Discrete
Organization Mathematics
Ethical Computer
Hacking Graphics
Web Software
Technology Engineering
Cyber Automata
Security
C
C++
Programming
Java .Net
Python Programs
Control Data
System Warehouse
Preparation
Aptitude Reasoning
Verbal Interview
Ability Questions
Company
Questions