What is the purpose of a loop in programming?
Loops are a way to repeat an action until a certain condition is met. Python has three types of loops: for, while and nested. For loops are used to iterate (perform repeatedly) through a sequence, While loops are used to repeat an action while a certain condition is true.
Learning about Python Programming Loops
For loop: A type of loop that allows for repeated code execution until a specified condition is met. While loop: A type of loop that executes code as long as a given condition is true. Break statement: A statement used in loops to exit the loop and move on to the next iteration or statement.
"For" loops allow us to repeat code multiple times, as well as provide simpler and easier-to-read programs. When creating a for loop, specific rules that need to be followed. For example, when adding code to the for loop, you must have 4 spaces in front of the code that is part of the loop. This action indicates that the code on that line is part of the code above it.
First, we will need to use the “for” keyword. For loops always start with “for” to show that the following code is a for loop.
For Loop Step 1
Next, provide a name for the counter variable. This variable’s value increases each time the loop repeats. If you use a for loop that loops 10 times, starting with 0 and increasing by 1 each time the loop starts over, the counter variable will represent 0 in the first run, then 1 in the next run, then 2, then 3, and so on, increasing by 1 each time the loop starts over. Although a counter variable can be named anything, a tradition is to use “i”.
For Loop Step 2
Next, add the keyword “in” to show that you are about to specify how many times the loop should repeat.
For Loop Step 3
Then, we will set the number by using the range() function. The range function will count from zero up to, but not including, the number listed in the function’s parentheses. ()
For Loop Step 4
End the first line of the loop with a colon (:). Here is an example: for i in range(3):
For Loop Step 5
What is the purpose of the for loop?
- They allow us to repeat code multiple times.
- They can only be used once.
- They make the program more complicated.
Which of the following for loop declarations is valid?
- for i in range(9)
- for i range X9
- for i in range(9):
Brain break: Draw an octopus flying a hot air balloon.
In Python, the 'for' loop can be used with multiple variables, allowing you to work with multiple items at once. Python's 'while' loop can also be used to generate an infinite loop, which runs until a certain condition is met.
Did you know?
. While loops repeat as long as the conditional statements within them are True. A while loop looks similar to a for loop, but it replaces the counting portion with a conditional statement. A nested loop is when one loop is put inside another loop.
Work together in pairs: What is the difference between a for loop and a while loop in Python programming?
Work together in pairs: What are the three types of Python loops and how are they used?
What is the function of a break statement in a loop?
- Exit the loop immediately
- Skip to the next iteration of the loop
- Restart the current iteration of the loop
What is another name for an infinite while-loop that never ends?
- Endless Loop
- Infinite Loop
- Forever Loop
What is printed when this code runs?
for i in range(3):
if i == 1: