Looping Constructs
Looping Constructs
Scenario
Pseudo-Code:
Looping constructs in Python
Pseudo-Code:
print “Python is awesome” (repeat 1000 times)
Looping constructs in Python
Pseudo-Code: Code:
print “Python is awesome” (repeat 1000 times) for i in range(1000):
print(“Python is awesome”)
Looping constructs in Python: The for loop
Pseudo-Code: Syntax:
print “Python is awesome” (repeat 1000 times) for iterating_variable in sequence:
statements(s)
Looping constructs in Python: The for loop
Pseudo-Code: Syntax:
print “Python is awesome” (repeat 1000 times) for iterating_variable in sequence:
statements(s)
4 space
“indentation”
Looping constructs in Python: The for loop
Pseudo-Code: Code:
print “Python is awesome” (repeat 1000 times) for i in range(1000):
print(“Python is awesome”)
Looping constructs in Python: The for loop
Pseudo-Code: Code:
print “Python is awesome” (repeat 1000 times) for i in range(1000):
print(“Python is awesome”)
4 space
“indentation”
Looping constructs in Python: The for loop
4 space
“indentation”
Types of loops: Example
Types of loops: Example
Scenario #1:
Pass in 5 subjects:
● Math
● Physics
● English..etc.
Types of loops: Example
Scenario #1:
Pass in 5 subjects:
Code:
● Math
for subject in range(5):
● Physics
# Pass exam 5 times
● English..etc.
# Once for each subject
Types of loops: Example
Scenario #1:
“stop condition”
Pass in 5 subjects:
Code:
● Math
for subject in range(5):
● Physics
# Pass exam 5 times
● English..etc.
# Once for each subject
Types of loops: Example
Scenario #2:
Scenario #2:
Scenario #2:
Scenario #2:
Scenario #2:
Syntax: Code:
while comparison:
while grade != ‘A’:
statements(s) # Keep repeating until
# comparison gives a False
Looping constructs in Python: Summary
Code: Code: