Table of contents
Loops makes life easy. We do not need to do same thing again and again by overself once we hand over it to loop. I revised for loop concept on day 5.
For loop with python lists
fruits = ["Apple", "Peach", "Pear"]
for fruit in fruits:
print(fruit)
print(fruit + " Pie")
print(fruits)
Output
Apple
Apple Pie
Peach
Peach Pie
Pear
Pear Pie
['Apple', 'Peach', 'Pear']
In the example above loop will run equal to the number of elements we have in a list. We can also use range() function. Which tells the loop to run equal to the defined range in range function.
sum_of_number = 0
for number in range(1,101):#starts from 1 unto 101, not included 101
sum_of_number += number
print(sum_of_number)
output:
5050
The above program gives the sum of 1 to 100 numbers. We can also define the step size in range() function.
range(1, 100, 3)
Exercises
We have done 4 exercises on day 5
- Average Height
- High Score
- Adding Even Numbers
- The FizzBuzz Job Interview Question
Head over to my GitHub repository to see their codes.
Project
I have done Password Generator Project. Where i have to use for loop and random module to generate strong password. This was really interesting project. The project is in GitHub repository.