[CreativeProgramming]Lecture6_Iteration in Python
[CreativeProgramming]Lecture6_Iteration in Python
Spring 2025
CUL1122 Lecture #06
Iteration in Python
Today
• Python Loops
• Syntax for Using the while Loop
❖range() and for Loop
▪ Syntax of the range() Function
▪ Steps to Use range() Function
▪ range() Examples
▪ for Loop with range() Function
❖for loop vs. while loop
3
Three Control Flows in a Computer Program: Iteration
Code 1 Code
If expression If expression
is True Expression is False If expression
is True
Expression
Code 2
Code 1 Code 2
If expression
Code 3 is False
4
The while Loop in Python
❖A while loop begins with ‘while,’ includes a condition, and ends with a
colon.
❖During each iteration:
▪ 1) The loop condition is checked.
▪ 2) If the condition is True, the code block inside the loop is executed.
▪ 3) Otherwise, the script leaves the The while Loop False
loop and proceeds to the next line while Condition: 1) Cond
of code. Code Block True
2) Code Block
3) …
5
while Loop Explained with an Example
Initialization
count = 0
Condition
while count < 1000:
Loop Body print(‘Thank you!’)
(Code Block) Increment
count = count + 1
6
Terminating the Loop Immediately
▪ This function requires a stop value, while the start and step values are optional.
❖Parameters
Name Description Default Value
start Specifying the initial value of the sequence. 0
stop Denoting the end value of the sequence (exclusive). N/A
step Defining the increment between consecutive numbers in the sequence. 1
8
range() Function Method #1: range(stop)
❖The most basic way to use the range() function is by specifying only the
end value, denoted as range(stop).
❖When using this form, the range() function automatically starts from 0,
increments by 1, and ends at stop - 1.
❖For example, to print the first 6 numbers starting from 0, you would use
the following code:
range(6)
for i in range(6): 0 0 1 2 3 4 5 6
print(i) sequence
start stop
▪ In this case, the default values are start = 0 and step = 1.
9
range() Function Method #2: range(start, stop)
❖You can also use the range() function by specifying both the start and
stop values, denoted as range(start, stop).
▪ This method is useful when you want the sequence to start from a value other
than 0.
❖When you provide two arguments to range(), it produces integers
starting from the start value up to, but not including, the stop value.
❖For example, to print numbers from 10 to 15, you would use the
following code:
range(10, 16)
for i in range(10, 16): 10 10 11 12 13 14 15 16
print(i) sequence
start stop
10
range() Function Method #3: range(start, stop, step)
❖You can use the range() function with start, stop, and step parameters
to specify increments.
❖When all three arguments are provided, the function creates a
sequence starting from the start value, incrementing by the step value,
and stopping before reaching the stop value.
❖For example, to print multiples of 5 from 10 to 35, you can use the
following code with a step value of 5:
range(10, 40, 5)
for i in range(10, 40, 5): 10 10 15 20 25 30 35 40
print(i) sequence
start stop
11
Syntax for a for Loop
13
Using the for Loop with the range() Function
❖You can use a for loop to iterate over a sequence of numbers generated
by the range() function.
❖The loop variable acts as an iterator that sequentially takes on each
value generated by the range() function as the loop progresses.
❖Let’s explore how to utilize a for loop with the range() function to
display the odd numbers from 1 to 12:
▪ 1) Iteration through the sequence: The range() function generates a number
sequence starting from the initial value up to, but not including, the stop value.
range(1, 12, 2)
for counter in range(1, 12, 2): 1 1 3 5 7 9 11 12
print(counter)
start sequence stop
14
Using the for Loop with the range() Function
▪ 2) counter represents each element: In each iteration of the loop, counter takes
on the value of each element in the sequence generated by range().
➢For example, in the previous example, during the first iteration, counter will be 1; during
the second iteration, counter will be 3; and so on, until it reaches 11 on the sixth
iteration.
▪ 3) Accessing elements in the sequence: Within the loop block, you can use the
variable counter to access elements in the generated sequence or perform
operations based on those elements.
15
Looping Through a String with a for Loop
❖In Python, when you use a for loop with a string, it iterates over each
character in the string, one at a time.
❖You can process each character individually or perform operations on
each character.
❖For instance, the variable char represents each character in the string,
and you can print each character using the print() function.
my_string = ‘Hello, world!’
# Iterate over each character in the string using a for loop
for char in my_string:
print(char)
16
Looping Through a List with a for Loop
❖In Python, when you use a for loop with a list, it iterates over each
element in the list, one at a time.
❖This method is useful for performing operations on each element of the
list or processing the elements individually.
❖Inside the loop, the variable item represents each element in the list.
You can print each element using the print() function.
my_list = [1, 3, 5, 7, 9]
# Iterate over each element in the list using a for loop
for item in my_list:
print(item)
17
for Loop vs. while Loop
❖In programming, there are two types of iteration: definite and indefinite.
❖Definite iteration involves specifying the number of times a designated
block of code will be executed when the loop starts.
❖Using a for loop for a specific number of iterations:
attempts = 0
for i in range(5): # Allow five attempts
password = input(‘Enter your password: ‘)
if password == ‘password123’:
print(‘Access granted!’)
break
else:
print(‘Incorrect password. Try again!’)
attempts = attempts + 1
18
for Loop vs. while Loop
19
for Loop vs. while Loop
❖On the other hand, indefinite iteration doesn’t specify the number of
loop executions in advance.
❖Instead, the designated block is executed repeatedly as long as a
condition is met.
❖The while loop is primarily used for indefinite iteration.
print(‘Using a while loop to allow for unlimited attempts:’)
while True: # Allow infinite attempts
password = input(‘Enter your password: ‘)
if password == ‘password123’:
print(‘Access granted!’)
break
20
Lab 6
Today
22
Exercise #1: Summing Even Numbers
❖ Create a script that calculates the sum of even numbers between 1 and
20 using both for and while loops.
23
Exercise #2: Countdown
❖Create a script that counts down using both a for loop and a while loop.
▪ Each countdown should start from 10 and end with “Blast off!” after reaching 1.
24
Exercise #3: Displaying a Movie Snack Menu
25
Exercise #4: Password Check
26
Exercise #5: Number Guessing
27
Exercise #6: FizzBuzzWoof Implementation
28
Exercise #7: Identifying Primes in a Range
29
수고하셨습니다!
30