(Day 05) Task : Python Loops & Password Generator Task :-

Table of contents
- Loops in Python :-
- Syntax of a for loop :-
- Why use a for loop?
- for loops and the range() Function:-
- range() can be defined in three parts :-
- Note :-
- Project :- You are going to write a program that automatically prints the solution to the FizzBuzz game. These are the rules of the FizzBuzz game: Your program should print each number from 1 to 100 in turn and include number 100. But when the number is divisible by 3 then instead of printing the number it should print "Fizz". When the number is divisible by 5, then instead of printing the number it should print "Buzz". And if the number is divisible by both 3 and 5 e.g. 15 then instead of the number it should print "FizzBuzz"
- Password Generator :-

Loops in Python :-
For
Loop :-
- A
for
loop is used to repeat a block of code for each item in a sequence (like a list, string, or range of numbers).
Syntax of a for
loop :-
for item in sequence:
# do something with item
item
: A variable name you choose (likefruit
,number
, etc.)sequence
: A list, string, range, etc.The code inside the loop runs once for each item in the sequence.
Why use a for
loop?
When you want to do something multiple times, especially for every item in a group.
Example:
Print every fruit in a basket, one by one.
fruits = ["apple", "peach", "banana"] for fruit in fruits: # Process each fruit here print(fruit)
Here it’s assigning the values of fruits to a variable fruit.
for
loops and the range()
Function:-
When you use
for
loops with therange()
function, you're telling Python:“Hey, repeat this code a certain number of times.”
What does range()
do?
The
range()
function generates a sequence of numbers.range(start, stop, step)
start
: The number to begin from (default is 0).stop
: The number to stop before.step
: How much to increment each time (default is 1).
range()
can be defined in three parts :-
Use
for i in range(n)
to repeat code n times :-
Do something n times. It's a simple way to repeat a block of code again and again.
range(n)
creates a list of numbers from0
ton-1
Note :- Remember
range()
excludes the stop number.Example :-
for i in range(5): print(i) # Output : 0, 1, 2, 3, 4
This prints numbers:
0, 1, 2, 3, 4
Starts from 0 by default, ends before 5 (as it excludes the stop number).
Use
range(start, stop)
to control where it begins and ends :-
“Start counting from
start
and stop beforestop
".You're telling Python exactly where to begin and where to end the loop.
start
→ the number where the loop startsstop
→ the number where the loop ends (but not included) ❌The loop runs from
start
tostop - 1
.Example :-
Print roll numbers from 1 to 30:
for roll_no in range(1, 31): print(f"Roll Number: {roll_no}")
Use
range(start, stop, step)
for custom steps :-“Start from
start
, stop beforestop
, and jump bystep
each time.”You're not just telling Python where to start and stop, but also how big each jump (step) should be.
start
→ where the loop begins.stop
→ where it stops (but not included).step
→ how much to increase or decrease each time.Example :-
for i in range(2, 11, 2): print(i) # Output :- 2 4 6 8 10
Note :-
You can use positive steps (count up).
Or negative steps (count down).
If
step
is not given, it defaults to1
.
Project :- You are going to write a program that automatically prints the solution to the FizzBuzz game. These are the rules of the FizzBuzz game: Your program should print each number from 1 to 100 in turn and include number 100. But when the number is divisible by 3 then instead of printing the number it should print "Fizz". When the number is divisible by 5, then instead of printing the number it should print "Buzz". And if the number is divisible by both 3 and 5 e.g. 15 then instead of the number it should print "FizzBuzz"
# FizzBuzz from 1 to 100
for number in range(1, 101): # Includes 100
if number % 3 == 0 and number % 5 == 0:
print("FizzBuzz") # Divisible by both 3 and 5
elif number % 3 == 0:
print("Fizz") # Divisible by 3 only
elif number % 5 == 0:
print("Buzz") # Divisible by 5 only
else:
print(number) # Not divisible by 3 or 5
Password Generator :-
Subscribe to my newsletter
Read articles from Aditya Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Aditya Sharma
Aditya Sharma
DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS