0% found this document useful (0 votes)
10 views16 pages

L4 Conditional Loops

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

L4 Conditional Loops

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

ED5340 - Data Science: Theory at h y

and Practise a p
gan
th u
M u
a n
th
L4 - Conditional statements
m a and
n a Loops
Ra

Ramanathan Muthuganapathy (https://ed.iitm.ac.in/~raman)


Course web page: https://ed.iitm.ac.in/~raman/datascience.html
Moodle page: Available at https://courses.iitm.ac.in/
Decision control (if - elif - else)
colon and indentation

if condition: if condition: if condition1:


st1 st1 h y statements
pat
a
st2 st2 an elif condition2:
th ug
Mu
else: an statements
at h
a n
st1
a m elif condition3:
R
st2 statements
else:
statements

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Relational operators
almost same as C

• <
h y
at
• > an
a p
u g
uth
• <= th a n M
n a
m a
• >= R a

• ==
• !=

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y
pat
a
an

Demo using L4_if_statements.py


u g
uth
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some nuances
than what C offers

• a<b<c
h y
at
• a == b == c an
a p
u g
uth
• a != b != a (Will this condition return
th a nTrue or False?)
M
n a
a
m
• Any non-zero number is a
treated
R as True (0 as False) - Same as C

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y

REDO the largest of three numbers pat


a
gan
th u
u

using ONLY relational operators


n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Logical operators
and, or, not (NOT &&, ||, ! but works similarly)

• cond1 and cond2 - returns True only if both are true


h y
at
• cond1 or cond2 - returns True if even one aofnapthe is true.
u g
uth
• NOTE: We can replace ‘condition’thwith
an any ‘valid expression’
M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


HW: Redo the largest one including t h y
logical
operators. u g a na p a

HW: Get to know how ‘not’ h a n M u th


operator works.
HW: Also, get to know
Ra
m a n about
a t
the functions any( )
and all( ).

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Loops
while and for (No do-while) - there are differences

while condition: while condition:


h y
statement pat statement
a
gan
th u
u
…… a n M ……
ath
a n
a m
…….. R else:

…….. statement

………….

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Loops
for loop - this iterates over each element in a sequence (string, range, list, tuple etc.)

for ele in seq: for ele in seq:


h y
statement pat statement
a
gan
th u
u
…… a n M ……
ath
a n
a m
…….. R else:

…….. statement

………….

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Key differences between for and while

• for iterates over the iterable (string/ list etc..), where as while does not
h y
at
• while uses a condition where as for doesgnot
an
a p
h u
Mut
an
at h
a n
m
Ra

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L4_loops_example.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


range( ) function
similar to for (exp1; exp2; exp3)

• range(10) - generates numbers from 0 to 9


h y
at
• range(5,15) - generates numbers from 5 toan14
ap (Note this)
u g
uth
• range(0, 10, 2) - generates numbers
th a nfrom 0 to 10 in steps of 2
M
n a
am
• In general, range(start, a
stop, Rstep)
- numbers from ‘start' up to ‘stop’ (but
excluding it) and incrementing/decrementing according to ‘step’

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Back to the demo using


at h
a p
gan
th u
u

L4_loops_example.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


HW: Find about break, continue, and apass t h y statements and
use them in a program. u g a na p
h
HW: Print numbers 1 to 10 onanthe M u t
same line, breaking out of
an infinite loop. m a n a th

HW: Print all unique combinations


R a
of 1, 2 and 3.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Conditional expression
similar to the ternary operator ? : in C

<expr1> if condition else <expr2>


h y
Equivalent to pat
a
gan
th u
u
if condition: a n M
ath
a n
a m
expr1 R

else:

expr2

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras

You might also like