Python Learning Series · @onlysamarrr · Day 10 / 30

Day 10: for Loop and range()

Day 10 teaches the for loop and range(). range(5) gives 0, 1, 2, 3, 4: the end value is never included.

Phase: Beginner · Reels: 46/450 – 50/450

Quick Answer

Day 10 teaches the for loop and range(). range(5) gives 0, 1, 2, 3, 4: the end value is never included.

What You Will Learn

Code

for i in range(5):
    print(i, end=" ")
print()
print(list(range(2, 11, 2)))
print(sum(range(1, 11)))
for i, f in enumerate(["mango", "banana"], start=1):
    print(i, f)
for i in range(1, 4):
    print(f"5 x {i} = {5 * i}")

Output

0 1 2 3 4 
[2, 4, 6, 8, 10]
55
1 mango
2 banana
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15

Code Explained, Line by Line

  1. range(5) produces 0 to 4. The number 5 itself is not included.
  2. range(2, 11, 2) starts at 2, stops before 11, and steps by 2: 2, 4, 6, 8, 10.
  3. sum(range(1, 11)) adds 1 to 10 and gives 55.
  4. enumerate() gives both a number and an item while looping over a list.
  5. Use for when you know what to loop over or how many times to loop.
Common Mistake: To get 1 to 10 you need range(1, 11), not range(1, 10), because the end value is never included.

Real-Life Use

Tables, lists of items and repeated calculations in apps are all for loops.

Practice Task: Find the sum of 1 to 10 and print the 5 times table (a table generator).

Test Yourself — MCQs

Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.

Q1. What numbers does range(5) give?

Q2. What is sum(range(1, 11))?

Q3. What is the last value of range(2, 11, 2)?

Today's 5 Reels (from the 90-Day Plan Excel)

46/450Conceptfor loop basics
47/450Live demorange(start, stop, step)
48/450Common mistakerange(5) mein 5 nahi aata
49/450QuizQuiz: 1 se 10 tak sum
50/450Interview / real lifeReal life: table generator
← Day 9Day 11 →