Repository navigation
Expand file tree
/
Copy pathPythonLoops.py
More file actions
52 lines (38 loc) · 1.5 KB
/
Copy pathPythonLoops.py
File metadata and controls
52 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Loops are functions which goes on and on
# for item in list_of_items:
#Do something to each item
# fruits = ["Apple", "Peach", "Pear"]
# for i in fruits:
# print(i)
# print (i+ " Pie")
# # print(fruits) would print list elements in a loop
# print(fruits)
# In the above code the i in for loop represents an variable where i in fruits means i for every data within a list i.e. i = Apple, i = Peach & i = Pear this would go on in a loop
# Loop allows us to execute the same line of code multiple times
# student_heights = input().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_students = 0
# for students in student_heights:
# number_of_students += 1
# print(f"number of students = {number_of_students}")
# average_height = round(total_height/number_of_students, 2)
# print(f"average height = {average_height}")
# range() is to generate range of func to loop through
# for number in range(a, b):
# print(number)
# for numbers in range(1, 10):
# print(numbers)
# range(start, stop, step)
# in (1, 10) it prints only uptill 1to9 and doesn't prints last number 1, 4, 7
# 3 in the below code is step which decides the size of the list which prints as
# for numbers in range(1, 10, 3):
# print(numbers)
total_number = 0
for number in range(1, 101):
total_number += number
print(total_number)