Basic Python - Flow Control


Learn how to configure Python flow control to better control scripting in your project.

Video recorded using: Ignition 8.0


(open in window)

[00:00] Flow control allows you to determine what you code does by only running certain parts of the code, when certain conditions are met. One of the simplest ways we do this is with an if statement. You may already be familiar with if then, else then logic. Python's if statements work in a similar way. Here on line 86, I have some pseudo code of a simple if statement so we can learn how they work and what their syntax looks like. Python is known for being very human readable, and you can start to see that in the if statements. If the condition is true, then some code runs on line 87, and then it continues on, running more code on line 88. If the condition was not true, then it would actually skip over the code on line 87, and instead go straight to running more code on line 88. Notice that the if statement starts out with the word if, followed by a condition. At the end of the condition, we then need to place a colon, this signifies that what follows will be within our if statement. Then each line of code that is going to be run if the condition is true needs to be indented. To return to running code normally, instead of based on the condition, simply unindent the code like we have done here. Now you can also add an else to your code, like I've done here on line 93. Notice how the else doesn't check a condition, rather its code is run when the if condition is false, so if my condition on line 91 is false it would instead run the code underneath the else on line 94 before running more code on line 95. Finally, you can add multiple conditions to your if statement using the elif, also known as else if, like I have on line 100. The logic works the same as it did before, checking the condition and only running the code if the condition is true. Once it runs some code under one of the conditions, or the else statement, it then moves on to running more code on line 104. You can add as many conditions as you want to a logical if block, by simply adding more elifs. Let's take a look at the example I have down below. I have some simple logic, and two variables, X and Y. If I run the code right now, with Y being greater than X, then the condition on line 112 is true. So it will run that code, skip over the other conditions, and then continuing on at line 118. We can execute the code to double check, and make sure we get the right output. If I were to change one of the values, say making the two variables equal to one another, I can then execute that code again and you'll notice I get a different set of outputs. Another helpful flow control method is the for loop, the for loop allows us to loop through a block of code multiple times, one after another. I have some more pseudo code here, so we can learn how the for loop works. Again, with Python being very human readable, we have for item in list, I want to run some code. Now again, like the if statement, I start out my for loop with the word for. Next, item is simply the name of the variable that we want to assign to each value in the list, the variable name can be anything and can be used within the for loop script to help change the script in subtle ways with each iteration. Next comes the in keyword, followed by our list, and then at the end we place a colon to signify that everything underneath and indented is going to be run as part of the for loop. Let's take a look at this in action. Here on line 130, I have a for loop that is looping through my list with values four, five, and six, notice how the item variable I have is just called var. So the first time var is going to have a value of four, because that's the first value in my list. It's then going to get printed, because that's what my for loop is doing. Let's try executing that line of code. You'll see that it starts out printing a value of four, and then continues on with the value five, and then finishes up with six. Sometimes you might not have a list but still want to run a bit of code multiple times. We can still use a for loop and also use Python's built-in range function, which will build a list for you. Here in this example, I'm using the range function to build a list that has five items in it, because I want to run through this bit of code five times. We can try executing this code, and you'll notice I get my output printed five times. There are two things to note here. First, you don't have to use that variable within your loop script. Second, it's really easy to change how many times this script loops through, by changing the value within the range function. The last thing that I want to take a look at here is user defined functions. User defined functions allow you to define some bit of code with a function name, and then run that bit of code later by calling the name of the function. You can think of them like a template within a script. Functions are defined using the def keyword, and then the name of the function. In my case, I've made a function here called myFunc. Parameters can also be placed within parenthesis to signify that a value is going to be passed into this function, and the function can then use that value in some way. Just like with our flow control, we finish off the line with a colon, and then every line of code that lies within the function needs to be indented underneath it. Here, my function definition simple takes that variable, adds five to it, and then prints it out. You can see down below, I am then calling my function twice. First, passing in a value of five. And then again, passing in a value of ten. You'll notice when I execute this code I get the two different values outputted. Functions and flow control are very useful tools in any scripts, and will be very useful when we tackle data sets in the next lesson.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.


Share this video