Loops in Python

Python Loops Explained

🔁 Mastering Loops in Python

Loops are used to repeat a block of code multiple times. Python has two types:

  • for loop – used when we know the number of iterations
  • while loop – used when the condition controls the loop

🔹 The range() Function

The range() function generates a sequence of numbers:

  • range(stop) – starts from 0 to stop-1
  • range(start, stop) – from start to stop-1
  • range(start, stop, step) – jumps by step size
for i in range(1, 10, 2):
    print(i)

🔸 for Loop

Iterates through a sequence like list, string, or range:

for letter in "Python":
    print(letter)

🔁 Understanding range() in Python

The range() function in Python is used to generate a sequence of numbers. It can take:

  • Start (optional): the starting number. Default is 0.
  • Stop: the end point (not included).
  • Step (optional): how much to increase each time. Default is 1.

🧮 Example 1: Natural Numbers from 1 to n

This prints numbers like 1, 2, 3, ..., n.

n = int(input("Enter a number: "))
for i in range(1, n + 1):
    print(i, end=", ")

🧮 Example 2: Even Numbers from 2 to n

This prints even numbers like 2, 4, 6, ..., n.

n = int(input("Enter a number: "))
for i in range(2, n + 1, 2):
    print(i, end=", ")

🧮 Example 3: Odd Numbers from 1 to n

This prints odd numbers like 1, 3, 5, ..., n.

n = int(input("Enter a number: "))
for i in range(1, n + 1, 2):
    print(i, end=", ")

🔁 Python range() in Decreasing Order

Understanding how to loop backwards using Python's range(start, stop, step) is very helpful when working with countdowns or reverse operations. Use a negative step value to go in reverse.

📍 Example 1: Print 10 to 1

for i in range(10, 0, -1):
    print(i, end=' ')

Output: 10 9 8 7 6 5 4 3 2 1

📍 Example 2: Even Numbers from 20 to 2

for i in range(20, 1, -2):
    print(i, end=' ')

Output: 20 18 16 14 12 10 8 6 4 2

📍 Example 3: Odd Numbers from 19 to 1

for i in range(19, 0, -2):
    print(i, end=' ')

Output: 19 17 15 13 11 9 7 5 3 1

📍 Example 4: Countdown by 5s from 50

for i in range(50, 0, -5):
    print(i, end=' ')

Output: 50 45 40 35 30 25 20 15 10 5

📍 Example 5: Reverse Alphabet (Z to A)

for i in range(90, 64, -1):
    print(chr(i), end=' ')

Output: Z Y X ... C B A

👉 Use these examples to solidify your understanding of Python's range() in reverse. Play around by changing the start, stop, and step values!

🔸 while Loop

Executes as long as the condition is True:

count = 1
while count <= 5:
    print(count)
    count += 1

🔸 Nested Loops

for i in range(1, 4):
    for j in range(1, 4):
        print(i, j)

💻 Programming Assignments

  1. Print all even numbers from 1 to 50.
  2. Generate a multiplication table using nested loops.
  3. Calculate the factorial of a number using while loop.
  4. Write a program to display the Fibonacci sequence up to n terms.
  5. Print a pattern:
    *
    **
    ***
    ****
    

📌 Summary

  • for – for definite repetition
  • while – for indefinite repetition
  • range() – controls the loop: range(start, stop, step)

Post a Comment

0 Comments

Me