0% found this document useful (0 votes)
1 views19 pages

Module 3 _ Python Operators & Expression

The document provides an overview of binary operations, conditional statements, and the importance of indentation in Python programming. It explains operators such as addition, subtraction, multiplication, division, and their specific behaviors, including integer division and remainder. Additionally, it discusses the nuances of using floats, comparisons, and best practices for evaluating expressions in Python.

Uploaded by

eaabiolaadebayo
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)
1 views19 pages

Module 3 _ Python Operators & Expression

The document provides an overview of binary operations, conditional statements, and the importance of indentation in Python programming. It explains operators such as addition, subtraction, multiplication, division, and their specific behaviors, including integer division and remainder. Additionally, it discusses the nuances of using floats, comparisons, and best practices for evaluating expressions in Python.

Uploaded by

eaabiolaadebayo
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/ 19

Operators and Expressions

1
Binary Operations
Op Meaning Example Remarks

+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplication 9*2 is 18
9.1*2.0 is 18.2
/ Division 9/2 is 4.25 In Python3
9.1/2.0 is 4.55 Real div.
// Integer Division 9//2 is 4
% Remainder 9%2 is 1

08/12/2025 Programming 2
The // operator
• Also referred to as “integer division”
• Result is a whole integer (floor of real
division)
– But the type need not be int
– the integral part of the real division
– rounded towards minus infinity
• Examples
9//4 is 2 (-1)//2 is -1 (-1)//(-2) is 0
1//2 is 0 1//(-2) is -1 9//4.5 is 2.0
08/12/2025 Programming 3
The % operator
• The remainder operator % returns the
remainder of the result of dividing its
first operand by its second.
9%4 is 1 (-1)%2 is 1 (-1)//(-2) is 0
9%4.5 is 0.0 1%(-2) is 1 1%0.6 is 0.4

Ideally: x == (x//y)*y + x %y
08/12/2025 Programming 4
Conditional Statements
• In daily routine
– If it is very hot, I will skip
exercise.
– If there is a quiz tomorrow, I will
first study and then sleep.
Otherwise I will sleep now.
– If I have to buy coffee, I will
go left. Else I will go
straight.
Aug 12, 2025 Programming 5
if-else statement
• Compare two integers and print the min.

if x < y: 1. Check if x is less


print (x) than y.
2. If so, print x
else: 3. Otherwise, print y.
print (y)
print (‘is the minimum’)

Aug 12, 2025 Programming 6


Indentation
• Indentation is important in Python
– grouping of statement (block of statements)
– no explicit brackets, e.g. { }, to group statements
x,y = 6,10 Run
x the program
y

if x < y: 6 10
print (x)
else:
print (y) ed Output
i p p
sk the min’)
print (‘is 6
Aug 12, 2025 Programming 7
if statement (no else!)
• General form of the if statement
e
if boolean-expr : rt u

fals
e
S1
S1
S2
• Execution of if statement S2
– First the expression is evaluated.
– If it evaluates to a true value, then S1 is
executed and then control moves to the S2.
– If expression evaluates to false, then control
moves to the S2 directly.
Aug 12, 2025 Programming 8
if-else statement
• General form of the if-else statement
if boolean-expr : rt u
e

fa
S1

els
else: S1 S2
S2
S3 S3
• Execution of if-else statement
– First the expression is evaluated.
– If it evaluates to a true value, then S1 is executed and
then control moves to S3.
– If expression evaluates to false, then S2 is executed
and then control moves to S3.
– S1/S2 can be blocks of statements!
Aug 12, 2025 Programming 9
Nested if, if-else

if a <= b:
if a <= c:

else:

else:
if b <= c) :

else:

Aug 12, 2025 Programming 10
Elif
• A special kind of nesting is the chain of if-
else-if-else-… statements
• Can be written elegantly using if-elif-..-else
if cond1: if cond1:
s1 s1
else: elif cond2:
if cond2: s2
s2 elif cond3:
else: s3
if cond3: elif …
s3 else
else: last-block-of-stmt

Aug 12, 2025 Programming 11
Summary of if, if-else
• if-else, nested if's, elif.
• Multiple ways to solve a problem
–issues of readability,
maintainability
–and efficiency

Aug 12, 2025 Programming 12


Class Quiz
• What is the value of expression:
(5<2) and (3/0 > 1)

a) Run time crash/error

b) I don’t know / I don’t care

c) False
The correct answer is
False
d) True

Aug 12, 2025 Programming 13


Short-circuit Evaluation
• Do not evaluate the second operand of binary
short-circuit logical operator if the result can be
deduced from the first operand
– Also applies to nested logical operators

true false false true

not( (2>5) and (3/0 > 1) ) or (4/0 < 2)


Evaluates to true
Aug 12, 2025 Programming 14
3 Factors for Expr Evaluation
• Precedence
– Applied to two different class of operators
– + and *, - and *, and and or, …
• Associativity
– Applied to operators of same class
– * and *, + and -, * and /, …
• Order
– Precedence and associativity identify the operands for
each operator
– Not which operand is evaluated first
– Python evaluates expressions from left to right
– While evaluating an assignment, the right-hand side is
Aug 12, 2025 Programming 15
evaluated before the left-hand side.
Class Quiz
• What is the output of the following program:
y = 0.1*3
if y != 0.3:
print ('Launch a Missile')
else:
print ("Let's have peace")
Launch a Missile

Aug 12, 2025 Programming 16


Caution about Using Floats
• Representation of real numbers in a computer
can not be exact
– Computers have limited memory to store data
– Between any two distinct real numbers, there are
infinitely many real numbers.
• On a typical machine running Python, there are
53 bits of precision available for a Python float

Aug 12, 2025 Programming 17


Caution about Using Floats
• The value stored internally for the decimal
number 0.1 is the binary fraction
0.00011001100110011001100110011001100110011001100110011010

• Equivalent to decimal value


0.1000000000000000055511151231257827021181583404541015625

• Approximation is similar to decimal


approximation 1/3 = 0.333333333...
• No matter how many digits you use, you have
an approximation
Aug 12, 2025 Programming 18
Comparing Floats
• Because of the approximations, comparison of
floats is not exact.
• Solution?
• Instead of
x == y
use
abs(x-y) <= epsilon
where epsilon is a suitably chosen small value

Aug 12, 2025 Programming 19

You might also like