0% found this document useful (0 votes)
2 views

PRO511-Chapter 4 Lecture Notes

Chapter Four covers repetition structures in Python, focusing on while loops and for loops. It explains condition-controlled and count-controlled loops, how to calculate a running total, and the use of sentinels to terminate loops. Additionally, it introduces nested loops for complex iterations and discusses augmented assignment operators for simplifying variable updates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PRO511-Chapter 4 Lecture Notes

Chapter Four covers repetition structures in Python, focusing on while loops and for loops. It explains condition-controlled and count-controlled loops, how to calculate a running total, and the use of sentinels to terminate loops. Additionally, it introduces nested loops for complex iterations and discusses augmented assignment operators for simplifying variable updates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter Four: Repetition Structures

Learning Outcomes

By the end of this section, learners should be able to:

• Use the while loop to write loop-controlled programs.

• Write programs that use a count-controlled loop.

• Write a program that calculates a Running Total.

• Understand and use sentinels to terminate loops.

• Validate input using loops.

• Implement programs that use nested loops.

4.1 Introduction

Repetition structures, also known as looping structures, allow you to repeat a set of instructions
multiple times. There are two main types of looping structures in Python: for loops and while
loops.

4.2 Introduction to Repetition Structures

Repetition structures (loops) allow you to execute a block of code multiple times. Python has
two types of loops:

• For loops: Used to iterate over a sequence (such as a list, tuple, or string) or any iterable
object, executing a block of code for each item.

• While loops: Used to repeatedly execute a block of code as long as a given condition
remains true.

Condition-Controlled and Count-Controlled Loops

A condition-controlled loop uses a true/false condition to determine whether it should


continue iterating. A count-controlled loop executes a predetermined number of times.

4.3 The While Loop: A Condition-Controlled Loop

A while loop executes as long as its condition remains true.

Syntax:

count = 0

while count < 5:

print(count)

count += 1

Output:

0
1

Example (User Input Control):

keep_going = 'y'

while keep_going == 'y':

keep_going = input("Continue? (y/n): ")

4.4 The For Loop

A for loop executes a block of code a set number of times.

Syntax:

for variable in [value1, value2, etc.]:

statement

Example:

for num in [1, 2, 3, 4, 5]:

print(num)

4.5 Using the range Function with the for Loop

The range function generates a sequence of numbers:

Syntax:

for variable in range(start, end, step):

statement

Example:

for num in range(1, 10, 2):

print(num)

4.6 Calculating a Running Total

A running total accumulates values in a loop.

Example:

numbers = [5, 3, 7, 2, 8]
running_total = 0

for number in numbers:

running_total += number

print(f'The total is {running_total}.')

4.7 Sentinels

A sentinel is a special value that signals a loop to stop.

Example: Property Tax Calculation

TAX_FACTOR = 0.0065

print('Enter property lot number or 0 to end.')

lot = int(input('Lot number: '))

while lot != 0:

value = float(input('Enter property value: '))

tax = value * TAX_FACTOR

print(f'Property tax: ${tax:,.2f}')

lot = int(input('Enter next lot number or 0 to end: '))

4.8 Nested Loops

A nested loop is a loop inside another loop.

Syntax:

for outer in range(1, 4):

for inner in range(1, 4):

print(f'Outer: {outer}, Inner: {inner}')

Summary

• While loops run as long as a condition is true.

• For loops iterate over sequences or use range().

• Sentinels help terminate loops based on user input.

• Nested loops allow complex iteration over multiple variables.

• Augmented assignment operators simplify updating variables in loops.

You might also like