0% found this document useful (0 votes)
15 views23 pages

Session 2, CH 3&4-Operators, Expressions & Decision Statements

This document discusses operators, expressions, and decision statements in Python. It defines expressions as blocks of code that produce values when evaluated. Operators like + and - are used to perform mathematical operations on operands. Decision statements like if, if-else, and if-elif-else are used to conditionally execute code blocks depending on boolean expressions. The document also covers operator precedence, associativity, compound assignment operators, nested if statements, and conditional expressions.

Uploaded by

rahul singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views23 pages

Session 2, CH 3&4-Operators, Expressions & Decision Statements

This document discusses operators, expressions, and decision statements in Python. It defines expressions as blocks of code that produce values when evaluated. Operators like + and - are used to perform mathematical operations on operands. Decision statements like if, if-else, and if-elif-else are used to conditionally execute code blocks depending on boolean expressions. The document also covers operator precedence, associativity, compound assignment operators, nested if statements, and conditional expressions.

Uploaded by

rahul singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

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.

 An expression can be broken down into operators and operands.

 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

To compute xY (X raised to Y), the expression is written as X**Y.

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.

 Where as associativity property decides which operation to be performed first.

Associativity is of two types.

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.

Associativity table for Arithmetic Operators


Bitwise Operators

 Python has six bitwise operators.


 Python support bitwise operator for bit wise manipulation.
 It allows the programmer to access and manipulate individual bits within a piece of data.
 Following are the six bitwise operators supported by
Compound Assignment Operator
The operators +, *, //, /, % and ** are used with assignment operator (=) to form compound or augmented
assignment operator.

Example:
X=X+1

But python allows programmer to combine assignment and addition operator.

Thus the above statement X = X + 1 can also be written as

X+=1

Various other Compound Assignment operators are as follows

*= , /= //=, %=, **=, -= , and !=


Decision Statements
 The list of decision statements supported by python is as follows:

 The if Statements

 The if-else Statements

 Nested if statements

 Multi-way if-elif-else Statements


The if Statement
The if statement executes the statements following the if statement if the condition is true.
Syntax of if Statement is as follows

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.

Syntax of if – else block is as follows

 The if block may contain one or more than one statements.

 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

The general form of conditional expression is as follows:


Expression1 if condition else Expression2
Example
Consider the following piece of code
if x%2==0:
x=x+1
else:
x=x+2

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.

 Operator precedence and Associativity is explained in brief.

 Programmer has also came to know various augmented operators.

 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.

You might also like