Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 6 Loops (for / range)

Updated
2 min read
S

Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey

Building skills in Python and full-stack development, with a focus on web apps and system design

Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering

From Python basics to full-stack projects, sharing my progress as I grow into a developer

Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development

Day 6 – Loops (for / range)

01. What is a Loop?

Loops are used when you want to repeat the same action multiple times.
In Python, the two main types of loops are for and while.
(Today we focus on for loops.)


02. Basic for Loop

A for loop iterates over iterable objects like lists, strings, or ranges.

fruits = ["Apple", "Banana", "Grape"]

for f in fruits:
    print(f)

👉 Output:

Apple
Banana
Grape

03. The range() Function

A built-in function commonly used for number sequences.

range(end)            # 0 to end-1
range(start, end)     # start to end-1
range(start, end, step) # start to end-1 with step

Examples:

for i in range(5):
    print(i)   # 0,1,2,3,4

for i in range(1, 6):
    print(i)   # 1,2,3,4,5

for i in range(2, 11, 2):
    print(i)   # 2,4,6,8,10

04. Nested Loops

A for loop can be placed inside another for loop.

for i in range(2, 4):       # 2, 3
    for j in range(1, 4):   # 1, 2, 3
        print(i, "*", j, "=", i * j)

05. Looping Through Strings

Strings are iterable, so you can loop through each character.

for ch in "Python":
    print(ch)

06. Practice Examples

Example 1: Sum of Numbers from 1 to 10

total = 0
for i in range(1, 11):
    total += i
print("Sum:", total)

Example 2: Multiplication Table (2 times table)

for i in range(1, 10):
    print(f"2 x {i} = {2*i}")

Example 3: Iterating Over a List

names = ["Tom", "Anna", "Sabin"]
for n in names:
    print(f"Hello, {n}!")

Python Basics

Part 6 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 7 While Loops + Mini Projects

01. Basic Structure of a while Loop Repeats as long as the condition is True Stops when the condition becomes False count = 1 while count <= 5: print("Hello", count) count += 1 👉 Output: Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 02. Inf...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.