0
0
Pythonprogramming~10 mins

Length and iteration methods in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Length and iteration methods
Start with a collection
Call len() function
Return number of items
Use for loop to iterate
Access each item one by one
End after last item
First, we find how many items are in a collection using len(). Then, we use a for loop to go through each item one by one until all are processed.
Execution Sample
Python
fruits = ['apple', 'banana', 'cherry']
print(len(fruits))
for fruit in fruits:
    print(fruit)
This code prints the number of fruits, then prints each fruit on its own line.
Execution Table
StepActionExpression EvaluatedResult/Output
1Create list 'fruits'fruits = ['apple', 'banana', 'cherry']fruits = ['apple', 'banana', 'cherry']
2Call len() on fruitslen(fruits)3
3Print lengthprint(3)3
4Start for loop, first itemfruit = fruits[0]'apple'
5Print first itemprint('apple')apple
6Next loop iterationfruit = fruits[1]'banana'
7Print second itemprint('banana')banana
8Next loop iterationfruit = fruits[2]'cherry'
9Print third itemprint('cherry')cherry
10Loop ends after last itemNo more itemsExit loop
💡 Loop ends because all items in the list have been processed.
Variable Tracker
VariableStartAfter Step 4After Step 6After Step 8Final
fruits['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']
fruitundefined'apple''banana''cherry''cherry'
Key Moments - 3 Insights
Why does the loop stop after printing 'cherry'?
The loop stops because it has gone through all items in the list. This is shown in execution_table row 10 where it says 'No more items' and the loop exits.
What does len(fruits) return and why?
len(fruits) returns 3 because there are three items in the list. This is shown in execution_table row 2 where len(fruits) evaluates to 3.
Why does the variable 'fruit' change each loop?
In each loop iteration, 'fruit' is assigned the next item from the list. This is shown in execution_table rows 4, 6, and 8 where 'fruit' takes values 'apple', 'banana', and 'cherry' respectively.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2. What value does len(fruits) return?
A3
B2
C0
DError
💡 Hint
Check the 'Result/Output' column at Step 2 in the execution_table.
At which step does the loop assign 'banana' to the variable 'fruit'?
AStep 4
BStep 6
CStep 8
DStep 10
💡 Hint
Look at the 'Expression Evaluated' column for the assignment of 'fruit' in the execution_table.
If the list had 4 items instead of 3, how would the execution_table change?
AThe loop would stop earlier.
BThe length printed would still be 3.
CThere would be one more loop iteration with a new fruit value.
DThe variable 'fruit' would not change.
💡 Hint
Refer to how the loop runs once per item in the list shown in the execution_table.
Concept Snapshot
len(collection) returns the number of items in a collection.
A for loop goes through each item one by one.
Inside the loop, a variable holds the current item.
Loop ends after the last item is processed.
Use print() to show values during iteration.
Full Transcript
This example shows how to find the length of a list using len() and then use a for loop to go through each item. First, the list 'fruits' is created with three items. Calling len(fruits) returns 3, which is printed. Then the for loop starts, assigning each fruit to the variable 'fruit' in turn. Each fruit is printed. The loop stops after the last fruit 'cherry' is printed because there are no more items. The variable 'fruit' changes each iteration to hold the current item. This process helps us count and access items in a collection easily.