Loops are a common programming convention that repeats a number of commands over and over. A Python “while” loop can continue as long as a stated condition is true. A “for” loop repeats a series of commands a specified number of times. In this video, Adam demonstrates how to use each type of loop in Python.
- [Instructor] There are two forms of looping statements that you can write with Python and these are useful for executing the same tasks repeatedly. They are known as while loops and for loops. Let's take a look at each one. The while loop is used when you want to execute a block of code as long as a condition remains true. I'm going to start by creating a variable called A and I'm going to set its initial value to one. Then we'll start the while loop and say that while A is less than 10, and this sets up the condition. So as long as the value of A is less than 10 then the following lines will execute. We move into the loop block with a colon and then indent the next lines two spaces. So as long as A is less than 10, I want to print the current value of A to the messages window. Then I want to increase the value of A by one. We can do that on the next slide by typing in A is equal to A plus one. And that's all there is to the while loop. Python doesn't use the same begin and end statements that you would use when writing a while loop in Transact-SQL and the entire block is defined strictly by the indenting of these two lines. So make sure that you insert the same number of spaces for each one. It doesn't necessarily have to be two spaces I have here at the beginning of each of these lines. It could be three or four. It doesn't really matter as long as these lines are indented the same. If I make one of these lines indented four spaces and leave the other one set to two spaces, then if I try and execute this, you're going to see what happens. I'll get this error message. And in fact, if you read through the output message you're going to see specifically that it's an indentation error. So let me go ahead and fix the indentation and run it again. This time the code is successfully run and I get the output numbers one through nine. Once the value of A was equal to 10, the while condition is no longer true. A is not less than 10. So the loop exits and the print and increment lines are no longer executed. So that's a while loop. The other loop that I want to take a look at is called a for loop. This method is used when you want a block of code to execute a fixed number of times. To do this, you'll use an iterating variable, which you can name whatever you want. I'm just going to say for and the variable X. This variable automatically start at a value of zero and increment each time the loop runs. Then as long as this variable is in range of zero and let's say three, the loop will execute. Just like with the while loop, you enter the block with a colon and then indent all of the following lines in the block the same amount. For this I'm simply going to print the text, Hello World. I'll press the execute button and take a look at the results. And it looks like I forgot the closing parenthese here in my print command. I'll execute it again. And this time it runs and I get the text Hello World repeated three times. So by this you can tell that the range specified up above is not inclusive. The for loop repeats as long as the iterating variable's value is equal to the first number but less than the second. We can see this clearer by printing the current value of X instead of the text, Hello World. I'll change it to say print X and then execute again. This shows that the first time through the loop the value of X was zero. Then it was one, then two. Then X is no longer in the specified range so the loop stops. Another thing that we could do with a for loop is to iterate over the number of items in a group rather than within a numeric range. So instead of the range zero to three, I could do something a little different. Instead, I'll specify a range of in the text, Hello World. Now X represents each letter in the text string. And when I execute the statement, it returns each letter in sequence. Or I can provide a list of Python items. Python uses square brackets to denote a list. Let's go ahead and get rid of this text, Hello World, here and I'll type in a list of colors. We'll do that by opening up a square bracket and then each color name wrapped in double quotation marks. I'll separate each list item with a comma. At the end of the list, I'll close the square bracket. So now I have a list with four colors: red, blue, green, and purple. I'm going to iterate over this list and print out each list item in sequence. I'll press the execute button and I'll get the results down below. So those are the basics of using loops in your Python scripts. The while loop is useful when you want to test a condition each time and repeat the loop as long as the condition remains true. And the for loop will repeat a block of code a specific number of times.
- Analyzing SQL Server data with Python
- Installing Machine Learning Services
- Writing Python scripts for SQL Server
- Python packages and libraries
- Producing graphics with Matplotlib
- Processing tabular data
- Creating a SQL stored procedure
- Creating an external data science client