-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson7.py
More file actions
92 lines (79 loc) · 1.92 KB
/
lesson7.py
File metadata and controls
92 lines (79 loc) · 1.92 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
""" Python For Loop """
"""
The for loop in Python is used to iterate the statements or a part of the program several times.
It is frequently used to traverse the data structures like list, tuple, or dictionary.
"""
# iterating through a string
str="Python"
for i in str:
print(i)
name = "Captain"
for i in name:
print(i, end='')
# print the multiplication table of a given number
print("\n")
list = [1,2,3,4,5,6,7,8,9,10]
n=6
for i in list:
table=n*i
print(table)
# print sum of a given list
list = [10,20,34,92,54,76,89]
sum = 0
for i in list:
sum = sum+i
# print("The list total is : ", sum)
print("The list total is : ", sum)
""" For Loop using range() function """
"""
syntax - range(start,stop,step size)
"""
print("")
print(" --- range() --- ")
# print numbers in sequence
for i in range(10):
print(i, end='')
# print table of a given number
for i in range(1,10):
c=n*i
print(n, "*", i,"=",c)
# print even numbers using step size
print("Even numbers")
n=20
for i in range(2,n,2):
print(i)
# print odd numbers using step size
print("Odd numbers")
n=20
for i in range(1,n,2):
print(i, end=',')
""" Using the len() function in range() function """
print("")
print(" --- range() with len() --- ")
l = ['Justine', 'Captain', 'Mickey', 'Nick', 'Manuu']
for i in range(len(l)):
print("Hello ", l[i])
""" Nested For Loop """
print(" --- Nested For Loop --- ")
rows = 5 # user input for rows
for i in range(0,rows+1): # outer loop prints number of rows
for j in range(i):
print("*", end='')
print()
r=10
for i in range(0,r+1):
for j in range(i):
print(i, end='')
print()
""" For Loop with Else statement """
print(" --- For loop with else statement ")
for i in range(0,5):
print(i,)
else:
print("End of loop")
for i in range(0,10):
print(i)
break;
else:
print("For loop exhausted")
print("The loop is broken because break statement... came out of loop")