Skip to content

Commit ea2bbae

Browse files
Update loops.py
1 parent 5a69a4b commit ea2bbae

1 file changed

Lines changed: 147 additions & 5 deletions

File tree

fundamentals/loops.py

Lines changed: 147 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,152 @@
8282
elif press == "2": # If the user types 2 then break or exit the loop
8383
break # This breaks the loop
8484
else: # if the user types anything else other than 1 or 2 it will say Incorrect option selected and loop again until the until the user presses 2 to exit
85-
print("Incorrect option selected")
85+
print("Incorrect option selected")
86+
8687

87-
"""
88-
--------------
89-
EXAMPLE 4
90-
--------------
88+
------------------------
89+
FOR LOOP EXAMPLE 1
90+
------------------------
91+
"""
92+
# A for loop with range()
93+
# range(10) generates numbers from 0 up to (but not including) 10
94+
for counter in range(10):
95+
print(counter)
96+
97+
"""
98+
Output example:
99+
100+
0
101+
1
102+
2
103+
3
104+
4
105+
5
106+
6
107+
7
108+
8
109+
9
110+
"""
111+
112+
"""
113+
--------------
114+
EXAMPLE 2
115+
--------------
116+
"""
117+
# A for loop iterating over a list
118+
numbers = [1, 2, 3, 4, 5]
119+
120+
for item in numbers:
121+
print(item)
122+
123+
"""
124+
Output example:
125+
126+
1
127+
2
128+
3
129+
4
130+
5
131+
"""
132+
133+
"""
134+
---------------
135+
EXAMPLE 3
136+
---------------
137+
"""
138+
# A for loop iterating over a string
139+
word = "python"
140+
141+
for letter in word:
142+
print(letter)
143+
144+
"""
145+
Output example:
146+
147+
p
148+
y
149+
t
150+
h
151+
o
152+
n
153+
"""
154+
155+
"""
156+
---------------
157+
EXAMPLE 4
158+
---------------
159+
"""
160+
# Using range() with a start and stop value
161+
# range(start, stop)
162+
for num in range(5, 10):
163+
print(num)
164+
165+
"""
166+
Output example:
167+
168+
5
169+
6
170+
7
171+
8
172+
9
173+
"""
174+
175+
"""
176+
---------------
177+
EXAMPLE 5
178+
---------------
179+
"""
180+
# Using range() with a step value
181+
# range(start, stop, step)
182+
for num in range(0, 10, 2):
183+
print(num)
184+
185+
"""
186+
Output example:
187+
188+
0
189+
2
190+
4
191+
6
192+
8
193+
"""
194+
195+
"""
196+
---------------
197+
EXAMPLE 6
198+
---------------
199+
"""
200+
# Using break inside a for loop
201+
for num in range(1, 10):
202+
if num == 5:
203+
break
204+
print(num)
205+
206+
"""
207+
Output example:
208+
209+
1
210+
2
211+
3
212+
4
213+
"""
214+
215+
"""
216+
---------------
217+
EXAMPLE 7
218+
---------------
219+
"""
220+
# Using continue inside a for loop
221+
for num in range(1, 6):
222+
if num == 3:
223+
continue
224+
print(num)
225+
226+
"""
227+
Output example:
228+
229+
1
230+
2
231+
4
232+
5
91233
"""

0 commit comments

Comments
 (0)