NEXT PAGE >
ITERATION |
WHAT IS SELECTION
Selection is the process of making a selection between options. We naturally make selections everyday in our life's, and we make selections based on information / data available to us, for example:
' If it is sunny outside then we select to wear sun glasses
Or else if it is raining then we select to take an umbrella '
In Python we can use the same selection logic with IF Statements IF, ELIF and ELSE. The command IF holds the first criteria, ELIF holds all further criteria and ELSE is used when no previous criteria are met.
' If it is sunny outside then we select to wear sun glasses
Or else if it is raining then we select to take an umbrella '
In Python we can use the same selection logic with IF Statements IF, ELIF and ELSE. The command IF holds the first criteria, ELIF holds all further criteria and ELSE is used when no previous criteria are met.
WATCH THIS VIDEO TO GUIDE YOU THROUGH THIS SECTION
SECTION 1 | IF STATEMENTS
The example below shows the IF, THEN, ELIF, ELSE being used in practice. Note that the print statements are indent (moved in by one TAB space), this tell Python that these actions are inside the IF statement and should only be run if the above criteria are met.
The code below demonstrates how to produce a simple if statement, using weather as the theme.
The code below demonstrates how to produce a simple if statement, using weather as the theme.
Why the two equals == signs in the code above? Using two equals signs is a comparator. It is the same as asking 'does it equal?'. Whereas one equals = sign, means 'make it equal to'.
SECTION 2 | IF STATEMENT STRUCTURE
It is very tempting to write an if statement using just if and not using elif and else. Remember your if statement must start with if, then any further criteria should be check with an elif (short for else if), and then finally if the data does not meet any of the criteria tested you can use else. Remember else cannot have a criteria. It is crucial to recognise the pivotal role of 'if', 'elif', and 'else'. Your 'if' statement should always commence with 'if', followed by the condition to check. To introduce additional checks after your initial 'if', use 'elif' (a contraction of 'else if'), ensuring your code explores alternative conditions sequentially. Finally, cap your condition-checking with 'else', a safety net catching all remaining cases where previous conditions fail — note, 'else' stands alone without a specified condition.
Indentation is Python’s way of assigning blocks of code to execute when the associated condition is true.
The example in the statement below uses only the IF part of the statement, whilst this does work in some situations, it is not good practice and is not very efficient for the computer to process.
Indentation is Python’s way of assigning blocks of code to execute when the associated condition is true.
The example in the statement below uses only the IF part of the statement, whilst this does work in some situations, it is not good practice and is not very efficient for the computer to process.
If you write the statement with just multiple IFs then the computer will have to read them all even if the criteria has already been met. As a general rule try not to use IF statements like they have been used in the example above. Instead use a IF,ELIF, ELSE statement this is much better because when the criteria is met the computer will finish reading the IF statement block and jump to the next part of the program.
Also note that the ELSE in the statement above will only work with the last IF, the first IF in the example ends before the second IF starts.
In some cases you might have completely separate criteria that would require to code using just IF, without ELIF and ELSE. An example of this might be monitoring a key press on the keyboard.
Also note that the ELSE in the statement above will only work with the last IF, the first IF in the example ends before the second IF starts.
In some cases you might have completely separate criteria that would require to code using just IF, without ELIF and ELSE. An example of this might be monitoring a key press on the keyboard.
SECTION 3 | USING VARIABLE IN IF STATEMENTS
Using variables in IF statements is just the same as using strings, except you do not put variables in the speech-marks. See the example below.
Note it is good practice to put the content on the right hand side of the IF statement in brackets and make sure you leave spaces so you can more clearly see/read your code.
ELEMENT |
DESCRIPTION |
EXAMPLE |
if |
Used to perform different actions based on different conditions. if: Executes a block of code if the specified condition is true. |
if age > 18: print("You are an adult.") |
elif |
Executes a block of code if the previous conditions were not true, but the current condition is true. |
if age < 13: print("You are a child.") elif age < 18: print("You are a teenager.") |
else |
Executes a block of code if no conditions are true. |
if age < 18: print("You are not an adult.") else: print("You are an adult.") |
indentation |
In Python, indentation is used to define code blocks. Unlike many other programming languages that use braces {} or other symbols to define code blocks, Python uses whitespace (tabs). Purpose: Indentation allows Python to determine which statements belong to a particular code block, such as those inside a loop or a conditional statement. This makes the code more readable and enforces a consistent structure. |
age = int(input("how old are you: ")) if age > 18: print("You are an adult.") name = input("What is your name") #In this example only the print statement is inside the if statement |
WHAT IS THE WEATHER ?
Expand on the IF statement example from above by:
1: Asking the user to input the weather
2: Adding more elif sections to cater for various user responses in your IF statement
1: Asking the user to input the weather
2: Adding more elif sections to cater for various user responses in your IF statement
DEBUGGING
The code below shows an example of an if statement using numbers as the comparison to output a student grade.
QUESTION
1: What is wrong with the code above? What output problems could it cause?
TASK
1: Fix the code above to work as expected using better IF statement structure.
1: What is wrong with the code above? What output problems could it cause?
TASK
1: Fix the code above to work as expected using better IF statement structure.
SKI TEAMS
CREATE A PROGRAM THAT DOES THE FOLLOWING:
1: Ask the user for the time it took to complete the ski course
2: IF they complete the course in less than 2 minutes then they are in team A. Output ' Congratulations - You are in Team A' 3: If they complete the course in 2 minute or more and less than 3 minutes then they are on team B. Output ' Congratulations - You are in Team B'
4: Otherwise they are on team C. Output ' Congratulations - You are in Team C'
1: Ask the user for the time it took to complete the ski course
2: IF they complete the course in less than 2 minutes then they are in team A. Output ' Congratulations - You are in Team A' 3: If they complete the course in 2 minute or more and less than 3 minutes then they are on team B. Output ' Congratulations - You are in Team B'
4: Otherwise they are on team C. Output ' Congratulations - You are in Team C'
SKI PASS
TASK: A Ski company sells lift passes and wants you to create a program that does the following
1: Ask the user if they want a day pass or week pass
2: If the user wants a day pass then ask the user how old they are
2b: If the user is less that 11 years old the the day pass is $40
2c: If the user is greater than 64 years old then the day pass is $20
2d: Otherwise the day pass is $60
3: If the user wants a week pass then ask the user how old they are
3b: If the user is less that 11 years old the the week pass is $100
3c: If the user is greater than 64 years old then the day pass is $100
3d: Otherwise the day pass is $200
4: Output the cost of the correct pass in a user friendly statement
1: Ask the user if they want a day pass or week pass
2: If the user wants a day pass then ask the user how old they are
2b: If the user is less that 11 years old the the day pass is $40
2c: If the user is greater than 64 years old then the day pass is $20
2d: Otherwise the day pass is $60
3: If the user wants a week pass then ask the user how old they are
3b: If the user is less that 11 years old the the week pass is $100
3c: If the user is greater than 64 years old then the day pass is $100
3d: Otherwise the day pass is $200
4: Output the cost of the correct pass in a user friendly statement
Question 1: Structure of If Statements
What is the correct structure of an if
statement in Python?
A: if (condition); {code}
B: if condition {code}
C: if condition: code
D: if (condition):
EXPLANATION
Correct Answer: D) if (condition):
Explanation: In Python, the correct structure of an if statement requires the condition to be followed by a colon (:). The code block under the if statement must be indented. Option D is the correct structure. Option A uses incorrect syntax with semicolons and curly braces, and option B lacks the colon.
Explanation: In Python, the correct structure of an if statement requires the condition to be followed by a colon (:). The code block under the if statement must be indented. Option D is the correct structure. Option A uses incorrect syntax with semicolons and curly braces, and option B lacks the colon.
Question 2: Importance of if-else Statements
Why is it a good practice to use if-else
instead of multiple if
statements when conditions are related?
A: It makes the code slower.
B: It reduces the readability of the code.
C: It ensures only one condition is checked if the first one is true.
D: It makes the program easier to debug.
EXPLANATION
Correct Answer: C) It ensures only one condition is checked if the first one is true.
Explanation: Using multiple if statements means that all conditions are checked, even if one is already true. The if-else structure ensures that once a condition is met, no further conditions are checked, which improves performance and makes the logic clearer.
Explanation: Using multiple if statements means that all conditions are checked, even if one is already true. The if-else structure ensures that once a condition is met, no further conditions are checked, which improves performance and makes the logic clearer.
Question 3: Nested If Statements
What is the output of the following code?
x = 10 if x > 5: if x < 15: print("x is between 5 and 15") else: print("x is greater than or equal to 15") else: print("x is less than or equal to 5")
A: x is between 5 and 15
B: x is greater than or equal to 15
C: x is less than or equal to 5
D: None of the above
EXPLANATION
Correct Answer: A) x is between 5 and 15
Explanation: The first condition x > 5 is true, so the program moves to the next if statement. Since x < 15 is also true, the output will be x is between 5 and 15. The other branches are skipped because they are not met.
Explanation: The first condition x > 5 is true, so the program moves to the next if statement. Since x < 15 is also true, the output will be x is between 5 and 15. The other branches are skipped because they are not met.
Question 4: Using elif Instead of Multiple if Statements
What is the benefit of using elif
in an if
structure?
A: It checks conditions independently like multiple
if
statements.B: It allows you to check multiple conditions but avoids checking unnecessary conditions once one is true.
C: It makes the program slower.
D: It is not recommended for Python.
EXPLANATION
Correct Answer: B) It allows you to check multiple conditions but avoids checking unnecessary conditions once one is true.
Explanation: The elif (else if) statement allows checking multiple conditions, but once a true condition is found, the program skips the remaining conditions. This helps in improving performance and organizing code better compared to using several independent if statements.
Explanation: The elif (else if) statement allows checking multiple conditions, but once a true condition is found, the program skips the remaining conditions. This helps in improving performance and organizing code better compared to using several independent if statements.
Question 5: Best Practice for Writing If Statements
Which of the following is considered best practice when writing if
statements in Python?
A: Always using multiple
if
statements instead of if-else
.B: Keeping the conditions and the code on the same line.
C: Using indentation to clearly separate code blocks within the
if
statement.D: Avoiding
else
statements whenever possible.EXPLANATION
Correct Answer: C) Using indentation to clearly separate code blocks within the if statement.
Explanation: Python relies on indentation to define code blocks. It's essential to correctly indent the code inside an if statement to make it clear which code belongs to which condition. Poor indentation can lead to syntax errors or logic issues. Keeping conditions and code on the same line (as in option B) is incorrect, and option D (avoiding else) is not best practice.
Explanation: Python relies on indentation to define code blocks. It's essential to correctly indent the code inside an if statement to make it clear which code belongs to which condition. Poor indentation can lead to syntax errors or logic issues. Keeping conditions and code on the same line (as in option B) is incorrect, and option D (avoiding else) is not best practice.