Python Basics Series – Part 4

Intro

In part 4 of this Python Basics Series we will be exploring loops, if-then statements, and boolean operators. Loops, if-else statements, and boolean operators are essential to any great program as they introduce the ability to branch and apply logic to our code.

Code

First lets look at what Loops are and what they can do for us. Loops can be used to repeat tasks over and over again. You can have finite or infinite loops meaning that you can set a limit on how many times you would like to loop through some code or you can make it loop forever. We have two main types of loops they are for loops and while loops.

First lets look at the for loop and we begin by declaring a new variable called my_list. This variable is going to be a list data type.

Next we add a print statement to set up the proper output for our for loop. This isn’t required but for our example to make sense it was necessary. Then we start our for loop and include a few print statements inside of the for loop (notice the indentation).

The output from running this code is:

What we see is the for loop iterate through our list (my_list) to print out each color in the list. We declare a temporary variable each after the keyword for to represent each item in our list. We could have chosen any name for this temporary variable but ‘each’ is a common word that is used.

We can also use a range value to form our for loop.

In this case we are going to print out the values 1-10 inside of our for loop. The ‘i’ is the temporary variable we chose in this case. See the output below:

The other type of loop that we use is called a while loop. It is used to perform some task until a condition is reached. The condition can be set to any value including True or False commonly known as a boolean value in programming. First let’s make an infinite loop (Warning! using this can result in a program that runs forever!).

I started with the keyword while and inside of parenthesis set the condition to equal True (a boolean value) which will cause this program to run continuosly. You can stop the execution of the program by pressing Ctrl and C at the same time. Output below:
This infinite loop can be useful for programs designed to perform tasks continuosly but adding a flexible boolean condition to allow the execution to end is recommended. Let’s see what that looks like.

Here we add a count integer variable set to 0. We include a boolean statement to evaluate if the count variable is less than or equal to 10 and in each iteration of the loop the count variable increments by one. The while loop checks this condition at each iteration of the loop and if the condition is True it will continue. If it evaluates to false the loop will cease to execute. The output of running this program is shown below:

Now lets use an if-else statement to set a flag value that represents our condition. If-else statements allow us to branch our code adding logic and increasing the complexity of our code. Lets look at how we can use if-else statements to increase the functionality of our code.

Here we set a flag variable to True and include that in our while loop conditional statement (inside the parenthesis). Next we add an if statement to evaluate if the count variable is less than 10. If it is we print out the string ‘adding 1 to count’. The next line is an else statement that allows us to evaluate a different condition if count is not less than 10. If count is not less than 10 we set the value of flag to False and in the next iteration of the loop the while conditional statement is evaluated and the loop ceases to execute. See below for the output:

If-else statements can be used in any part of our program not just inside of a loop. You can also add additional branches to the if-else execution by using ‘elif’. See below:

In the second to last iteration of our loop you can see where the ‘elif’ statement is evaluated. More operators can be explored here https://www.w3schools.com/python/python_operators.asp. That is it for our Python Basics Series check back soon where we will dive deeper into Python with some more complex programming in our Python Beginner Series!

Scroll to Top