Skip to 0 minutes and 3 secondsNow that you have an understanding of how to make computers test for either a true or a false, you can use this in your program using "if." When a computer makes a choice, it follows this process. If a statement is true, then I will run this code. Now this is known as selection. Your job, as the programr, is to provide the statement which will be evaluated as either true or false and the code which will be performed if the statement is true. This instruction is known as an if/then. It's one of the most commonly used instructions in programming. A version of the if/then exists across all general purpose programming languages.
Skip to 0 minutes and 44 secondsNow, the syntax of an if/then in Python looks like this. If the statement is true, run this code. You will see that I use the instruction if. Between the if and the colon is the statement that I want to be evaluated. And indented after the colon is the code that I want to be run, in this case, print do this. So unless the statement is true, our program will just carry on and not print anything. Now, let's create a new program that uses an if statement. The program we're going to create is a simple one that asks the user for a phrase to talk to me. If the user enters the command "hi," the program will respond with "hello."
Skip to 1 minute and 37 secondsSo let's create a new program. I'm going to save my program as ifs.
Skip to 1 minute and 47 secondsAnd I'm going to say phrase equals input talk to me.
Skip to 1 minute and 58 secondsNow we're going to use our if statement to test whether our phrase is equal to "hi." I'm going to say, if phrase equals hi or enter a colon, we'll press Enter. We'll say print hello.
Skip to 2 minutes and 18 secondsThe indent is really important here. The indent tells Python to run this code if this phrase equals hi. Now, let's run our program and see what happens.
Skip to 2 minutes and 35 secondsWe get the message to say talk to me. I'm just going to put in "hi," and hopefully, if our if statement works, we should see the message "hello." And we get hello. Now, let's just run it again but use a different phrase. So talk to me. No.
Skip to 2 minutes and 56 secondsNothing happens. We don't see that print message. Our if statement works.
Skip to 3 minutes and 2 secondsNow, let's change your program so it always say "bye" when the program finishes. I'm going to add a line of code to the bottom of my program that says print bye. But note I haven't indented it. This line of code is not part of this if statement. Now, what should happen is when I run my program, if I say talk to me, hi, I should still get the hello message, but it should also say "bye." And actually, whatever message I put in-- talk to me-- I don't want to.
Skip to 3 minutes and 43 secondsI should always see that message that says bye. So now experiment with your own program, entering the phrase hi and different greetings and just make sure you get the results that you expect. Your program should only say hello, if you say hi, but you should always see the bye message at the end.
If it's this, then do that
Now that you have an understanding of how to make computers test whether something is true or false, you can use this in your programs using
When a computer is asked to make a choice, it carries out a process like this:
if this statement is true then I will run this code”
This is known as selection. Your job as the programmer is to provide:
- the statement which will be evaluated as either true or false, and
- the code which will be performed if the statement is true.
The instruction is known as an if–then and is one of the most commonly used structures in programming. A version of the if–then exists across all general purpose programming languages.
The syntax of an if–then in Python looks like this:
if statement == True: print("do this")
if and the
: is the code which will be evaluated, this is known as the condition, namely
statement == True.
Indented under the
: is the code which will be run if the statement is equal to
A flow chart of this if, then would look like this:
Let’s put this into practice by creating a simple program which will talk back to you when you type certain phrases. For example, if you say “hi” it will say “hello”.
If you are using Mu, follow these instructions to create a new program:
- Click on the New icon to create a new program.
- Click Save and give it the filename
If you are using Trinket, follow these instructions to create a new program:
- Create a new Python project by clicking New Trinket, Python.
- Save your project by giving it the name “ifs” and clicking Save.
Create an if statement
- Add the following code, which will use
inputto get some text from the user and store it in a variable called
phrase = input("Talk to me > ")
- Use an
ifstatement to check if the phrase typed by the user is equal to “hi”.
if phrase == "hi":
Note: remember that a double equals sign is used (==) when testing whether two things are equal.
- Indented under your
ifstatement, add the code to print out “hello”.
if phrase == "hi": print("hello")
Note: the indentation (gap) before
: your IDE may have automatically indented the cursor for you, if not you can use the TAB key to create an indent.
- Run your program and enter “hi” when prompted. You should see the message “hello”.
If you put a space before “hi”, you won’t see the message “hello”. Why do you think this is? Put a comment in the discussion with your thoughts.
- Run your program again but enter a different message. You should notice that you don’t see the “hello” message. The “hello” message will only be shown if your enter “hi”.
Next you will change your program so that it always says “bye” when the program finishes. To have code run after the
if, you need to write it without an indent, otherwise Python will still think it is part of the
- Add a print statement to the very end of your program:
Note: there is NO indent before
Your program should now look like this.
phrase = input("Talk to me > ") if phrase == "hi": print("hello") print("bye")
- Run your program a few times. Experiment by entering the phrase “hi” and then different greetings. Do you get the results you expect?
Your program should only say “hello” if you say “hi”, but it should always say “bye”.
Reflect: What real-world situations do you think could be represented by an “if–then” statement? Share your thoughts in the comments.