Controlling the Flow of Your Program - Python Programming for Beginners (2015)

Python Programming for Beginners (2015)

Chapter Six: Controlling the Flow of Your Program

In this chapter, you will learn:

· What condition statements are and how they work

· How to use control flow tools in your program

Now we're on to one of the most important (and interesting) chapter in this book. This part of the program is where your program will start to chose which things to do and decide which things not to do.

In this chapter we will learn about control flow tools, which are incredibly important because they control the flow of the program. Without control flow tools the program will just simply run through every piece of code you write from beginning to end.

Condition Statements

A condition statement is a statement which checks a condition and then run a certain part of code depending on whether the condition is met or not.

The different conditions you can check are the ones below:

Are the variables the same?

x == y

Are the variables different?

X != y

Is one variable greater than the other?

x > y

Is one variable greater than or equal to the other?

x >= y

If one variable smaller than or equal to the other?

x <= y

Is one variable smaller than the other?

x < y

The statements will return true or false. You can check more than one statement by placing a logical operator between the statements. Here are the logical operators you can use in Python:

Are the two statements true?

x == y and a == b

Are at least one of the statements true?

x == y or a == b

Is the statement untrue?

not x == y

The If Statement

The if statement is a very useful statement something which is commonly used in many different programs. This statement checks if a statement is true or not and only runs the lines of code if the statement is true.

If you have coded in other programming languages then you will find that the if statement in Python is a little bit different than it is in the other languages. For a start, you don't need to use brackets (), curly brackets {} or the statement End If to define where the statement begins and ends. Instead, Python uses indentation to define the beginning and the end of the if statement instead, which makes it much easier.

The structure of an if statement is:

if first condition is met:

· do A

elif second condition is met:

· do B

else:

· do C

If you have used other programming languages you will probably already have worked out that elif means else if. If you haven't used another programming language, elif will check another condition and run the code if it it is met. You can have as many elif statements as you need.

Finally, else is the code that will run if none of the conditions are true.

Let's run an example to see how the if statement works. Type this into IDLE, then save and run it.

check_input = input(“Choose 1 or 2”)

#check the input and run different code depending on the choice

if check_input == “1”:

· print (“Sorry, you lose!”)

elif check_input == “2”:

· print (“Congratulations! You win!”)

else:

· print (“Your choice is invalid.”)

Now test the program out and run all of the responses. Have a think about the programs you use regularly, how might the if statement be used to make these programs work?

There is another kind of if statement you can use if you only need to do something simple which can fit on one line. This is called the inline if. Here is an example of how this works:

print (“Congratulations! You Win!” if check_input == 2 else “Sorry, you lose!”)

The line above will display “Congratulations! You Win!” if check_input is 2, otherwise it will display “Sorry, you lose!”

Loops

Loops are incredibly important when it comes to programming. As the name suggests, a loop will execute the same code over and over again. This is very useful for running the main part of the program over and over again.

There are different kinds of loops which you can use in Python which we will go through now.

The For Loop

The for loop will run through the code over and over again until the statement is no longer true. For example:

user_message = input (“Type in a message: “)

for a in user_message:

· print(a)

If you type this into IDLE and run the program you will find that the message will be displayed one letter per line.

The for loop is a good place to teach you about lists. Lists are lists of data stored within one variable. You declare a list using the following statement:

set_list = [“Zero”, “One”, “Two”, “Three”, “Four”, “Five]

If you then write the code print (set_list) it will display the entire list on one line. You can also choose to display one part of the list by writing the code print (set_list[3]). The number in the square brackets is the number in the list you want to display.

You can use the for loop to run through a list one by one with the following code:

for set_list_print in set_list:

· print (set_list_print)

This loop will then display each piece of data in the list one by one on each line.

The While Loop

The while loop repeats the loop while a condition is true. For example:

set_number = input(“Type in a number between 5 and 20)

#Run loop as long as the set_number is more than 0

while set_number > 0:

· print (“Number: ”, set_number)

· set_number -= 1

Now save and run the program and see what happens.

Whenever you are running a while loop, make sure that the loop is able to end at some point otherwise it will loop forever until you somehow close the program down!

Break and Continue

In some programs you may want to find a way to exit the loop or to skip past a part of the loop.

If you wish to exit the loop you need to use the word “break”. Once the program reads the break it will come out of the loop and carry on with the program. For example:

set_list = [“Zero”, “One”, “Two”, “Three”, “Four”, “Five]

num = 0

for set_list_print in set_list:

· print (num, set_list_print)

· num += 1

· if num == 3

· break

Run the program and see what happens. You will find that the program stops once the num variable hits 3, even though it would otherwise have ran until the num variable reached 5.

If you want to skip part of the loop then you use the variable “continue”, which will ignore the next piece of code and continue on with the loop. For example:

set_list = [“Zero”, “One”, “Two”, “Three”, “Four”, “Five]

num = 0

for set_list_print in set_list:

· print (num, set_list_print)

· if num == 3

· continue

· num += 1

Edit the code you typed out above with this piece of code and then run it to see what happens. You will find that the num variable only reaches 3 and then stays at the same value for the rest of the program.