Session 2, CH 3&4-Operators, Expressions & Decision Statements
Session 2, CH 3&4-Operators, Expressions & Decision Statements
Source:
Kamthane, A. N. & Kamthane, A.A., Programming and Problem Solving with Python, Tata McGraw-Hill
Education India. & from various sources
Introduction to Expression and Operators
An expression in Python is a block of code that produces a result or value upon
evaluation.
Operators are symbols help the user or command computer to perform mathematical
operations.
Example:
In the expression 6 + 3 the ‘+’ act as operator. Where operator requires operands to
perform operations. Therefore 6 and 3 act as operands.
Arithmetic Operators
There are two types of Arithmetic Operators
a) Unary Operator
b) Binary Operator
Unary Operators:
Unary arithmetic operators perform mathematical operations on one operand only. The ‘+’ and ‘-’ are two unary operators.
Example:
>>> x = -5 #Negates the value of X
>>> x
-5
Binary Operators
Binary operators are operators which require two operands .
Operator is written in between two operands.
Example:
>>> 3 + 10
>>>13
>>> 10 – 7
>>> 3
Division(/) Operators and Floor Division Operators
Division Operator
If the division operator is applied in between two operands, it returns the result as the arithmetic quotient of the
operands.
Example:
>>> 4/2
>>> 2.0
>>> 2/3
0.6666666666666666
Floor Division (//) Operator
If the floor division operator is applied in between two operands, it returns the result as the arithmetic
quotient of the operands .
Python floor division (//) operator when applied on two int operands, Python returns an int result.
Example:
>>> 9//4
2
Modulo (%) Operator
When the second number divides the first number, the modulo operator returns the remainder.
Example:
>>> 21 % 9
3
Note:
X%Y is equivalent to X – Y * (x//Y)
The Exponent ** Operator
The ‘**’ exponent operator is used to calculate the power or exponent of a number
Example
>>> 5**4
625
Operator Precedence and Associativity
The operator precedence determines the order in which the python interpreter evaluates the operators in
an expression.
a) Left to Right
Example:
4+6–3+2 = 9
Note: When the operators of same priority are found in the expression, precedence is given to the left most
operator.
b) Right to Left
X = Y = Z = Value
Note: When the operators of same priority are found in the expression, precedence is given to the right
most operator.
Associativity Continued….
Example:
Z = 4 * 6 + 8 // 2
= 28
In the above expression * is evaluated first, even though * and // have the same priorities. The operator *
occurs before // and hence the evaluation starts from left to right.
Example:
X=X+1
X+=1
The if Statements
Nested if statements
Details of if Statement
The keyword if begins the if statement
The condition is a Boolean expression that determines whether or not the body of if block will be
executed.
A colon(:) must always be followed by the condition.
The block may contain one or more statements. The statement or statements are executed if and
only if the condition within the if statement is true.
Flow Chart of if statement
Example of if Statement
Write a Program to print You are eligible to vote if age is greater than or equal to 100.
Solution:
The if-else Statement
The if-else statements take care of true and false conditions. It has two blocks i.e. one block is for if and
other block is of else.
Block following to if is executed( when the condition is true) and else block is executed when the
condition is false.
Flow Chart of if-else statement
Program on if - else statement
Write a program to display message You can Vote if age is greater than 18 or display message You are not
eligible for voting.
Nested if statements
The if statement inside another if statement then it is called nested if statements.
Syntax of nested if statement is as follows
The if-elif-else statement
Syntax of if-elif-else is as follows
In this kind of statements, number of conditions i.e. boolean expressions are checked from top to
bottom.
When the true condition is found, the statement associated with it is executed and the rest of
conditional statements are skipped.
If none of the conditions are true, then the last else statement is executed.
If all other conditions are false and if the final else is not present then no action place
Program on if-elif-else statement
Write a program to prompt the user to enter the day of week. If the entered day of week is between 1 and
7 then display respective name of the day.
Solution:
Conditional Expressions
To improve the performance of simple if-else statement, python has provides above concept named
conditional expression. Thus above solution by using conditional expression is as follows
x*x if x % 2 == 0 else x*x*x
Conclusion……
Various binary operators such as addition (+), subtraction (-), multiplication (*), integer division, floor
division, modulus operators (%), and exponent (**) are covered.
Python Supports various decision statements such as if, if-else, and Multi-way if-elif-else statements.
Python does not have a ternary operators. But instead it has a Conditional Expression.