2. Conditional Statements in Python
● Till now all the statements we have used, have been sequential
● Control structure - directs the order of execution of code
● In real world, we usually evaluate information & then decide the course of
action
● The code block will be executed only if condition is satisfied.
● if - used in python for conditional execution of statement
● Indentation is important while defining if-blocks
● if-blocks can be nested
3.
4. The else & elif clauses
● Sometimes you might want to execute a block of code if the if-condition is
evaluated to False.
● If if-condition is True, the block under if will be executed, else the block under
else will be executed.
● If there are multiple conditions, elif can be used
● If one of the condition is true, others will be ignored
5.
6. Nested if blocks
If <expr>: - Indentation level 0
<stmt> - Indentation level 1
<stmt>
if <expr>: - Inner if - at indentation level 1
<stmt> - Indentation level 2
<stmt>
else: - Else paired with inner if - level 1
<stmt>
<stmt>
else: - Else paired with the first if - level 0
<stmt>
<stmt>
7. Loops in Python
● Python has 2 loop commands:
○ for loop
○ while loop
● All loops have same basic functionality, they only differ in syntax.
● The loops will run till a condition is met.
8. while loop
● Execute a set of sentences until the condition is true.
● Indented statements required
● Condition should evaluate to True or False
● Loop can be broken in between
9.
10. break, continue, pass
● break - The break is a keyword in python which is used to bring the program
control out of the loop.
● The break statement breaks the loops one by one.
● continue - The continue statement in Python returns the control to the
beginning of the loop.
● The continue statement rejects all the remaining statements in the current
iteration of the loop and moves the control back to the top of the loop.
● pass - The pass statement in Python is used when a statement is required
syntactically but you do not want any command or code to execute.
● The pass statement is a null operation; nothing happens when it executes.
11. for-loop
● used for sequential traversal
● falls under the category of definite iteration
● The loop will be iterated only for a specified number of times.
● We can also execute statements, once for each element in a sequence data
type - list, tuple, string, etc.
● Can use break statement to break the for-loop in between
● Can use pass, continue statements to skip a piece of code.
12.
13. for loops using range, strings, list
● range(start, end) function - will run the for loop from start number to end
number
● Using list, string and tuple as a for loop condition will run the loop once for
each element of these data types.
● You can iterate nested lists using nested for loops
14. Nested loops
● Python language allows to use one loop inside another.
● Any kind of loops can be combined together to form nested loops.
● Even if-elif-else structures can be nested inside loops and vice versa.
15. Nested For Loop
Level 0 indentation
Level 1 indentation (as it is a part of the outer for loop)
Level 2 indentation
These lines are a part of the inner loop
The statements on level 2 indentation will be executed the number of times
specified in the inner loop.
The whole inner loop will be executed the number of times specified in the outer
loop.
16. Nested while loop
Level 0 indentation
Level 1 indentation - inside the outer while loop
Level 2 indentation - statements part of the inner
while loop
The statements inside the inner while loop will be executed till the condition
specified in the inner while loop is successful.
The inner while loop will execute as long as the condition specified in the outer
while loop is successful.