Python- Day5

Nidhi Ashtikar
4 min readJul 14, 2024

--

Loops, Ranges, and Code Blocks

Loops:

Loops are used to execute a block of code repeatedly as long as a certain condition is met. There are two main types of loops in Python: for loops and while loops.

1. for Loop

A for loop is used to iterate over a sequence (such as a list, tuple, dictionary, set, or string).

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

2. while Loop

A while loop repeatedly executes a block of code as long as a given condition is true.

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

Loop Control Statements

Python also provides control statements to manage the flow of loops:

  • break: Terminates the loop prematurely.
  • continue: Skips the rest of the code inside the loop for the current iteration and moves to the next iteration.
  • else: Executes a block of code once when the loop condition is no longer true (useful in for and while loops.

break:

for number in range(10):
if number == 5:
break
print(number)
#(start: SupportsIndex, stop: SupportsIndex, step: SupportsIndex = ..., /) -> range

for num in range (1, 10, 3):
print(num)

continue:

for number in range(10):
if number % 2 == 0:
continue
print(number)

else with for loop:

for number in range(5):
print(number)
else:
print("Loop ended")

Project 1: Average Height

student_heights =  input("What is the student heights\n").split()   

for n in range(0, len(student_heights)):
student_heights[n] = int(student_heights[n])

total_height = 0
for height in student_heights:
total_height += height
print(f"total_height = {total_height}")


number_of_student = 0
for student in student_heights:
number_of_student += 1

print(f"number of student = {number_of_student}")

average_height = round(total_height/number_of_student)
print(f"average_height = {average_height}")



>>

What is the student heights
120 130 152 258 58 56
total_height = 774
number of student = 6
average_height = 129
PS C:\Users\Hp\Desktop\Python>

Project 2: Highest Score

student_score = input("Enter the marks - ").split()

for n in range (0, len(student_score)):
student_score[n] = int(student_score[n])

highest_score = 0

for score in student_score:
if score > highest_score:
highest_score = score


print(f"The highest score is {highest_score}")


>>

Enter the marks - 25 268 258 65 2041 6228 255 2 44
The highest score is 6228

Project 3: Calculating numbers, 1+2+3+4…….

first = int(input("What is first value - "))
second = int(input("What is second value - "))


total = 0

for num in range (first, second +1):
total += num

print(f"The total is {total}.")

>>

What is first value - 1
What is second value - 50
The total is 1275.

Project 4: Calculating Even numbers, 2+4+6+8….

target = int(input(" Enter number between 1 to 100 -  "))

even_num = 0

for number in range(2, target+1, 2):
even_num += number

print(f"The total is {even_num}")



>>

Enter number between 1 to 100 - 20
The total is 110

Project 5: Fizz Buzz Game

The FizzBuzz game is a simple programming challenge often used to teach basic control structures and logic. The rules are:

  1. Print numbers from 1 to a given limit.
  2. For multiples of 3, print “Fizz” instead of the number.
  3. For multiples of 5, print “Buzz” instead of the number.
  4. For numbers that are multiples of both 3 and 5, print “FizzBuzz” instead of the number.
number =  int(input("Enter number from 1 t0 100 - "))

for num in range(1, number+1):
if num % 3 ==0 and num % 5 == 0:
print("fizzBizz")

elif num % 3 == 0 :
print("Fizz")

elif num% 5 == 0 :
print("Buzz")

else:
print(num)



>>

Enter number from 1 t0 100 - 15
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
fizzBizz

Project 6: Password Generator:

import random

print("-----Welcome to Password Generator!------")

letters = int(input("How many letter you want?\n"))
symbols = int(input("How many symbols you want?\n"))
numbers = int(input ("How many number you want?\n"))

password_list = []

# Generate letters
#You can use "+=" or ".append"

for _ in range(letters):
password_list.append(random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'))

# Generate symbols

for _ in range(symbols):
password_list += (random.choice('!@#$%^&*()-_=+[{]}\\|;:\'",<.>/?'))

# Generate numbers

for _ in range(numbers):
password_list += (random.choice('0123456789'))

# Shuffle characters

random.shuffle(password_list)

# Combine characters into password

password = ''.join(password_list)

print(f"Your password is: {password}")




>>

-----Welcome to Password Generator!------
How many letter you want?
10
How many symbols you want?
3
How many number you want?
5
Your password is: VjVHE18SWJ35d?3@^G

If you found this guide helpful then do click on 👏 the button.

Follow for more Learning like this 😊

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!

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

No responses yet