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

Repetition Control Structure

The document discusses repetition control structures in Python, specifically for and while loops. It provides examples of using for loops to iterate over lists and ranges of numbers, and using while loops to repeat a block of code as long as a condition is true. It also includes exercises for users to write programs using these loop structures.

Uploaded by

Rio Rabino
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Repetition Control Structure

The document discusses repetition control structures in Python, specifically for and while loops. It provides examples of using for loops to iterate over lists and ranges of numbers, and using while loops to repeat a block of code as long as a condition is true. It also includes exercises for users to write programs using these loop structures.

Uploaded by

Rio Rabino
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

PYTHON

REPETITION
CONTROL
STRUCTURES
REPETITION CONTROL
STRUCTURES
TWO MAIN REPETITION CONTROL
STRUCTURES IN PYTHON

01
02
A REVISIT IN C for LOOP

• initialization: This is the initial value of the loop control variable. It is executed only once,
before the loop starts.
• condition: This is the condition that is checked at the beginning of each iteration. If the
condition is true, the loop continues; otherwise, the loop ends.
• increment/decrement: This is the operation that is performed at
the end of each iteration to update the loop control variable.
• code to be executed: This is the block of code that is executed
repeatedly as long as the condition is true.
Python for Loop

• For instance, if you want to print the numbers from 1 to 100;


or

• You want to display the elements in this variable:


• num = (1, 2, 3, 4, 5)
Python for Loop

• In this structure, item is a variable that takes on the value of each


element in sequence on each iteration of the loop.
• The indented block of code following the for statement is the body
of the loop, and is executed once for each value in the sequence.
Flowchart of Python for Loop
Example: Loop over Python list

Output:
Python for Loop with Python range()


Example: for loop using range()

Output:
Example: for loop using range()
What about this one?
Output:

• In this example, range(1, 6) generates a sequence of numbers from 1


to 5 (inclusive), and the for loop assigns each number to the variable i in
turn.
Group Activity
1. Given the variable below with its corresponding elements:
num = [1, 2, 3, 4, 5]

using for loop, write a program that will display the output:
Group Activity
2. Using for loop, write a program that will ask the name of the user, and will
display the name 10 times.
Python while Loop

The syntax of a while loop is as follows:

while condition
condition True while

3. condition
condition False
condition False
Example of while loop
Write a program that will print the numbers from 1 to 5.
Program

Process
1.the condition is i <= 5.
Output
2.The loop will continue to execute as long as
i is less than or equal to 5
3.Inside the loop, the value of i will be
printed,
4.then increment it by 1 using i += 1.
Group Activity
1. Write a program that asks the user to enter a number, and then prints out
all the even numbers from 0 to that number.

2. Write a program that asks the user to enter a password, and keeps
prompting them until they enter the correct password (which you can hard-
code in the program).

3. Write a program that asks the user to enter a number, and then prints out
the sum of all the digits in that number.
Group Activity
Average:

1. Write a program that generates a random number between 1 and 100, and
asks the user to guess the number. If the user's guess is too high or too
low, the program should give a hint and continue prompting the user until
they guess the correct number.
Group Activity
Difficult:

1. Write a program that asks the user to enter a sentence, and then prints out
the longest word in the sentence.

2. Write a program that asks the user to enter a series of numbers, and then
finds the largest and smallest numbers in the series. The user should be
able to enter as many numbers as they like, and the program should keep
prompting the user until they enter a special character (such as a negative
number) to signal the end of the series.
THANK YOU

You might also like