forked from CalebCurry/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-more-looping.py
More file actions
237 lines (167 loc) · 5.88 KB
/
Copy path07-more-looping.py
File metadata and controls
237 lines (167 loc) · 5.88 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
########## BREAK ##########
#You can break out of a loop any time using the break keyword.
#For example, if you are looking for a particular elemennt
languages = ["Python", "C++", "Java", "Perl", "C#"]
for language in languages:
if language == "Python":
print(language + " found")
break
#Things after break within same block never get hit
#Things outside of the if can still be hit if break is not hit first
for language in languages:
if language == "C++":
print(language + " found")
break
print("YOU CAN'T SEE ME!")
print("It's JOHN CENA!!!!") #executes first iteration but not 2nd
########## CONTINUE #########
#continue works in a similar way except that it will only skip the rest of the iteration.
#This may be useful if you need to continue iterating through the list
#(in this context you could be looking for duplicates or other interesting lanugages)
print("Searching for Java:")
for language in languages:
if language == "Java":
print(language + " found")
continue
print(language + ": Not What we are looking for...")
########## ELSE INSTEAD OF CONTINUE
#This same structure can be achieved with an else
print("Searching for Perl:")
for language in languages:
if language == "Perl":
print(language + " found")
else:
print(language + ": Not What we are looking for...")
########## HOW TO DO NOTHING ##########
for x in range(10):
pass
#You can do this when...uh... hmm.....why WOULD you want to do this?
#My bet is that this is the equivalent to TODO. AKA come back to it later to finish.
#This can also be used for a busy wait... Keeping processor busy.
######### ELSE WITH FOR ##########
#This one is actually new to me. You can use an else with a for.
#This will execute anytime the for loop hits the end of iterable (range or list)
for i in range(0,10):
print(i, end=" ")
else:
print("done")
#This can be useful for a failed search as an example because it will not be hit with break
for language in languages:
if language == "Alabama":
print(language + " found")
break
else:
print("Nope.")
######### While loop ##########
#Another form of loop is the while loop.
#Here is an example that counts 0-9 (including 0 and 9)
i = 0 #initialize
while(i < 10): #condition
print(i, end=" ")
i += 1 #update
print()
#customize the loop as you please! Here's how to count down by two
i = 30
while(i >= 0):
print(i, end=" ")
i -= 2
print()
########## CONVERTING WHILE TO FOR AND BACK ###########
#I think it is important to comfortably work with for and while
#Both loops need an initialization, condition, and update.
initialization = 5
stop_at = 10
increment = 1
for i in range(initialization, stop_at, increment):
print("for loop:", i)
while(initialization < stop_at):
print("while loop:", initialization)
initialization += increment
#To make them match, the update should come at the end of the while
#When passing a variable to range, the variable is not affected
#Range returns new data and initialization is left at 5.
########## ELSE WITH WHILE ##########
#The else statement for a while will execute if no break is hit.
i = 0
while(i < 10):
print(i, end=" ")
i += 1
else:
print("Else of while")
#Random other example
#Checks to see first square >
i = 0
while(i < 10):
if i**2 > 50:
print("First square big enough:", i)
break
i += 1
else:
print("None are big enough")
########## FLAG VARIABLES ##########
#You may not be comfortable with else after a loop
#The equivalent without an else would be like so:
index = -1 #keeps -1 until told otherwise
i = 0
while(i < 10):
if i**2 > 500:
index = i #change index if requirements are met. Not reached because > returns false
break
i += 1
if index > -1:
print("First square big enough:", i)
else:
print("None are big enough")
#Setting a starting value (such as -1), possibly changing it,
#and checking it afterwards creates a flag variable.
#Just a concept. Nothing new on syntax
######### "DO WHILE" loop ##########
#In other languages, there is a concept as a do-while loop.
#These loops always execute at least once.
#We can mimmick this in python easily.
i = 15
print(i) #prints i atleast once no matter what
i += 1
while (i < 10):
print(i)
i += 1
#To generalize this:
#do stuff
#condition - true to continue
#do stuff
#This structure is useful for sentinel / indefinate loops
########## Indefinate / Sentinel loops ##########
#an Indefinate loop is a loop that we do not decide how long it will run ahead of time
#The loop can be stopped, however. This makes it different than an infinate loop.
#An example may be displaying a menu numerous times
print("Do you want to continue? Y/N: ")
response = input()
while response == "Y" or response == "y":
print("Do you want to continue? Y/N: ")
response = input()
#a logical name for the variable would be 'continue' or 'in'
#however these are keyword. don't try it.
#not super common vocab but good to know...
#A sentinel value is a value used to stop a loop. In this case it is anything besides "Y" or "y"
#for programs a sentinel value is often 'q'
########## UPPER AND LOWER ##########
#We can also write our code like so
print("Do you want to continue AGAIN? Y/N: ")
response = input()
while response.lower() == "y":
print("Do you want to continue? Y/N: ")
response = input()
#This is important to understand as "Y" and "y" are not the same thing
#Overlooking this can introduce logical bugs in our software
#There is also an upper.
#We can also invoke it on constant strings
print("am i screaming?".upper())
########## checking if a string is uppercase or lowercase ##########
name = "Caleb"
if name.isupper():
print("Upper")
elif name.islower():
print("Lower")
else:
print("Mixed")
#not sure when you might need this but still good to know.