Intro
Selection is an important concept to learn, it allows you to add logic into your program. In Python selections are constructed by using “if statements”. This tutorial will provide the essential knowledge required to add selection into your Python programs.
What Is Selection?
Expanding on what was said in the intro, selection allows you to add logic into your application. This allows you to create programs that will react differently depending on the inputs received.
Operators
Operators are used to adjust how these “decisions” are made. They can be: equality, greater than, less than, etc. Operators in Python can also be combined with boolean logic such as “AND”, “OR” and “NOT”. This allows for complex decisions to be formed.
Common Operators
Here’s some of the common operators available in Python:
Operator | Meaning |
---|---|
== | Equal |
!= | Not Equal |
> | More Than |
< | Less Than |
>= | More Than Or Equal |
<= | Less Than Or Equal |
Example
As a recap of boolean logic, here’s a pseudocode example:
|
|
Now converting it into Python code:
|
|
Running the code we should get “True” as an output.
“if”
As mentioned in the intro; Python handles selection through “if statements”. Now let’s see an example Python program that uses if statements.
|
|
Have a guess at what you think might happen?
View Answer
When run, this code should generate the output shown below.
|
|
Let’s take a look at the first if statement:
|
|
- The first line tells Python we are creating a if statement.
- Have a look at the structure
- We start the statement by using the “if” keyword
- Then we have our decision (using an operator)
- Then we end the line with a colon
- To tell Python which section of code should be run when the decision is TRUE we indent the code after the statement.
You may notice that every if statement is checked, what happens if we want to match the first ’truthy’ statement and skip the rest? Let’s move on to the next section and see.
“elif”
To run the first matching expression and skip the rest, an “elif” statement can be used.
|
|
Have a guess at what you think might happen?
View Answer
When run, this code should generate the output shown below.
|
|
In Python we can put “elif” expressions after an “if”. We can add as many as we want after the initial expression, as seen in this example it has three possible decisions but only one will run.
But what happens if we want a “catch all” decision, well Python can do this too! See the next section.
“else”
To add a “catch all” in Python we can add a “else” expression to our code.
|
|
What do you think happens?
View Answer
When run, this code should generate the output shown below.
|
|
Using an “else” expression we ensure that our decisions always have a fallback, meaning if all of the selections result in FALSE, it will run anything in our “else” block.
We can use an “else” expression as the last decision. That means we have to at least defined an “if” block, we can put any “elif” expressions in-between; as shown in the example code above.
Multiple Operators
All of the examples shown so far have only had one operator per expression, but what happens if we want to have two decisions resulting in one outcome. Well we chain operators using boolean logic.
TIP: “and” and “or” are not the only keywords that can be used.
|
|
Take a moment and think what should happen in this code?
View Answer
When run, this code should generate the output shown below.
|
|
As we can see our “if” statement will only run when both decisions are TRUE. This is because we have chained two operators with an “and”.
Simple Calculator
Now we can put our knowledge into use with a practical project, building a calculator that can perform addition and subtraction depending on the users decision.
Here’s a basic calculator that currently performs only addition, expand it to add subtraction as well (created in a previous tutorial.
|
|
Follow these steps as a guide for what we need our program to do:
- get two numbers from the user as separate variables
- convert both to float or int data-types
- ask user what operation to use (+ or -)
- if +, add them together and store in answer variable, then skip to step 7
- else if -, subtract them and store in answer variable
- else, show a warning
- output the answer
Try to code your own version before looking at the completed code.
Completed Code
This code may look different to yours’. Don’t worry for now, check if your program functions first by moving on to the testing section.
|
|
Testing Our Calculator
Lets test our application with numbers 14 and 20 and the operator “+”, this should give the answer of 34.
|
|
Now we should test whether our error handling functions, so lets use 20 and 5 and the operator “*”.
We should get the below output if our error message works.
|
|
Have a go and test your calculator with more numbers using both addition and subtraction, to ensure it works as expected.
Next Steps
How about you expand the calculator and add multiplication and division into the program. This will reinforce your knowledge of selection in Python and allow you to use your calculator to aid you in the future.