Week 2 Loops - CS50's Introduction to Programming with Python
Essentially, loops are a way to do something over and over again.
Begin by typing code cat.py
in the terminal window.
In the text editor, begin with the following code:
print("meow") print("meow") print("meow")
Running this code by typing python cat.py
, you’ll notice that the program meows three times.
In developing as a programmer, you want to consider how one could improve areas of one’s code where one types the same thing over and over again. Imagine where one might want to “meow” 500 times. Would it be logical to type that same expression of print("meow")
over and over again?
Loops enable you to create a block of code that executes over and over again.
while
loop is nearly universal throughout all coding languages.