Thursday, 9 April 2026

April Python Bootcamp Day 6 - Loops

 


Day 6: Loops in Python 

Loops are one of the most powerful concepts in programming. They allow you to execute code repeatedly, automate tasks, and handle large data efficiently.

In today’s session, we’ll cover:

  • For Loop
  • While Loop
  • Break & Continue
  • Loop Else Concept (Important & Unique to Python)

 Why Loops Matter?

Imagine:

  • Printing numbers from 1 to 100
  • Processing thousands of users
  • Running a condition until it's satisfied

Without loops → repetitive code ❌
With loops → clean & efficient code ✅


 FOR LOOP

 What is a For Loop?

A for loop is used to iterate over a sequence (list, string, tuple, etc.).

 Syntax

for variable in iterable:
# code block

 How It Works

  • Takes one element at a time from iterable
  • Assigns it to variable
  • Executes the block
  • Repeats until iterable ends

 Example

for i in range(5):
print(i)

 Output:

0
1
2
3
4

 Understanding range()

range(start, stop, step)

Examples:

range(5) # 0 to 4
range(1, 5) # 1 to 4
range(1, 10, 2) # 1, 3, 5, 7, 9

 Looping Through Data Types

String

for ch in "Python":
print(ch)

List

for num in [10, 20, 30]:
print(num)

 FOR-ELSE (Important Concept)

for i in range(3):
print(i)
else:
print("Loop completed")

else runs only if loop doesn't break


 WHILE LOOP

 What is a While Loop?

Executes code as long as condition is True


 Syntax

while condition:
# code block

 Example

i = 0
while i < 5:
print(i)
i += 1

 Infinite Loop

while True:
print("Running...")

 Runs forever unless stopped manually


 WHILE-ELSE

i = 0
while i < 3:
print(i)
i += 1
else:
print("Done")

 Runs only if loop ends normally (no break)


 BREAK STATEMENT

Stops loop immediately

for i in range(5):
if i == 3:
break
print(i)

 CONTINUE STATEMENT

Skips current iteration

for i in range(5):
if i == 2:
continue
print(i)

 FOR vs WHILE

FeatureFor LoopWhile Loop
Based onIterableCondition
Use CaseKnown iterationsUnknown iterations
RiskSafeCan become infinite

 Key Takeaways

  • for loop → iterate over data
  • while loop → run until condition fails
  • break → stops loop
  • continue → skips iteration
  • else → runs only if loop completes normally

 Practice Questions

 Basic

  • Print numbers from 1 to 10 using for loop
  • Print numbers from 10 to 1 using while loop
  • Print all characters in a string
  • Print even numbers from 1 to 20

 Intermediate

  • Sum of first n numbers
  • Multiplication table of a number
  • Count digits in a number
  • Reverse a number

 Advanced

  • Check if number is prime (use loop + break + else)
  • Fibonacci series
  • Pattern printing (triangle)
  • Menu-driven program using while loop

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (239) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (10) BI (10) Books (262) Bootcamp (3) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (29) Data Analytics (21) data management (15) Data Science (340) Data Strucures (16) Deep Learning (145) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (68) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (278) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) pytho (1) Python (1288) Python Coding Challenge (1124) Python Mistakes (50) Python Quiz (466) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (48) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)