-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathloops.py
More file actions
119 lines (100 loc) · 2.55 KB
/
loops.py
File metadata and controls
119 lines (100 loc) · 2.55 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#notes from python training
#dictionaries
ages = {'kevin': 59, 'bob': 40, 'kayla': 21}
for key in ages:
print(key)
#conditionals in loops
#keys and values tuples
for key, value in ages.items():
print(key, value)
#string
for letter in 'my_string':
print(letter)
#while loop
counter = 1
while counter <= 25:
if counter % 4 == 0:
print(counter)
counter +=1
#continue and break statements
count = 0
while count < 10:
if count % 2 == 0:
count += 1
continue
#fstring
print(f"We're counting odd numbers: {count}")
count += 1
#calling count, then +1, then put the output value into count
#break statement
count = 1
while count < 10:
if count % 2 == 0:
break
#break stops the execution
print(f"We're counting odd numbers: {count}")
count += 1
#continue and break statements
colors = ['blue', 'green', 'red', 'purple']
for color in colors:
if color == 'blue':
continue
elif color == 'red':
break
print(color)
#while else
count = 1
while count <= 4:
print(count)
count += 1
else:
print("While loop completed")
#for loop
for i in [1, 2, 3, 4, 5]:
print(i)
else:
print("For loop completed")
#search a collection
colors = ['red', 'pink', 'blue', 'orange', 'green']
for color in colors:
if color == 'orange':
print('Orange is in the list')
break
else:
print('Orange is not in the list')
#iterate a certain number of times, range function
my_range = range(10)
print
#list range
print(list(my_range))
#full range
print(list(range(1, 14, 2)))
count = 1
while count <= 4:
print('looping')
count += 1
#using for loop
for _ in range(4):
print('looping')
#take a list and go through each item, make modification, list modifications
#list comprehentions
colors = ['red', 'blue', 'orange', 'green', 'yellow']
uppercase_colors = []
for color in colors:
uppercase_colors.append(color.upper())
print(uppercase_colors)
#list of info but want to put a for loop in - one line
colors = ['red', 'blue', 'orange', 'green', 'yellow']
uppercase_colors = [color.upper() for color in colors]
print(uppercase_colors)
#filering long version
warm_colors = []
for color in colors:
if color in ['red', 'orange', 'yellow']:
warm_colors.append(color)
print(warm_colors)
#shorter version of filtering
colors = ['red', 'blue', 'orange', 'green', 'yellow']
warm_colors = [color for color in colors if color in ['red', 'orange', 'yellow']]
print(warm_colors)
#next up fizz buzz, see fizz buzz file