- Python Basics
-
Overview
- Introduction to Python and Installation
- Variables and Data Types
- Conditional Statements (if-else)
- Loops (for, while)
- Functions and Lambda Expressions
- Lists, Tuples, and Dictionaries
- File Handling (Reading/Writing Files)
- Exception Handling (Try, Except)
- Modules and Packages
- List Comprehensions and Generators
Loops (for, while)
Add to Bookmark1. Introduction
Loops are used to execute a block of code multiple times. Python provides two main types of loops:
for
loop (used for iterating over sequences like lists, tuples, strings, etc.)while
loop (executes until a condition becomesFalse
)
Loops help in reducing redundancy and improving code efficiency.
2. The for
Loop
The for
loop is used when you want to iterate over a sequence like a list, tuple, dictionary, string, or range.
Syntax
for variable in sequence:
# Code to execute
Example: Iterating Over a List
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output
apple
banana
cherry
Using range()
in for
Loop
The range()
function generates a sequence of numbers.
for i in range(5): # Generates numbers 0 to 4
print(i)
Output
0
1
2
3
4
Using range(start, stop, step)
for i in range(1, 10, 2): # Start from 1, stop at 10, step by 2
print(i)
Output
1
3
5
7
9
Iterating Over a String
for char in "Python":
print(char)
Output
P
y
t
h
o
n
3. The while
Loop
The while
loop runs as long as the condition is True
.
Syntax
while condition:
# Code to execute
Example: Counting from 1 to 5
count = 1
while count <= 5:
print(count)
count += 1 # Increment count
Output
1
2
3
4
5
4. Controlling Loops (break
and continue
)
Using break
(Stops the Loop)
for num in range(1, 10):
if num == 5:
break # Stops when num is 5
print(num)
Output
1
2
3
4
Using continue
(Skips Current Iteration)
for num in range(1, 6):
if num == 3:
continue # Skips number 3
print(num)
Output
1
2
4
5
5. Using else
with Loops
Python allows else
with loops, which executes if the loop completes normally (i.e., without break
).
Example: Using else
with for
for i in range(1, 4):
print(i)
else:
print("Loop completed successfully!")
Output
1
2
3
Loop completed successfully!
Example: Using else
with while
x = 1
while x < 4:
print(x)
x += 1
else:
print("Loop completed!")
Output
1
2
3
Loop completed!
6. Nested Loops
A loop inside another loop.
for i in range(1, 3):
for j in range(1, 4):
print(f"i={i}, j={j}")
Output
i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=2, j=2
i=2, j=3
7. Infinite Loops (Avoid!)
An infinite loop runs forever if the stopping condition is never met.
while True:
print("This will run forever!") # Press Ctrl+C to stop
8. Practical Example: Sum of Numbers
total = 0
for num in range(1, 6):
total += num
print("Sum:", total)
Output
Sum: 15
9. Summary
for
loops iterate over sequences like lists, strings, and ranges.while
loops run as long as the condition is True
.break
stops the loop, and continue
skips an iteration.else
runs when loops finish normally.
Nested loops allow loops inside loops.
Prepare for Interview
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
Random Blogs
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- What Is SEO and Why Is It Important?
- Variable Assignment in Python
- Generative AI - The Future of Artificial Intelligence
- Datasets for analyze in Tableau
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- How to Start Your Career as a DevOps Engineer
- Big Data: The Future of Data-Driven Decision Making
- Understanding HTAP Databases: Bridging Transactions and Analytics
- The Beginner’s Guide to Normalization and Denormalization in Databases
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- AI Agents & Autonomous Systems – The Future of Self-Driven Intelligence
- Store Data Into CSV File Using Python Tkinter GUI Library
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- SQL Joins Explained: A Complete Guide with Examples
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset