Untitled document.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Loops ​ 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...

Loops ​ 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 Loops ​ The while loop is nearly universal throughout all coding languages. ​ Such a loop will repeat a block of code over and over again. In the text editor window, edit your code as follows: i=3 while i != 0: print("meow") ​ Notice how even though this code will execute print("meow") multiple times, it will never stop! It will loop forever. while loops work by repeatedly asking if the condition of the loop has been fulfilled. In this case, the compiler is asking, “does i not equal zero?” When you get stuck in a loop that executes forever, you can press control-c on your keyboard to break out of the loop. To fix this loop that lasts forever, we can edit our code as follows i=3 while i != 0: print("meow") i=i-1 ​ Notice that now our code executes properly, reducing i by 1 for each “iteration” through the loop. The term iteration has special significance within coding. By iteration, we mean one cycle through the loop. The first iteration is the “0th” iteration through the loop. The second is the “1st” iteration. In programming, we count starting with 0, then 1, then 2. We can further improve our code as follows: i=1 while i 0: break for _ in range(n): print("meow") ​ Notice how this while loop will always run (forever) until n is greater than 0. When n is greater than 0, the loop breaks. Bringing in our prior learning, we can use functions to further improve our code: def main(): meow(get_number()) def get_number(): while True: n = int(input("What's n? ")) if n > 0: return n def meow(n): for _ in range(n): print("meow") main() ​ Notice how not only did we change your code to operate in multiple functions, but we also used a return statement to return the value of n back to the main function. More About Lists ​ Consider the world of Hogwarts from the famed Harry Potter universe. ​ In the terminal, type code hogwarts.py. In the text editor, code as follows: students = ["Hermoine", "Harry", "Ron"] print(students) print(students) print(students) ​ Notice how we have a list of students with their names as above. We then print the student who is at the 0th location, “Hermoine”. Each of the other students is printed as well. Just as we illustrated previously, we can use a loop to iterate over the list. You can improve your code as follows: students = ["Hermoine", "Harry", "Ron"] for student in students: print(student) ​ Notice that for each student in the students list, it will print the student as intended. You might wonder why we did not use the _ designation as discussed prior. We choose not to do this because student is explicitly used in our code. ​ You can learn more in Python’s documentation of lists. Length ​ We can utilize len as a way of checking the length of the list called students. Imagine that you don’t simply want to print the name of the student but also their position in the list. To accomplish this, you can edit your code as follows: students = ["Hermoine", "Harry", "Ron"] for i in range(len(students)): print(i + 1, students[i]) ​ Notice how executing this code results in not only getting the position of each student plus one using i + 1, but also prints the name of each student. len allows you to dynamically see how long the list of the students is regardless of how much it grows. ​ You can learn more in Python’s documentation of len. ​

Use Quizgecko on...
Browser
Browser