In BDA, we will gradually build our own small libraries. First, let's recap the basics.
In this tutorial, you will practice core Python loop logic using:
- counters
- indexes
forloops- nested loops (matrix traversal)
Before starting:
- Open the
session1folder in Visual Studio Code. - Activate your virtual environment.
- Open (or create) your solution files in:
session1/solutions/counter: a variable that increases/decreases to track quantity.index: the position of an element in a list (starts at0).pointer: in this class, we use this word like a position tracker (usually an index counter).range(len(data)): common pattern to iterate through list indexes.nested loop: a loop inside another loop (used for matrices).break: stops the current loop immediately.
Consider a list. Can you count all elements without using len()?
Create a file in your solutions/ folder with this code:
File: session1/solutions/exercise-01-02.py
data = [10, 20, 30, 40, 50]
count = 0
for item in data:
count += 1
print(count)Expected output:
5Now wrap the same logic in a function:
File: session1/solutions/exercise-01-02.py
def my_len(data):
count = 0
for item in data:
count += 1
return countSave this function in a different file in the same folder, for example exercise_01_02_lib.py.
Then import and use it from your main script:
File: session1/solutions/exercise-01-02.py
from exercise_01_02_lib import my_len
print(my_len([10, 20, 30]))Expected output:
3Note
What are the time and space complexities of my_len?
Show answer
Time: O(n)
Space: O(1)
If you do not get it, talk to Stelios.
Let's sum all elements using a total variable.
File: session1/solutions/exercise-01-02.py. Fill in the missing code.
data = [10, 20, 30, 40, 50]
total = 0
...
print(total)Expected output:
150Solution
Show answer
count += 1increments a counter, whiletotal += itemaccumulates values.data = [10, 20, 30, 40, 50] total = 0 for item in data: total += item print(total)
Good practice: wrap reusable logic into helper functions (for example exercise_01_02_lib.py) and import them into your main script.
Note
What are the time and space complexities of the script above?
Show answer
Time: O(n)
Space: O(1)
Let's find the position of a target value.
File: session1/solutions/exercise-01-02.py. Fill up the missing code.
data = [10, 20, 30, 40, 50]
# We use `pointer` as an index counter (starting at 0).
pointer = 0
...Expected output (0-based index, counting starts from 0):
2Solution
Show answer
We use a position counter (
pointer) and stop at the first match usingbreak.data = [10, 20, 30, 40, 50] pointer = 0 for item in data: if item == 30: print(pointer) break pointer += 1
Another common way to work with positions is:
File: session1/solutions/exercise-01-02.py
data = [10, 20, 30, 40, 50]
for i in range(len(data)):
print(i)Run this and explore the output.
File: session1/solutions/exercise-01-02.py
matrix = [
[10, 20],
[30, 40]
]
for row in matrix:
print(row)
for value in row:
print(value)Now track row and column indexes explicitly:
File: session1/solutions/exercise-01-02.py
matrix = [
[10, 20],
[30, 40]
]
row_index = 0
col_index = 0
for row in matrix:
print("row:", row_index)
for value in row:
print("col:", col_index, "value:", value)
col_index += 1
# Reset col_index for each new row.
col_index = 0
row_index += 1Expected output:
row: 0
col: 0 value: 10
col: 1 value: 20
row: 1
col: 0 value: 30
col: 1 value: 40Note
Why do we set col_index = 0 after each row?
Show answer
- Because each new row starts from the first column again.
- If you do not reset it, column indexes continue from the previous row and become incorrect.
What are the time and space complexities of the script above?
Show answer
Time: O(r * c) where r is the number of rows and c is the number of columns.
Space: O(1)
Call Stelios for a quick challenge question before moving to the exercise.
Add your answers to:
session1/solutions/Tasks:
- Write a function to count elements between
1and10(inclusive) indata = [30, 6, 9, 12, 15, 8]. - Write a function to sum all even numbers in the same list.
- Write a function that returns the position of the first value equal to
12in the same list. If the value is not found, return-1. - For the matrix below, print the position of
25as coordinates [2, 2] in the matrix (row, column), using 1-based indexing.
File: session1/solutions/exercise-01-02.py
matrix = [
[5, 10, 15],
[20, 25, 30]
]- In one short comment, explain why resetting the column index is important in nested loops.
- What are the time and space complexities of your script(s)?
Complete the following quiz.
quizmd quizzes/python-loops-and-indexing-quiz.mdIf you want to choose a theme:
quizmd --theme light quizzes/python-loops-and-indexing-quiz.md
quizmd --theme dark quizzes/python-loops-and-indexing-quiz.mdYou are now ready to move to the next tutorial.