I am messing around a little bit more with Python. This is total beginner stuff. Let’s look at two kinds of loops in Python: The “for” loop and the “while” loop. We’re also going to learn a little bit about modules.
This program counts down from 30 to 0, and prints one number from that countdown every second. Here’s the whole program. It’s very short and sweet. We’ll go over it line by line.
- import time
- for count in range (30, -1, -1):
- time_a = time.time()
- time_b = time.time()
- while (time_b < time_a+1):
- time_b = time.time()
- print count
- print “BOOM”
Line 1: This tells Python to import the module “time.” There are lots of modules that have been written for Python, and these modules contain lots of code that is commonly needed when writing programs. So with modules, you don’t have to write out long blocks of code to perform those functions. The module “time” has lots of functions that, surprisingly enough, deal with time.
Line 2: This is the beginning of our “for” loop. The word “count” is a variable that we define. We could just as easily call this variable “potato” or any other word. But we use the word “count” because that’s what the variable is for: to keep track of the count. The “in range” and the numbers in parentheses tell Python how many times we want it to go through this loop. The first number, 30, is the starting number. The loop assigns this value to the variable “count.” The second number, -1, is where we want the loop to stop. The reason that we don’t put a 0 here is because Python performs the “for” loop until the conditions of the second number is met. If the variable “count” has a value of -1, the loop will not go through another cycle. The last number, -1, is the amount we want to subtract from the variable “count” for each cycle of the loop. Finally, the colon at the end tells Python to follow the instructions in the succeeding lines during each cycle of the loop. So this line basically says to Python, “For the numbers 30 to -1, subtracting 1 for each cycle of this loop, assigned to the variable ‘count,’ perform the following instructions.”
Line 3: This line is indented 4 spaces. Every line that is indented 4 spaces after a loop is set up becomes part of that loop. Now what this line does is creates a variable called “time_a” and assigns a value to it, which is the current system time. It does this with the “time.time()” statement. The “time” before the period tells Python that you want something from the “time” module that it imported on the first line. The “time()” after the period tells Python what specific function you want from that “time” module, which is the current time on the system clock, expressed in seconds from the epoch. The epoch, in Linux, was midnight on January 1, 1970. If we didn’t have the “time” module to go fetch the system clock’s time, we’d have to write a lot more code to do that. But with the “time” module imported, it makes it a lot easier to tell Python to go grab it.
Line 4: This line is also indented 4 spaces, so we know it’s also a part of the loop set up in Line 2. This line does the exact same thing as Line 3, but it creates a variable called “time_b” instead of “time_a.”
Line 5: This line is also indented 4 spaces, so it’s part of the “for” loop. Here we create a “while” loop. The fundamental difference between this loop and the “for” loop that we started earlier, is that the “for” loop has a specific number of cycles we want it to go through, whereas the “while” loop doesn’t have that. We want Python to go through as many cycles as it takes until the condition of our “while” loop have been met. So how do we set the condition? It is defined in the parentheses. We want Python to go through this loop until the variable “time_b” is no longer less than the variable “time_a” plus 1. In Lines 3 and 4, we assigned the values of “time_a” and “time_b” to be those of the system clock at whatever time the program executed the instructions on those lines. Because computers work very quickly, the values of “time_a” and “time_b” are almost exactly the same. So we know that going into this loop, the condition of the loop have not been met. Once again, the colon at the end of the line tells Python to follow the instructions after this line as part of the loop.
Line 6: This line is indented an additional 4 spaces, which tells Python that it is part of the “while” loop set up in Line 5. What’s neat about Lines 5 and 6 is that they are a loop inside a loop. So this line just assigns the current system time to the variable “time_b” again. The previous value that was assigned to “time_b” gets thrown out, and the new value takes its place. This is done by the computer very quickly, and Python then looks at the condition of our “while” loop from Line 5 again. Is the value of “time_b” still less than the value of “time_a” plus one? If it is, then the program goes through another cycle of the loop. It just keeps assigning a new value to the variable “time_b” until it is no longer less than “time_a” plus one. Over and over and over again. The program may go through this loop thousands of times in one second.
Line 7: This line is indented only 4 spaces, so Python (and we) know that it is not part of the “while” loop, but it is still part of the “for” loop. This line just tells Python to print (on the screen) the value of the variable “count.” This is the last line that is indented, so Python knows that it has reached the end of the instructions that we want it to follow during each cycle of the “for” loop. It won’t go on to the next line until it goes through the loop the number of times we have defined.
Line 8: This line is not indented, so it’s not part of any loops. Once the program has gone through all the loops, we tell it to print the word “BOOM.” And that’s the end of the program.
It’s not much, but it’s a start. I want to learn more about Python.
Related posts:



