Learn loops in Python with clear examples for beginners. Lecture 5 covers for loops, while loops, range(), nested loops, and real Data Science use-cases, complete with MCQs and practice questions.
What You Will Learn
By the end of this lecture, you will understand:
- What loops are and why we use them
forloopwhilelooprange()function- Looping through lists & dictionaries
- Nested loops
- Real-world data science applications
- MCQs + Practice Questions
1) What Are Loops?
A loop repeats a block of code multiple times.
Real-life examples:
- You check emails every morning → repeated
- You count your steps → repeated
- You scroll Instagram → repeated until you stop
Python does the same using loops.
2) FOR LOOP
Used when you know how many times you want to repeat.
Basic Structure
for variable in sequence:
do something
Example 1 – Print numbers 1 to 5
for i in range(1, 6):
print(i)
Example 2 – Loop through a list
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
xample 3 – Loop through characters
for ch in "Python":
print(ch)
Example 4 – Print even numbers
for num in range(2, 21, 2):
print(num)
Example 5 – Sum of numbers
total = 0
for x in range(1, 6):
total += x
print(total)
3) range() Function – VERY IMPORTANT
range(start, stop, step)
Examples:
Default (0 to 4)
for i in range(5):
print(i)
Custom range
for i in range(10, 21):
print(i)
With step
for i in range(1, 20, 3):
print(i)
4) WHILE LOOP
Runs until a condition becomes False.
Basic Structure
while condition:
do something
Example 1 – Print 1 to 5
i = 1
while i <= 5:
print(i)
i += 1
Example 2 – Countdown
count = 5
while count > 0:
print(count)
count -= 1
Example 3 – Ask user until correct password
password = ""
while password != "python123":
password = input("Enter password: ")
print("Access Granted")
Example 4 – Sum until input is 0
total = 0
num = int(input("Enter number (0 to stop): "))
while num != 0:
total += num
num = int(input("Enter number (0 to stop): "))
print("Total:", total)
5) Looping Through DICTIONARIES
Example 1 – Loop through keys
student = {"name": "Ali", "age": 21, "grade": "A"}
for key in student:
print(key)
Example 2 – Loop through values
for value in student.values():
print(value)
Example 3 – Loop through items
for key, value in student.items():
print(key, ":", value)
6) Nested Loops (Loop inside Loop)
Example 1 – Multiplication Table
for i in range(1, 6):
for j in range(1, 6):
print(i, "*", j, "=", i*j)
Example 2 – Printing a Square Pattern
for i in range(5):
for j in range(5):
print("*", end=" ")
print()
Example 3 – Looping Students & Courses
students = ["Ali", "Sara"]
courses = ["Math", "AI"]
for s in students:
for c in courses:
print(s, "is enrolled in", c)
7) Real Data Science Examples
Example 1 – Counting missing values
data = [10, None, 20, None, 30]
missing = 0
for x in data:
if x is None:
missing += 1
print("Missing values:", missing)
Example 2 – Cleaning dataset values
raw_scores = ["85", "90", "N/A", "75"]
cleaned = []
for score in raw_scores:
if score != "N/A":
cleaned.append(int(score))
print(cleaned)
Example 3 – Finding maximum temperature
temps = [32, 36, 30, 38, 35]
max_temp = temps[0]
for t in temps:
if t > max_temp:
max_temp = t
print("Max temp:", max_temp)
Example 4 – Building frequency table
nums = [1, 2, 2, 3, 3, 3]
freq = {}
for n in nums:
if n in freq:
freq[n] += 1
else:
freq[n] = 1
print(freq)
Quiz – Test Your Understanding
Instruction: First read the question and choose your answer. Then scroll down to see the correct answer
What does a loop do?
a) Runs code once
b) Runs code repeatedly
c) Stops code
d) Deletes variables
Answer: b) Runs code repeatedly
What will this print?
for i in range(3):
print(i)
a) 1 2 3
b) 0 1 2
c) 0 1 2 3
d) Error
Answer: b) 0 1 2
Which loop runs until a condition is false?
a) for
b) while
c) until
d) loop
Answer: b) while
What is the output?
i = 5
while i > 3:
print(i)
i -= 1
Answer:
5
4
3
Practice Questions
Instruction for students: Read the question, open your Jupyter Notebook, and try to solve it yourself. After that, come back and check the answer.
for i in range(10, 0, -1):
print(i)
total = 0
for i in range(1, 51):
total += i
print(total)
for i in range(2, 101, 2):
print(i)
nums = []
for i in range(5):
n = int(input("Enter number: "))
nums.append(n)
print(nums)
for i in range(1, 5):
for j in range(i):
print("#", end="")
print()




