Definite iteration is iteration a fixed number of times, usually looping through some kind of iterable like a list.
Python implements definite iteration using for loops. It consists of three parts:
Initialization: A variable is declared and initialized with an initial value.
Condition: A condition is checked before each iteration. If the condition is true, the loop continues; otherwise, it terminates.
Update: The variable is updated after each iteration.
Here's an example of a for loop:
for i in range(5): print("i is currently ", i)
The loop repeats 5 times, each time incrementing i by one - 0,1,2,3,4
Each time through the loop it checks to see if there is still a number left in the range. if there are no numbers remaining in the range the loop terminates
What is the primary purpose of a for loop in programming?
{"answers": {"A": "To define a reusable block of code", "B": "To store multiple values in a single variable", "C": "To perform a set of instructions repeatedly for each item in a sequence", "D": "To evaluate conditions and execute code based on the result"}, "question_text": "What is the primary purpose of a for loop in programming?", "correct_answer": "C"}
In Python, which keyword is used to start a for loop?
{"answers": {"A": "repeat", "B": "while", "C": "do", "D": "for"}, "question_text": "In Python, which keyword is used to start a for loop?", "correct_answer": "D"}
In Python, how many parts does a for loop declaration typically consist of?
{"answers": {"A": "1", "B": "4", "C": "3", "D": "2"}, "question_text": "In Python, how many parts does a for loop declaration typically consist of?", "correct_answer": "D"}
Range() function
Definition:
The range() function in Python is used to generate a sequence of numbers. It returns a range object which represents a sequence of numbers within a specified range. The range object can be used in for loops or converted to other iterable data types like lists.
Syntax:
range(start, stop, step)
start: Optional. The number to start the sequence from. If not provided, it defaults to 0.
stop: Required. The number to stop the sequence at (exclusive). It does not include this number in the sequence.
step: Optional. The difference between each number in the sequence. If not provided, it defaults to 1.
Example:
code:
for i in range(0, 10, 2):
print(i)
output:
0
2
4
6
8
Explanation:
In the above example, the range function is used to generate a sequence of numbers from 0 to 10 (exclusive) with a step size of 2. Each number in the sequence is printed using a for loop.
What does the range() function in Python do?
{"answers": {"A": "Returns a sequence of numbers starting from 0 up to, but not including, the given stop value, incrementing by 1.", "B": "Returns a sequence of numbers starting from the given start value up to, but not including, the stop value, incrementing by the given step.", "C": "Returns a sequence of numbers starting from 1 up to, but not including, the given stop value, incrementing by 1.", "D": "Returns a sequence of numbers starting from the given start value up to, but not including, the stop value, incrementing by 1."}, "question_text": "What does the range() function in Python do?", "correct_answer": "B"}
What is the output of the following Python code?
for i in range(2, 6): print(i)
{"answers": {"A": "2 3 4", "B": "3 4 5 6", "C": "1 2 3 4 5", "D": "2 3 4 5"}, "question_text": "What is the output of the following Python code?\n\nfor i in range(2, 6):\n print(i)", "correct_answer": "D"}
What is the output of the following Python code?
for i in range(3, 12, 2): print(i, end=' ')
{"answers": {"A": "4 6 8 10 12", "B": "3 5 7 9 11", "C": "2 4 6 8 10", "D": "3 4 5 6 7 8 9 10 11"}, "question_text": "What is the output of the following Python code?\n\nfor i in range(3, 12, 2):\n print(i, end=' ')", "correct_answer": "B"}
Review: Fill in the Blanks
Definite iteration refers to the process of executing a loop a fixed number of times, which usually involves iterating through an like a list. In Python, definite iteration is accomplished using , which consist of three essential parts: initialization, condition, and update.
Initialization involves declaring a variable and assigning it an . The condition is checked before each iteration, and if it is true, the loop continues; if false, the loop . After each iteration, the variable undergoes an to reflect the next value in the sequence.
The function in Python generates a sequence of numbers and returns a range object that can be utilized in for loops. This function has a specific syntax that includes a start, stop, and step, where the start is optional and defaults to , the stop is required, and the step is also optional and defaults to .
For example, when using the range function with a start of 0, a stop of 10, and a step of 2, the for loop prints the sequence of numbers 0 through 10 (exclusive), incrementing by each time. Each printed number is part of the generated sequence, demonstrating how the range function effectively controls the flow of the .
Click the keywords below to fill in the blanks:
Loop
Range()
2
For Loops
Terminates
Iterable
0
Update
Initial Value
1