## DEV Community is a community of 700,720 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# #100 days of Python, Day 5 Vishnubhotla V D V Bharadwaj
Techno Brat, Love to Recount the new Tech !!

Today I learned about For Loops, Range and Code Blocks. At the end of the day, we create a Password generator that asks the number of letters, symbols, and letters you want in the password.

Loops : Things that happen over, over, and over.

For loops : It allows us to execute the same line of code multiple times. Indentation is really important in loops.

Normally, For loops are performed in two cases. One is using lists and the other is using range function. First, we solve programs using lists.

Let's do an Average height calculator exercise. Here first we calculate the overall sum of heights of the students and get the count of the students. Then simply by dividing them, we get the average height of the students.

``````student_heights = input("Enter a list of student heights").split()
for n in range(0, len(student_heights)):
student_heights[n] = int(student_heights[n])
print(student_heights)
sums = 0
count = 0
for i in student_heights:
sums += i
count+= 1
print(sums)
print(count)
average_height = round(sums/count)
print(f'Average height of students is {average_height}')

``````

A simple Highest score exercise. It checks each element one by one and if the n+1 element is greater than n then (n+1)th element gets the maximum tag and the loop continues till the last element.

``````student_scores = input("Enter a list of student scores").split()
for n in range(0, len(student_scores)):
student_scores[n] = int(student_scores[n])
print(student_scores)
maxim = 0
i =1
for maxi in student_scores:
if maxi > maxim:
maxim = maxi
i +=1
print(maxi)

``````

range() used when you want to generate a range of numbers. Where it includes the starting index excluding the last index.

For example, if range(1,100) then 1 is considered but only up to 99.

Adding all the numbers from 1 to 100 :

``````sums = 0
for i in range(1,101):
sums+=i
print(sums)

``````

Adding even numbers from 1 to 100:

``````sums = 0
for i in range(1,101):
if i%2 == 0:
sums+= i
print(sums)

``````

FizzBuzz problem: If divided by 3 prints "Fizz", if divided by 5 prints "Buzz", if it is divided by 5 and 3 then it prints "FizzBuzz".

``````for i in range(1,101):
if i%5 == 0 and i %3 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i %5 == 0:
print("Buzz")
else:
print(i)

``````

Ok, that's the end of the day and it's time to create our password generator program. We will do it in two ways.

Creating a simple one as it follows the input order.

``````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 = ['!', '#', '\$', '%', '&', '(', ')', '*', '+']

nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

for char in range(1, nr_letters +1):
random_char = random.choice(letters)
for num in range(1, nr_numbers +1):
random_num = random.choice(numbers)
for sym in range(1, nr_symbols +1):
random_sym = random.choice(symbols)

``````

The above code output just follows the input order.

``````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 = ['!', '#', '\$', '%', '&', '(', ')', '*', '+']

nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

for char in range(1, nr_letters +1):
random_char = random.choice(letters)
for num in range(1, nr_numbers +1):
random_num = random.choice(numbers)
for sym in range(1, nr_symbols +1):
random_sym = random.choice(symbols)