Python Operators
Python Operators
PRECEDENCE:
• P – Parentheses
• E – Exponentiation
• M – Multiplication (Multiplication and division have the same
precedence)
• D – Division
• A – Addition (Addition and subtraction have the same precedence)
• S – Subtraction
The modulus operator helps us extract the last digit/s of a number. For
example:
• x % 10 -> yields the last digit
• x % 100 -> yield last two digits
Example: Arithmetic operators in Python
• Python3
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Power
p = a ** b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Output
13
5
36
2.25
2
1
6561
Note: Refer to Differences between / and // for some interesting facts about
these two operators.
Comparison Operators
Comparison of Relational operators compares the values. It either
returns True or False according to the condition.
Operator Description Syntax
> Greater than: True if the left operand is greater than the right x>y
< Less than: True if the left operand is less than the right x<y
is x is the same as y x is y
x is not
is not x is not the same as y y
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Output
False
True
False
True
False
True
Logical Operators
Logical operators perform Logical AND, Logical OR, and Logical
NOT operations. It is used to combine conditional statements.
Operator Description Syntax
and Logical AND: True if both the operands are true x and y
• Python3
# Examples of Logical Operator
a = True
b = False
# Print a or b is True
print(a or b)
Output
False
True
False
Bitwise Operators
Bitwise operators act on bits and perform the bit-by-bit operations. These are
used to operate on binary numbers.
Operator Description Syntax
| Bitwise OR x|y
~ Bitwise NOT ~x
• Python3
Output
0
14
-11
14
2
40
Assignment Operators
Assignment operators are used to assign values to the variables.
Operator Description Syntax
• Python3
Output
10
20
10
100
102400
Identity Operators
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: Identity Operator
• Python3
a = 10
b = 20
c = a
print(a is not b)
print(a is c)
Output
True
True
Membership Operators
in and not in are the membership operators; used to test whether a value or
variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence
Example: Membership Operator
• Python3
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
• Python3
Output
610
Hello! Welcome.
Operator Associativity
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: Operator Associativity
• Python3
# Left-right associativity
# 100 / 10 * 10 is calculated as
# (100 / 10) * 10 and not
# as 100 / (10 * 10)
print(100 / 10 * 10)
# Left-right associativity
# 5 - 2 + 3 is calculated as
# (5 - 2) + 3 and not
# as 5 - (2 + 3)
print(5 - 2 + 3)
# left-right associativity
print(5 - (2 + 3))
# right-left associativity
# 2 ** 3 ** 2 is calculated as
# 2 ** (3 ** 2) and not
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)
Output
100.0
6
0
512
Output:
2
-3
The first output is fine, but the second one may be surprised if we are
coming Java/C++ world. In Python, the “//” operator works as a floor division
for integer and float arguments. However, the division operator ‘/’ returns
always a float value.
Note: The “//” operator is used to return the closest integer value which is
less than or equal to a specified expression or value. So from the above
code, 5//2 returns 2. You know that 5/2 is 2.5, and the closest integer which
is less than or equal is 2[5//2].( it is inverse to the normal maths, in normal
maths the value is 3).
Example
• Python3
Output
2.5
-2.5
The real floor division operator is “//”. It returns the floor value for both
integer and floating-point arguments.
• Python3
Output
2
-3
2.0
-3.0
See this for example.
Ternary operators
Ternary operators are 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]
• Simple Method to use ternary operator:
• Python
print(min)
Output:
10
• Direct Method by using tuples, Dictionary, and lambda
• Python
Output:
10
10
10
• Ternary operator can be written as nested if-else:
• Python
• Python
if a != b:
if a > b:
print("a is greater than b")
else:
print("b is greater than a")
else:
print("Both a and b are equal")
Output:
b is greater than a
•
To use print function in ternary operator be like:-
Example: Find the Larger number among 2 using ternary operator in python3
• Python3
a=5
b=7
Output:
7 is Greater
Important Points:
• First the given condition is evaluated (a < b), then either a or b is
returned based on the Boolean value returned by the condition
• Order of the arguments in the operator is different from other
languages like C/C++ (See C/C++ ternary operators).
• Conditional expressions have the lowest priority amongst all Python
operations.
Method used prior to 2.5 when the ternary operator was not present
In an expression like the one given below, the interpreter checks for the
expression if this is true then on_true is evaluated, else the on_false is
evaluated.
Syntax :
'''When condition becomes true, expression [on_false]
is not executed and value of "True and [on_true]"
is returned. Else value of "False or [on_false]"
is returned.
Note that "True and x" is equal to x.
And "False or x" is equal to x. '''
[expression] and [on_true] or [on_false]
Example :
• Python
print(min)
Output:
10
Note : The only drawback of this method is that on_true must not be zero
or False. If this happens on_false will be evaluated always. The reason for
that is if the expression is true, the interpreter will check for the on_true, if
that will be zero or false, that will force the interpreter to check for on_false to
give the final result of the whole expression.