Introduction To Python
Introduction To Python
we looked at
- conditional structures: if statement and match statement
- iteration using for loop
- range() function for generating arithmetic progressions
len() function
break, continue, and pass statements
Using else conditional statement with for loop in python
output
• Example
for i in range(len(employee))
print(employee[i])
We can use the following loop control statements change the normal execution flow
• break statement
• continue statement
• pass statement
Code block
immediately after
the while loop
output
Code block
immediately after
the while loop
The pass statement in Python is used when a statement is required syntactically but you
do not want any command or code to execute
• E.g. it can be used to write empty loops
The else block just after for/while is executed only when the loop is NOT terminated by a
break statement
output
With the break statement we can stop the loop even if the while condition is true:
i = 1,
while i < 5:
print (i)
if i == 3
break
i = i + 1
Exit the loop and execute the statements outside the loop
Nested loops consist of an outer loop and one or more inner loops. Each time the outer
loop is repeated, the inner loops are re-entered and started anew.
Useful when you have to iterative over multidimensional data structures like dictionaries
in Python
If you have a block of code you want to run x number of times, within that a block of
code which you want to run y number of times, you use what is known as a "nested
loop"
output
Display the elements in the following dictionary data structure as in the figure below
items() method returns an object that contains the key-value pairs of the dictionary, as
tuples in a list
We looked at:
• Iteration
• for loop
• while loop
• range() function
• len() function
• Using else statement with for and while loops
• break, continue, and pass statements
• Nested loops