Thank you for printing our content at [Link].
Please check back soon for new
contents.
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Try Programiz PRO today. python)
Search... Try Programiz PRO ([Link]
(/)
[Link]
Python Operators
Operators are special symbols that perform operations on variables (/python-
programming/variables-constants-literals) and values. For example,
print(5 + 6) # 11
Run Code (/python-programming/online-compiler)
Here, + is an operator that adds two numbers: 5 and 6.
Types of Python Operators
Here's a list of different types of Python operators that we will learn in this
tutorial.
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. Special Operators
Thank you for printing our content at [Link]. Please check back soon for new
contents.
1. Python Arithmetic Operators
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Try Programiz PRO today. python)
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, Search... Try Programiz
multiplication, etc. ForPRO ([Link]
example,
(/)
[Link]
sub = 10 - 5 # 5
Here, - is an arithmetic operator that subtracts two values or variables.
Operator Operation Example
+ Addition 5 + 2 = 7
- Subtraction 4 - 2 = 2
* Multiplication 2 * 3 = 6
/ Division 4 / 2 = 2
// Floor Division 10 // 3 = 3
% Modulo 5 % 2 = 1
** Power 4 ** 2 = 16
Example 1: Arithmetic Operators in Python
Thank you for printing our content at [Link]. Please check back soon for new
contents.
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
a = 7
Try Programiz PRO today. python)
b = 2
Search... Try Programiz PRO ([Link]
# (/)
addition
print ('Sum: ', a + b) [Link]
# subtraction
print ('Subtraction: ', a - b)
# multiplication
print ('Multiplication: ', a * b)
# division
print ('Division: ', a / b)
# floor division
print ('Floor Division: ', a // b)
# modulo
print ('Modulo: ', a % b)
# a to the power b
print ('Power: ', a ** b)
Run Code (/python-programming/online-compiler)
Output
Sum: 9
Subtraction: 5
Multiplication: 14
Division: 3.5
Floor Division: 3
Modulo: 1
Power: 49
In the above example, we have used multiple arithmetic operators,
+ to add a and b
- to subtract b from a
* to multiply a and b
/ to
Thank you fordivide bycontent
printingaour b at [Link]. Please check back soon for new
contents.
// to floor divide a by b
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
% to get
Try Programiz PRO the remainder
today. python)
** to getSearch... Try Programiz
a to the power b PRO ([Link]
(/)
[Link]
2. Python Assignment Operators
Assignment operators are used to assign values to variables. For example,
# assign 5 to x
x = 5
Here, = is an assignment operator that assigns 5 to x .
Here's a list of different assignment operators available in Python.
Operator Name Example
= Assignment Operator a = 7
+= Addition Assignment a += 1 # a = a + 1
-= Subtraction Assignment a -= 3 # a = a - 3
*= Multiplication Assignment a *= 4 # a = a * 4
Thank you
/= for printing our content
Division at [Link]
Assignment /= 3 check
# a = back
a / 3soon for new
contents.
%= Remainder Assignment a %= 10 # a = a % 10
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Try Programiz PRO today. python)
**= Exponent Assignment a **= 10 # a = a ** 10
Search... Try Programiz PRO ([Link]
(/)
[Link]
Example 2: Assignment Operators
# assign 10 to a
a = 10
# assign 5 to b
b = 5
# assign the sum of a and b to a
a += b # a = a + b
print(a)
# Output: 15
Run Code (/python-programming/online-compiler)
Here, we have used the += operator to assign the sum of a and b to a .
Similarly, we can use any other assignment operators as per our needs.
3. Python Comparison Operators
Comparison operators compare two values/variables and return a boolean
result: True or False . For example,
a = 5
b = 2
print (a > b) # True
Thank you for printing our content at [Link]. Please check back soon for new
Run Code (/python-programming/online-compiler)
contents.
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
TryHere,
Programiz
thePRO comparison operator is used to compare whether a python)
> today. is greater than
b or not. Search... Try Programiz PRO ([Link]
(/)
[Link]
Operator Meaning Example
== Is Equal To 3 == 5 gives us False
!= Not Equal To 3 != 5 gives us True
> Greater Than 3 > 5 gives us False
< Less Than 3 < 5 gives us True
>= Greater Than or Equal To 3 >= 5 give us False
<= Less Than or Equal To 3 <= 5 gives us True
Example 3: Comparison Operators
Thank you for printing our content at [Link]. Please check back soon for new
contents.
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
a = 5 PRO today.
Try Programiz python)
b = 2 Search... Try Programiz PRO ([Link]
(/)
# equal to operator [Link]
print('a == b =', a == b)
# not equal to operator
print('a != b =', a != b)
# greater than operator
print('a > b =', a > b)
# less than operator
print('a < b =', a < b)
# greater than or equal to operator
print('a >= b =', a >= b)
# less than or equal to operator
print('a <= b =', a <= b)
Run Code (/python-programming/online-compiler)
Output
a == b = False
a != b = True
a > b = True
a < b = False
a >= b = True
a <= b = False
Note: Comparison operators are used in decision-making and loops
(/python-programming/looping-technique). We'll discuss more of the
comparison operator and decision-making in later tutorials.
4. Python Logical Operators
Thank you for printing our content at [Link]. Please check back soon for new
contents.
Logical operators are used to check whether an expression is True or False .
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
TryThey are PRO
Programiz used in decision-making. For example,
today. python)
Search... Try Programiz PRO ([Link]
(/)
a = 5 [Link]
b = 6
print((a > 2) and (b >= 6)) # True
Run Code (/python-programming/online-compiler)
Here, and is the logical operator AND. Since both a > 2 and b >= 6 are True ,
the result is True .
Operator Example Meaning
Logical AND:
and a and b
True only if both the operands are True
Logical OR:
or a or b
True if at least one of the operands is True
Logical NOT:
not not a
True if the operand is False and vice-versa.
Example 4: Logical Operators
# logical AND
print(True and True) # True
print(True and False) # False
# logical OR
print(True or False) # True
# logical NOT
print(not True) # False
Thank you for printing our content at [Link]. Please check back soon for new
Run Code (/python-programming/online-compiler)
contents.
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Try Programiz PRO today. python)
Note: Here is the truth table (/python-programming/keyword-
Search... Try Programiz PRO ([Link]
list#and_or_not)
(/) for these logical operators.
[Link]
5. Python Bitwise operators
Bitwise operators act on operands as if they were strings of binary digits. They
operate bit by bit, hence the name.
For example, 2 is 10 in binary, and 7 is 111 .
In the table below: Let x = 10 ( 0000 1010 in binary) and y =4( 0000 0100 in
binary)
Operator Meaning Example
& Bitwise AND x & y = 0 ( 0000 0000 )
| Bitwise OR x | y = 14 ( 0000 1110 )
~ Bitwise NOT ~x = -11 ( 1111 0101 )
^ Bitwise XOR x ^ y = 14 ( 0000 1110 )
>> Bitwise right shift x >> 2 = 2 ( 0000 0010 )
<< Bitwise left shift x 0010 1000)
6. Python Special operators
Python language offers some special types of operators like the identity
operator and the membership operator. They are described below with
examples.
Identity
Thank operators
you for printing our content at [Link]. Please check back soon for new
contents.
In Python, is and is not are used to check if two values are located at the
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Trysame memory
Programiz location.
PRO today. python)
Search...
It's important Try Programiz
to note that having two ([Link]
PRO variables with equal values doesn't
(/)
necessarily mean they are identical.
[Link]
Operator Meaning Example
True if the operands are identical (refer to the
is x is True
same object)
True if the operands are not identical (do not x is not
is not
refer to the same object) True
Example 4: Identity operators in Python
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
print(x1 is not y1) # prints False
print(x2 is y2) # prints True
print(x3 is y3) # prints False
Run Code (/python-programming/online-compiler)
Here, we see that x1 and y1 are integers of the same values, so they are equal
as well as identical. The same is the case with x2 and y2 (strings).
But x3 and y3 are lists. They are equal but not identical. It is because the
interpreter locates them separately in memory, although they are equal.
Membership
Thank operators
you for printing our content at [Link]. Please check back soon for new
contents.
In Python, in and not in are the membership operators. They are used to test
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Trywhether
Programiz a value
PRO or variable is found in a sequence (string (/python-
today. python)
programming/string), list (/python-programming/list), tuple (/python-
Search... Try Programiz PRO ([Link]
(/)
programming/tuple), set (/python-programming/set) and dictionary (/python-
[Link]
programming/dictionary)).
In a dictionary, we can only test for the presence of a key, not the value.
Operator Meaning Example
in True if value/variable is found in the sequence 5 in x
True if value/variable is not found in the 5 not in
not in
sequence x
Example 5: Membership operators in Python
message = 'Hello world'
dict1 = {1:'a', 2:'b'}
# check if 'H' is present in message string
print('H' in message) # prints True
# check if 'hello' is present in message string
print('hello' not in message) # prints True
# check if '1' key is present in dict1
print(1 in dict1) # prints True
# check if 'a' key is present in dict1
print('a' in dict1) # prints False
Run Code (/python-programming/online-compiler)
Output
Thank you for printing our content at [Link]. Please check back soon for new
True
contents.
True
True
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
False
Try Programiz PRO today. python)
Search... Try Programiz PRO ([Link]
Here,
(/) 'H' is in message , but 'hello' is not present in message (remember,
Python is case-sensitive). [Link]
Similarly, 1 is key, and 'a' is the value in dictionary dict1 . Hence, 'a' in y
returns False .
Also Read:
Precedence and Associativity of operators in Python (/python-
programming/precedence-associativity)
Python Operator Overloading (/python-programming/operator-overloading)
Before we wrap up, let’s put your knowledge of Python operators to the test! Can
you solve the following challenge?
Challenge:
Write a function to split the restaurant bill among friends.
Take the subtotal of the bill and the number of friends as inputs.
Calculate the total bill by adding 20% tax to the subtotal and then divide it by the
number of friends.
Return
Thank you the amount
for printing each friend
our content has to pay, rounded offPlease
at [Link]. to two check
decimal places.
back soon for new
contents.
1 solving
Learn to code def bill_split(amount, friends)
problems and writing code with our hands-on Python course. ([Link]
2
Try Programiz PRO today. python)
Search... Try Programiz PRO ([Link]
(/)
[Link]
Check Code
Video: Operators in Python
Previous Tutorial: Next Tutorial:
Thank you Python Basic
for printing our content(/python-
at [Link] (/python-
Please check back soon for new
contents. programming/input- programming/if-
Input and if...else
output-import) elif-else)
Output Statement
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Try Programiz PRO today. python)
Search... Try Programiz PRO ([Link]
(/)
Share on: [Link]
([Link] ([Link]
u=[Link] text=Check%20this%20amazing%2
programming/operators) programming/operators)
Did you find this article helpful?
Our premium learning platform, created with over a decade of
experience.
Try Programiz PRO
([Link]
Related Tutorials
Python Tutorial Python Tutorial
Precedence and Associativity of Python Operator Overloading
Operators in Python
(/python-programming/precedence- (/python-programming/operator-
associativity) overloading)
Python Tutorial Python Tutorial
Python if...else Statement Python 3 Tutorial
Thank you for printing our content at [Link]. Please check back soon for new
(/python-programming/if-elif-else) (/python-programming/tutorial)
contents.
Learn to code solving problems and writing code with our hands-on Python course. ([Link]
Try Programiz PRO today. python)
Search... Try Programiz PRO ([Link]
(/)
[Link]