Class 11 - Presentation # 3 - Operators
Class 11 - Presentation # 3 - Operators
XI
Python - Operators
An operator is a special symbol in Python that is used to to perform specific
For example, in the expression num1 + num2, the variable num1 and num2 are
operands and the + (plus) sign is an operator.
2
XI
Arithmetic Operators
Python uses arithmetic operators upon different type of data depending on the
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
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
3 + - (Unary) 7 == !=
4 * / % // 8 = %= /= //= -= += *= **=
5
XI
Evaluation of Arithmetic Expressions
The order of precedence of operators determines the result of any arithmetic
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
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.
8
XI
Quick Exercise:
7.0/2 = 17 % -2 =
17//4 = -9.5 % 2 =
17.0 // 4 = 9.5 % 2 =
-17 / 2 = 8 // 5 =
9
XI
Quick Exercise:
10
XI
Relational Operators
Relational operators compares two values and returns either True or False according
not Reverses the result, returns True not (a < b and b>10)
if the condition evaluates to
False, False otherwise
False True
14
XI
Let us understand :
Expression Evaluation
15
XI
Expression Evaluation
● The OR operator will test the second condition only if the first operand
is false,otherwise it ignores it.
17
XI
Example:
print(23==23,45==34)
18
XI
Example:
print(23==23,45==34) True False
20
XI
Quick Exercise:
=98 and 34 or 1
=34 or 1
=34
21
XI
Assignment and Augmented Assignment
Operators
22
XI
Assignment and Augmented Assignment
Operators
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)
26
XI
Membership Operators
Membership Operators are used to check if a value is a member of a given sequence
27
XI
Precedence of Operators
Parenthesis can be used to override the precedence. The expression within () is
3 *, /, %, // 8 is, is not
29
XI
© CS-DEPT DPS MATHURA ROAD
30
Assignments in Python
XI
Operator Chaining
Chaining of relational operators is allowed in Python.
31
XI
Operator Associativity
Associativity is the order in which an expression (having multiple operators of the
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 :
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 :
34
XI
Quick Exercise:
What will be the output :
35
XI
Quick Exercise:
What will be the output :
36
XI
Type Conversion
37
XI
Implicit Type Conversion (Type Promotion)
It refers to the data type conversion performed by the interpreter without the user’s
38
XI
Implicit Type Conversion - Rules
39
XI
Explicit Type Conversion ( Type Casting)
An explicit type conversion is user defined conversion that forces an expression to be
40
XI
Some data type conversion functions:
41
XI
Some data type conversion functions:
42
XI
© CS-DEPT DPS MATHURA ROAD
Thank you!
43
XI