PYTHON LOOP BASIC(BEST PRACTICE SET EVER)
Python Loop Mastery:
🔹 Level 1: Basic Loop Syntax
1.for loop with range()
📌 Syntax:
for i in range(5):print(i)
🧠 Output:
0
1
2
3
42.while loop
Ex:
n = 0
while n < 5:
print(n)
n += 1
Output:
0
1
2
3
4
3.loop variables (i, j)
Basic i loop variable
📌 Syntax:
🧠 Output:
4.break, continue
🔹 Level 2: Loop on Data Structures
Loop over Tuple
🧠 Output:
🔹 Level 4: Nested Loops
1.Loop inside another loop:
#📌Syntex:for i in range(1, 4): # Outer loop for j in range(1, 4): # Inner loop print(f"Row {i}, Column {j}")🧠 Output:
Row 1, Column 1 Row 1, Column 2 Row 1, Column 3 Row 2, Column 1 Row 2, Column 2 Row 2, Column 3 Row 3, Column 1 Row 3, Column 2 Row 3, Column 3
2.Multiplication table:
🔹 Level 5: Loop Pattern:
Star pattern:
🔹 Level 6: Mathematical Loops
1.Factorial calculation:
2.Fibonacci series:
3.Prime number checker
4.Sum of digits:
5.Palindrome checker:
🔹 Level 7: Loop-Based Problem Solving
1.Find max/min from list:
2.Count vowels/consonants:
3.Reverse number or string:
4.Count digits/words:
🔹 Level 8: Advanced Optimization
1.List comprehension:
🔹 Step 4: Nested Loops
🔹 Step 5: List Comprehension with String Operations
🔹 Step 6: Dictionary Comprehension (Advanced)
1.Dictionary comprehension with if-else:
print(even_or_odd)
Output:
{1: 'ODD', 2: 'EVEN', 3: 'ODD', 4: 'EVEN', 5: 'ODD', 6: 'EVEN', 7: 'ODD', 8: 'EVEN', 9: 'ODD', 10: 'EVEN'}
2. Dictionary from two lists:
values = ['Tanvir', 22, 'Dhaka']
combined={keys[i]:values[i] for i in range(len(keys))}
print(combined)
3. Dictionary from a string (count characters):
name="Shahed Bin Idris"
count_letter={c:name.count(c) for c in name}
print(count_letter)
4. Dictionary comprehension with conditions and filtering:
5. Convert normal loop to dict comprehension
6. Invert a dictionary (value → key)
#Challenge 2:colors = { "red": "#FF0000", "green": "#00FF00", "blue": "#0000FF"}
x={value:key for key,value in colors.items()}print(x)Output:{'#FF0000': 'red', '#00FF00': 'green', '#0000FF': 'blue'}
#Challenge 2:country_codes = { "BD": 880, "US": 1, "UK": 44}x={value:key for key,value in country_codes.items()}print(x)Output:{880: 'BD', 1: 'US', 44: 'UK'}
Challenge:students = { "Shahed": "A", "Rafi": "B", "Tanim": "A", "Sami": "C", "Fahim": "B"}x={}for name,grade in students.items(): x.setdefault(grade,[]).append(name)print(x)Output:{'A': ['Shahed', 'Tanim'], 'B': ['Rafi', 'Fahim'], 'C': ['Sami']}
7. Nested dictionary comprehension (advance)
8:
Square:
🔹 Step 7: Set & Tuple Comprehension
🔹 Step 8: Real-World Practice Problems
2.Efficient looping
Using normal loop:
names=["Shahed","Sabuj","Jonayed"]
for i in range(len(names)):
print(names[i])
Using Efficent way:
names=["Shahed","Hridoy","Omar"]
length=len(names)
for j in range(length):
print(names[j])
Using normal loop:
squares=[]
for x in range(1,11):
squares.append(x**2)
print(squares)
Using efficient loop:
squares=[x**2 for x in range(1,11)]
print(squares)
Phase 5:
Using normal efficient loop:
names=["Shahed","Sabuj","Jonayed"]
length=len(names)
for name in range(length):
print(f"Roll:{name+1}:{names[name]}")
Output:
Roll:1:Shahed
Roll:2:Sabuj
Roll:3:Jonayed
Using enumerate loop:
names=["Shahed","Sabuj","Jonayed"]
for roll,name in enumerate(names,start=1):
print(f"Roll:{roll}:{name}")
Output:
Roll:1:Shahed
Roll:2:Sabuj
Roll:3:Jonayed
Phase 6:(Using zip)
Using normal efficent loop:
names=["Shahed","Sabuj","Joanyed"]
ages=[21,26,21]
length_1=len(names)
length_2=len(ages)
for detail in range(length_1):
print(f"Name:{names[detail]} | Age:{ages[detail]}")
Output:
Name:Shahed | Age:21
Name:Sabuj | Age:26
Name:Joanyed | Age:21
Using zip method:
names=["Shahed","Sabuj","Joanyed"]
ages=[21,26,21]
for name,age in zip(names,ages):
print(f"Name:{name} | Age:{age}")
Output:
Name:Shahed | Age:21
Name:Sabuj | Age:26
Name:Jonayed | Age:21
Phase 6 Challenge:
Challenge 1: Student Report
names = ["Shahed", "Sabuj", "Jonayed", "Rafi"]
scores = [85, 90, 78, 92]
for roll,(name,mark) in enumerate(zip(names,scores),start=1):
print(f"Roll:{roll} | Name:{name} | Marks:{mark} | Name length:{len(name)}")
Output:
Roll:1 | Name:Shahed | Marks:85 | Name length:6
Roll:2 | Name:Sabuj | Marks:90 | Name length:5
Roll:3 | Name:Jonayed | Marks:78 | Name length:7
Roll:4 | Name:Rafi | Marks:92 | Name length:4
Challenge 2: Price List
products = ["Apple", "Banana", "Mango", "Watermelon"]
prices = [120, 40, 100, 250]
for serial,(product,price) in enumerate(zip(products,prices),start=1):
print(f"{serial}. {product} (length:{len(product)}) - Price:{price} Taka")
Output:
1. Apple (length:5) - Price:120 Taka
2. Banana (length:6) - Price:40 Taka
3. Mango (length:5) - Price:100 Taka
4. Watermelon (length:10) - Price:250 Taka
Challenge 3: Mixed Data
countries = ["Bangladesh", "India", "Nepal", "Bhutan"]
capitals = ["Dhaka", "New Delhi", "Kathmandu", "Thimphu"]
populations = [170, 1400, 30, 0.8] # in millions
for serial,(country,capital,population) in enumerate(zip(countries,capitals,populations),start=1):
print(f"{serial}. {country} - {capital} | Population:{population} million")
Output:
1. Bangladesh - Dhaka | Population:170 million
2. India - New Delhi | Population:1400 million
3. Nepal - Kathmandu | Population:30 million
4. Bhutan - Thimphu | Population:0.8 million
Comments
Post a Comment