-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.py
More file actions
84 lines (67 loc) · 1.81 KB
/
loops.py
File metadata and controls
84 lines (67 loc) · 1.81 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
# Traditional loop
'''
n = 5
while n > 0:
print('{}{}'.format('This is a while loop and value of the iterator is::', n))
n = n - 1
print('End of loop')
'''
# Infinite loop
'''
while True:
text = input('Enter text')
if text == '#':
print("Exiting loop due to the # character")
break
'''
# for loop with numbers
'''for i in [1, 10, 78, 2, 5]:
print('{}{}'.format("current number is::", i))
# for loop with largest numbers.
i = [9, 41, 12, 3, 74, 15]
maxNumber = max(i)
print("{}{}".format("max number is:", maxNumber))'''
# largest so far by looping.
i = [9, 41, 12, 3, 74, 15]
largestSoFar = 0
for number in i:
if number > largestSoFar:
largestSoFar = number
print("{}{}".format("largest so far is::", largestSoFar))
# finding a number with boolean variable
i = [9, 41, 12, 3, 74, 15]
found = False
foundNumber = 0
for number in i:
if number == 15:
found = True
foundNumber = number
break
print("{}{}".format("Number found and its::", number))
# for loop with Strings.
'''
listOfStrings = ['Joseph', 'Sam', 'Kranty', 'Riddi']
for names in listOfStrings:
print("{}{}".format("name is::", names))'''
# Loop with continue
'''
while True:
text = input('Enter text')
if text == 'exit':
print('Exiting loop')
break
if text == 'break':
print('To top of the loop')
continue
print("done !!")'''
listOfNames = ["name1", "name2", "name3", "name4"]
for names in listOfNames:
print("names are::{}".format(names))
# dictionaries with conditional statements.
alien = {'colour': 'red', 'x-coords': 12, 'y-coords': '19', 'height': '5 feet 4 inches'}
if alien['colour'] == 'green':
print('Alien is cool')
elif alien['colour'] == 'blue':
print('It\'s not cool')
else:
print('can\'t determine the nature of alien')