This code uses the modulo operator % to get the remainder of the division of n and 3 in line 5. If n % 3 != 0: # test whether n is not divisible by 3 without remainder The following code goes through a list of numbers and prints out only those numbers that are divisible by 3 (without remainder). Of course, the next iteration is only started if, in the case of a while-loop, the condition is still true, or in the case of a for-loop, there are still remaining items in the list that we are looping through. When a continue command is encountered within the body of a loop, the current execution of the loop body is also immediately stopped, but in contrast to the break command, the execution then continues with the next iteration of the loop body. While this code is not shorter than the previous while-based version, we are only creating random numbers in one place, so it can be considered a little bit more clear. However, since we have the if-statement with the break, the execution will be terminated as soon as the random number generator rolls an 11. Python does not have a do-while loop but it can be simulated using a combination of while and break: import randomĪ while loop with the condition True will in principle run forever. Since we first need to create a random number before the condition can be tested, this example would actually be a little bit shorter and clearer using a do-while loop. If you have experience with programming languages other than Python, you may know that some languages have a "do … while" loop construct where the condition is only tested after each time the loop body has been executed so that the loop body is always executed at least once. Obviously, this version is not completely identical to the while based version from above because the loop will be executed at most 1000 times here. When the random number produced in the loop body is 11, the body of the if-statement, so the break command, will be executed and the program execution immediately leaves the loop and continues with the print statement after it. This means we can rewrite the program from above using a for-loop rather than a while-loop like this: import randomīreak # terminate loop and continue after it If the loop is part of a nested loop only the inner loop will be terminated. The break command will automatically terminate the execution of the current loop and continue with the code after it. What you may not yet know is that there are two additional commands, break and continue, that can be used in combination with either a for or a while-loop. Print('This took ' + str(attempts) + ' attempts') R = random.randrange(100) # produce random number between 0 and 99 With this knowledge, it should be pretty clear what the following code example does: import random In contrast, the while-loop has a condition that is checked before each iteration and if the condition becomes False, the loop is terminated and the code execution continues after the loop body. While in the second case, the for-loop is often used together with the range(…) function to determine how often the loop body should be executed: for i in range(50): In the first case, the for-loop typically looks like this for item in list: You should know that the for-loop is typically used when the goal is to go through a given set or list of items or do something a certain number of times. There are two kinds of loops in Python, the for-loop and the while-loop. Next, let’s quickly revisit loops in Python.
0 Comments
Leave a Reply. |