Day 5 — Python Programming — LOOPS
Loops:
Loops in Python are used to execute a block of code repeatedly.
A loop is a way to repeat code again and again until a condition is met.
Python supports two main types of loops:
for Loop
Used when you know how many times you want to repeat something.
for i in range(5):
print(i)
👉 This prints numbers from 0 to 4.while Loop
Used when you don’t know how many times, but you want to repeat while a condition is true.
i = 0
while i < 5:
print(i)
i += 1
👉 This also prints 0 to 4.Loop Controls:
break→ Stops the loop early.continue→ Skips the current step, goes to next.else→ Runs after loop ends (only if nobreakis used).
Let’s start Coding from Simple:
#Real EG -
fruits = ["apple", "banana", "cherry"]
for fruit in fruits: #Reaches each item in list
print(fruit)
#👉 Loops through the list and prints each fruit.
output >>
apple
banana
cherry
#Explanation -
#We have a list
#so for loop assign variable to each item in the list
#1. print => fruit = apple
#2. print => fruit = banana
#3. print => fruit = cherry
#List this we get out output #You can add anything to each item in the List
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("I Love " + fruit)
output>>
I Love apple
I Love banana
I Love cherryLet’s Play with number’s now:
#Want TOTAL ==> USE ==> sum()
scores = [85, 92, 78, 88, 90]
total = sum(scores)
print(total)
output>>
433#Use For Loop:
scores = [85, 92, 78, 88, 90]
total = 0
for s in scores:
total += s
print(total)
#total is set to 0 ==> But we want total of the list
#So s is each item in list
#Each time it will do ==> total = total + s
#total = 0 +85 , now total is 85
#total = 85+92 , now total is 177 ...
output >>
433#Want MAX No ==> USE ==> max()
scores = [85, 92, 78, 88, 90]
print(max(scores))
output >>
92
#HOW YOU WILL DO USING FOR LOOP ==?# Use For Loop:
scores = [85, 92, 78, 88, 90]
m = 0 # Start with m as 0
for s in scores: # Look at each number in the list
if s > m: # If this number is bigger than m
m = s # Change m to this number
# After the loop, m is the biggest number
#Explanation ==>
# Set m which was 0 to that item 85, so m is 85
# Next is 92 > m = 85 , Yes => Now m is 92
# Next is 78 > 92 , NO (It will do nothing)
# Next is 88 > 92 , NO
# Next is 90 > 92 , NO
# So m is 92 , which is max
print(m)
# output >> 92For Loops and the Range() Function:
range (a, b)
#Range with for loop
for n in range(1, 5):
print(n)
output >>
1
2
3
4
#It will not include the last number #Can add step to range function
for n in range(1, 10, 2):
print(n)
output >>
1
3
5
7
9Calculates the sum of all integers from 1 to 100 and prints the result.
#1+2+3+4+5+6+7..........t = 0
for n in range(1, 101):
t += n # t = 0 + 1 , t = 1
# t = 1 + 2 , t = 3
# t = 3 + 3 , t = 6
# t = 6 + 4 , t = 10
print(t)
output >>
5050Prints the numbers from 1 to 20, but replaces multiples of 3 with “NUM-3”, multiples of 5 with “NUM-5”, and multiples of both 3 and 5 with “3&5”
for r in range(1, 20):
if r % 3 == 0 and r % 5 == 0 :
print("3&5")
elif r % 5 == 0 :
print("NUM-5")
elif r % 3 == 0 :
print("NUM-3")
else:
print(r)
output >>
1
2
NUM-3
4
NUM-5
NUM-3
7
8
NUM-3
NUM-5
11
NUM-3
13
14
3&5
16
17
NUM-3
19PROJECT = Welcome to the Password Generator!
Simple-
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the Password Generator!")
num = int(input("How many number you want in your password ? \n"))
let = int(input("How many letters you want ? \n"))
sym = int(input("How many symbols you want ? \n"))
password = ""
for r in range(0, let): #each item in range of 0 to num user game
password += random.choice(letters) #password = password + random choice from list letters
for r in range(0, num):
password += random.choice(numbers)
for r in range(0, sym):
password += random.choice(symbols)
random.shuffle(password)
print(password)
output >>
Welcome to the PyPassword Generator!
How many number you want in your password ?
2
How many letters you want ?
5
How many symbols you want ?
1
vMoWJ10%Final —
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
let = int(input("How many letters you want ? \n"))
num = int(input("How many number you want in your password ? \n"))
sym = int(input("How many symbols you want ? \n"))
password = []
for r in range(0, let): #each item in range of 0 to num user game
password += random.choice(letters) #password = password + random choice from list letters
for r in range(0, num):
password += random.choice(numbers)
for r in range(0, sym):
password += random.choice(symbols)
random.shuffle(password) #suffling password (list)
print(password)
#turn back into string
passw = ""
for c in password:
passw += c
print(f"Your password is {passw}")
output >>
Welcome to the PyPassword Generator!
How many letters you want ?
5
How many number you want in your password ?
2
How many symbols you want ?
1
['8', 'm', 'F', 'b', 'B', 'b', '&', '8']
Your password is 8mFbBb&8If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
Get Nidhi Ashtikar’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s connect! Find me on LinkedIn.
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment — I’m here to help you explore whatever interests you!
If you’re interested in joining AWS classes, don’t hesitate to contact me at: nashtikar2@gmail.com









