Computational Thinking
© CS-DEPT DPS MATHURA ROAD
and Programming - 1
Python - Operators
XI
Python - Operators
An operator is a special symbol in Python that is used to to perform specific
© CS-DEPT DPS MATHURA ROAD
mathematical or logical computation on values/variables. The values that the
operators work on are called operands.
For example, in the expression num1 + num2, the variable num1 and num2 are
operands and the + (plus) sign is an operator.
Python supports several kinds of operators whose categorisation will be covered in
the next slides.
2
XI
Arithmetic Operators
Python uses arithmetic operators upon different type of data depending on the
© CS-DEPT DPS MATHURA ROAD
compatibility of the operator with the literal which is it used:
SNo. Name Symbol Example Result
1. Exponentiation ** 3**2 9
(R to L) 2 ** 3 ** 2 2 ** 9 = 512
4 ** 0.5 2.0
2. Multiplication * 3*4 12
2.0 * 3 6.0
“Python” * 2 PythonPython
3
XI
Arithmetic Operators
3. Addition + 3+2 5
© CS-DEPT DPS MATHURA ROAD
5 + 2.0 7.0
“Python” + “World” PythonWorld
4. Subtraction - 3-2 1
5.0 - 2 3.0
5. Division / 6/2 3.0
5.0/2 2.5
6. Floor Division // 7//2 3
7//2.0 3.0
7. Modulo/Remainder % 5%2 1
7.0% 3 1.0
4
XI
Precedence of Arithmetic Operators
© CS-DEPT DPS MATHURA ROAD
Order of Operators Order of Operators
Precedence Precedence
1 () 5 + - (Binary)
2 ** 6 <= < > >=
3 + - (Unary) 7 == !=
4 * / % // 8 = %= /= //= -= += *= **=
5
XI
Evaluation of Arithmetic Expressions
The order of precedence of operators determines the result of any arithmetic
© CS-DEPT DPS MATHURA ROAD
expression. However the precedence of the operators may be altered by writing an
expression in parentheses. For example,
Expression Result
2 1
2+ 3 *5 2 + 15 = 17
3 1 4 2
2 - 4 * 9 + 6 / 2 2 - 36 + 3.0 = -31.0
3 2 1
2 + 4 * (9 - 6) 2 + 4 * 3 = 2 + 12 = 14
6
XI
Facts about Arithmetic Operators
All arithmetic operators are left bound, the exponential operator ** is always right
© CS-DEPT DPS MATHURA ROAD
hand i.e. consecutive occurrences of ** operators in an expression is always
evaluated starting with the 2 operands in the right.
Example:
2 1 1 2
2 ** 3 ** 2 = 2 ** 9 = 512 (2 ** 3) ** 2 = 8 ** 2 = 64
1 2 2 1
13 % 5 % 2 = 3 % 2 = 1 13 % (5 % 3) = 13 % 2 = 1
7
XI
Some more facts about Arithmetic Operators
The division operator (/) always gives the result of the division as a float value.
© CS-DEPT DPS MATHURA ROAD
The floor division operator (//) gives the nearest smaller integer result of the
division of the operands. (In case any of the operands is a float, the final result is a
float equivalent of the int result)
A modulo operator returns the remainder of the division operation of the two
operands. Numerator % Denominator is calculated as
Numerator = Quotient * Denominator + Remainder or
Remainder = Numerator - Quotient * Denominator where
Quotient = Numerator // Denominator
x % y = x - (x//y) * y
8
XI
Quick Exercise:
© CS-DEPT DPS MATHURA ROAD
7/2 = -17 % -2 =
7.0/2 = 17 % -2 =
17//4 = -9.5 % 2 =
17.0 // 4 = 9.5 % 2 =
-17 // 2 = -17 % -5=
-17 / 2 = 8 // 5 =
9
XI
Quick Exercise:
© CS-DEPT DPS MATHURA ROAD
7/2 = 3.5 -17 % -2 = -1
7.0/2 =3.5 17 % -2 = -1
17//4 =4 -9.5 % 2 = 0.5
17.0 // 4 =4.0 9.5 % 2 =1.5
-17 // 2 =-9 -17 % -5= -2
-17 / 2 =- 8.5
8 // 5 = 1
10
XI
Relational Operators
Relational operators compares two values and returns either True or False according
© CS-DEPT DPS MATHURA ROAD
to the condition.
Operator Meaning Example
== Equal to a == b
!= Not equal to a != b
< Less than a<b
> Greater than a>b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b 11
XI
Logical Operators
Logical Operators operate upon Boolean values and return a result either True or
© CS-DEPT DPS MATHURA ROAD
False
Operator Meaning Example
not Reverses the result, returns True not (a < b and b>10)
if the condition evaluates to
False, False otherwise
or Returns True if one of the a < b or b < c
condition evaluates to True
and Returns True only if both or all a < b and b < c
the conditions evaluate to True
12
XI
Logical Operators
Logical operators (unary) - not Condition not(Condition)
© CS-DEPT DPS MATHURA ROAD
True False
False True
Logical operators (binary) - and & or
Condition 1 Condition 2 and or
False False False False
False True False True
True False False True
True True True True
13
XI
Precedence of Logical Operators
© CS-DEPT DPS MATHURA ROAD
When multiple logical operators exist in an expression, their execution
sequence is decided by their precedence order, which is not > and > or. It
means that not has higher priority than and which has higher priority than or.
This precedence order can be overridden by using parentheses.
14
XI
Let us understand :
© CS-DEPT DPS MATHURA ROAD
We assume that x=3, y=4, and z=6.
Expression Evaluation
x>y and y>z or x<z False and False or True
(False and False) or True
False or True
True
x>y or y>z and x <z False or False and True
False or False
False
15
XI
Expression Evaluation
© CS-DEPT DPS MATHURA ROAD
not x>y or y<z not False or True
True or True
x=3, y=4, and z=6 True
not (x>y or y<z) not (False or True)
not True
False
not (x < y and y < z) not (True and True)
not True
False
16
XI
NOTE :
© CS-DEPT DPS MATHURA ROAD
● The AND operator checks for the second condition only when the first is
true,otherwise ignores it.
a=4 and 0 will store 0
● The OR operator will test the second condition only if the first operand
is false,otherwise it ignores it.
b=30 or 0 will store 30
17
XI
Example:
print(23==23,45==34)
© CS-DEPT DPS MATHURA ROAD
print("Hello"=="Bye","Bye"=="Bye")
print(43>23,43<23)
print("AB">"AC","AB"<"AC")
print(51<=51,51<=54)
print("51"<"533")
print(50>34 and 52<90)
print(50>34 and 52>90)
print(65>34 or 52<90,50<34 or 52>90)
print(not("Morning"=="Evening"))
print(not(25>35))
18
XI
Example:
print(23==23,45==34) True False
© CS-DEPT DPS MATHURA ROAD
print("Hello"=="Bye","Bye"=="Bye") False True
print(43>23,43<23)
True False
print("AB">"AC","AB"<"AC")
print(51<=51,51<=54) False True
print("51"<"533") True True
print(50>34 and 52<90) True
print(50>34 and 52>90) True
print(65>34 or 52<90,50<34 or 52>90)
False
print(not("Morning"=="Evening"))
True False
print(not(25>35))
True
True
19
XI
Quick Exercise:
© CS-DEPT DPS MATHURA ROAD
Q1. What will be the output of Q2. What will be the output of
following snippet : following snippet :
20
XI
Quick Exercise:
© CS-DEPT DPS MATHURA ROAD
Q1. Q2.
The value of s will be 34 True False
s=98 and 34 or not(0)
=98 and 34 or 1
=34 or 1
=34
21
XI
Assignment and Augmented Assignment
Operators
© CS-DEPT DPS MATHURA ROAD
Assignment and Augmented Assignment operators are used to assign values to
variables.
Operator Meaning Example
= Assigns value of right operand to a = 10
left operand
+= Adds value of right operand to left a+= b is similar to
and assigns to left operand a=a+b
-= subtracts value of right operand to a-= b is similar to
left and assigns to left operand a=a-b
22
XI
Assignment and Augmented Assignment
Operators
© CS-DEPT DPS MATHURA ROAD
Operator Meaning Example
*= multiplies value of right operand with a*= b is similar to
left operand and assigns to left a=a*b
operand
/= divides value of left operand to right a/= b is similar to
and assigns to left operand a=a/b
//= Performs floor division and assigns a//= b is similar to
value to left operand a=a//b
**= Performs exponential calculation and a**= b is similar to
assigns value to left operand a=a**b
23
XI
Identity Operators
Identity operators are used to determine whether the value of a variable is of a
© CS-DEPT DPS MATHURA ROAD
certain type or not. Identity operators can also be used to determine whether two
variables are referring to the same object or not. There are two identity operators.
Operator Meaning Example
is Evaluates to True if the variables on either
side of the operator point to the same
memory location and False otherwise.
var1 is var2 results to True if id(var1) =
id(var2)
is not Evaluates to False if the variables on either
side of the operator point to the same
memory location and True otherwise.
var1 is not var2 results to True if id(var1) !=
id(var2)
24
XI
Special Cases
If the expression a is b is True, it means that a==b will also be True. But it is not
© CS-DEPT DPS MATHURA ROAD
always true the other way round. That means in some cases if a==b, but a is b is
False.
Situations where this is violated (if a == b, a is b is False)
1. Input of strings from the console (These strings are bound to fresh memory
even if they have value identical to some other existing string in memory)
25
XI
Special Cases
2. Writing integer literals with many digits (very big integers)
© CS-DEPT DPS MATHURA ROAD
3. Writing floating point literals and complex literals
Big Integers Complex Numbers
26
XI
Membership Operators
Membership Operators are used to check if a value is a member of a given sequence
© CS-DEPT DPS MATHURA ROAD
or not.
Operator Meaning Example
in Returns True if the a = [1,2,3]
variable/value is found in the 2 in a returns True
specified sequence and False ‘1’ in a returns False
otherwise.
not in Returns True if the a = [1,2,3]
variable/value is not found in 10 not in a returns
the specified sequence and True
False otherwise. 1 not in a returns False
27
XI
Precedence of Operators
Parenthesis can be used to override the precedence. The expression within () is
© CS-DEPT DPS MATHURA ROAD
evaluated first. For operators with equal precedence, they are evaluated left to right
Order of Operators Order of Operators
Precedence Precedence
1 ** 6 == , !=
2 +,- (Unary) 7 =,*=,/=,%=,//=,+=,-=,**=
3 *, /, %, // 8 is, is not
4 +, - (Binary) 9 in, not in
5 <=,<,>,>= 10 not, or, and
28
XI
Quick Exercise:
What will be the output :
© CS-DEPT DPS MATHURA ROAD
1. print((4**2)*3**3)
2. a=30
b=40
c=a*b//3
print(c)
3. x=2 *((3*12)-8/10)
print(“Value of x :”,x)
29
XI
© CS-DEPT DPS MATHURA ROAD
30
Assignments in Python
XI
Operator Chaining
Chaining of relational operators is allowed in Python.
© CS-DEPT DPS MATHURA ROAD
For example: x<y<=z is equivalent to x < y and y <=z
1<2<3 1 < 2 and 2 < 3 returns
True
11 < 13 and 13 > 12 returns
11 < 13 > 12 True
31
XI
Operator Associativity
Associativity is the order in which an expression (having multiple operators of the
© CS-DEPT DPS MATHURA ROAD
same precedence) is evaluated. Almost all the operators have left-to-right
associativity except exponentiation (**), which has right to left associativity
Expression Evaluation
7 * 8 / 5 //2 56/5//2
11.2//2
5.0
3 ** 3 ** 2 3 ** 9
19863
32
XI
Quick Exercise
What will be the output :
© CS-DEPT DPS MATHURA ROAD
1. x = 36 / 4 * (3 + 2) * 4 + 2
print(x)
2. var = "James" * 2 * 3
print(var)
3. var1 = 1
var2 = 2
var3 = "3"
print(var1 + var2 + var3)
33
XI
Quick Exercise:
What will be the output :
© CS-DEPT DPS MATHURA ROAD
5. X=10 6. first = 2
X= X+10
second =3
X= X-5
third = first * second
print (X)
print (first, second, third)
X, Y = X-2, 22
first= first + second + third
print (X, Y)
third = second * first
print (first, second, third)
34
XI
Quick Exercise:
What will be the output :
© CS-DEPT DPS MATHURA ROAD
7. x= 40 8. x, y =20, 60
y=x+1 y, x, y=x, y-10, x+10
x,y=20, y +x print (x, y)
print (x, y)
35
XI
Quick Exercise:
What will be the output :
© CS-DEPT DPS MATHURA ROAD
9. a , b, c=10, 20, 30 10. a, b, c = 2, 3, 4
p , q, r= c-5, a+3, b-4 a , b, c = a*a, a*b, a*c
print (‘a, b, c:’ , a, b, c, end = ‘ ’)
print (a, b, c)
print (‘p, q, r:’ , p, q, r)
36
XI
Type Conversion
© CS-DEPT DPS MATHURA ROAD
The process of converting the value of one data type (integer, string, float,
etc.) to another data type is called type conversion. Python has two types of
type conversion.
● Implicit Type Conversion
● Explicit Type Conversion
37
XI
Implicit Type Conversion (Type Promotion)
It refers to the data type conversion performed by the interpreter without the user’s
© CS-DEPT DPS MATHURA ROAD
intervention. It is generally applied whenever an expression having operands
belonging to mixed data types is to be evaluated. In a mixed arithmetic expression,
Python converts all operands up to the type of the largest operand (type promotion,
also known as coercion)
If both operands of an expression are standard numeric types, the following
coercions are applied :
38
XI
Implicit Type Conversion - Rules
© CS-DEPT DPS MATHURA ROAD
If either operand is a complex number, the  >>4+(2+2j)
other is converted to a complex
>>6+2j
if either argument is a floating point >>7+8.3
number, the other is converted to floating
point >>15.3
No conversion takes place if both operands >> 6+5
are integers.
>> 11
39
XI
Explicit Type Conversion ( Type Casting)
An explicit type conversion is user defined conversion that forces an expression to be
© CS-DEPT DPS MATHURA ROAD
of a specific data type.
It is performed by <datatype>(expression) function.
40
XI
Some data type conversion functions:
Function Converted from Converted to Example
© CS-DEPT DPS MATHURA ROAD
int() Any number integer int(7.8) will give 7
convertible type int(‘12’) will give 12
float() Any number floating point float(7) will give 7.0
convertible type number float(‘12’) will give 12.0
complex() numbers Complex complex(7) will give 7+0j
number complex(1,2) will give 1+2j
41
XI
Some data type conversion functions:
Function Converted from Converted to Example
© CS-DEPT DPS MATHURA ROAD
str() Numbers, string str(3) will give ‘3’
str(1.23) will give ‘1.23’
boolean str(1+2j) will give ‘(1+2j)’
str(True) will give ‘True’
bool() Any type boolean bool(0) will give False
bool(1) will give True
bool(‘’) will give False
bool(‘hello’) will give True
bool(45) will return True
42
XI
© CS-DEPT DPS MATHURA ROAD
Thank you!
Department of Computer Science
Mathura Road
43
XI