Skip to content

Commit 6da7831

Browse files
Added explanation for loops and added a few examples, outputs and explanations in the loops.py file
1 parent f9a2e9b commit 6da7831

1 file changed

Lines changed: 74 additions & 4 deletions

File tree

basics/loops.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,76 @@
11
"""
2-
---------------------------
3-
Lets Learn loops in Python
4-
--------------------------
2+
Loops in Python
3+
---------------
4+
5+
What is loops?
6+
--------------
7+
Loops in Python are used to repeat actions
8+
They let your program do something over and over again without writing the same code many times
9+
10+
They are basically saying, “keep doing this until I tell you to stop”
11+
or “do this for every item in a list.”
512
"""
6-
print("Python loops")
13+
14+
"""
15+
There are two main types of loops in Python:
16+
---------------------------------------------
17+
18+
1. while loop, used to keep repeating something as long as a certain condition is true.
19+
2. for loop used to repeat something a set number of times or go through items in a sequence (like a list or string).
20+
-Even tho <Range> is not a type of loop we will cover it as a number generator for the for loops in this file
21+
"""
22+
"""
23+
------------------------
24+
WHILE LOOP EXAMPLE 1
25+
------------------------
26+
"""
27+
counter = 0 # The number you would like to start with gets stored in the variable counter
28+
while counter < 10: # keep repeating the code below as long as the counter is less than 10 or else it will stop when it reaches 9
29+
print(counter) # Print the starting number
30+
counter += 1 # Increment by 1 each time
31+
32+
"""
33+
Output example:
34+
35+
0
36+
1
37+
2
38+
3
39+
4
40+
5
41+
6
42+
7
43+
8
44+
9
45+
46+
"""
47+
"""
48+
--------------
49+
EXAMPLE 2
50+
--------------
51+
"""
52+
exit_program = " " # Tells the program to loop no matter what the user types
53+
while exit_program != "yes": # If the Var exit_program is not equal to "yes"
54+
print("keep looping") # The program will keep looping no matter what the user types
55+
exit_program = input("exit? ") # Once the user types the keyword "yes" the program will exit
56+
if exit_program == "yes": # Added an if statement so once the program exits it will greet the user goodbye
57+
print("goodbye") # Greets the user goodbye after typing yes and exiting the program
58+
59+
"""
60+
Output example:
61+
62+
keep looping
63+
exit? no
64+
keep looping
65+
exit? nope
66+
keep looping
67+
exit? never
68+
keep looping
69+
exit? yes
70+
goodbye
71+
"""
72+
"""
73+
-------------
74+
EXAMPLE 3
75+
-------------
76+
"""

0 commit comments

Comments
 (0)