If Statements
If-Elif-Else
Programs can be made to make decisions.
This is called selection.
If-Elif-Else
This is called selection.
In Python this is achieved using:
# Selection using IF-ELIF-ELSE
if age > 30:
print('You are too old!')
elif age < 20:
print('You are too young!')
else:
print('You are the correct age.')
If-Elif-Else
Key points (do this or the code will not run!)
: (colon) at the end of the if, elif, else line
# Selection using IF-ELIF-ELSE
if age > 30:
print('You are too old!')
elif age < 20:
print('You are too young!')
else:
print('You are the correct age.')
indentation on the lines below the : for all the code that needs to happen because of the if-elif-else statement.
Comparison & Logic Operators
These are used all the time with selection
Operator | Description |
---|---|
== | equal to |
!= | not equal to |
> | greater than |
< | less than |
>= | greater or equal to |
<= | less or equal to |
Operator | Description |
---|---|
and | both conditions must be met |
or | either condition must be met |
Comparison operators
Logical operators
Example I
if num > 10:
print('This is over 10')
else:
print('This is not over 10')
Example II
if num > 10:
print('This is over 10')
elif num == 10:
print('This is equal to 10')
else:
print('This is under 10')
Example III
if num >= 10:
if num <= 20:
print('This is between 10 and 20')
else:
print('This is over 20')
else:
print('This is under 10')
# Top-Tip: Convert to lowercase
# =============================
uppercaseWord = 'PYTHON'
lowercaseWord = upperCaseWord.lower()
#or
lowercaseWord = 'PYTHON'.lower()
# Top-Tip: Convert to lowercase
# =============================
uppercaseWord = 'PYTHON'
lowercaseWord = upperCaseWord.lower()
#or
lowercaseWord = 'PYTHON'.lower()
Example IV
num = int(input("Enter a number between 10 and 20: "))
if num >= 10 and num <= 20:
print('Thank you')
else:
print('Out of range')
Example V
num = int(input("Enter an even number between 1 and 5: "))
if num == 2 or num == 4:
print('Thank you')
else:
print('Incorrect')
Tasks I
- Ask for two numbers. If the first one is larger than the second, display the second number first and then the first number, otherwise show the first number first and then the second.
- Ask the user to enter a number that is under 20. If they enter a number that is 20 or more, display the message "Too high", otherwise display "Thank you"
- Ask the user to enter a number between 10 and 20 (inclusive). If they enter a number within this range, display the message "Thank you", otherwise display the message "Incorrect answer".
- Ask the user to enter their favourite colour. If they enter "red", "RED" or "Red" display the message "I like red too", otherwise display the message "I don't like [colour], I prefer red".
Tasks II
- Ask the user if it is raining and convert their answer to lower case so it doesn't matter what case they type it in. If they answer "yes" ask if it is windy. If they answer "yes" to this second question, display the answer "It is too windy for an umbrella", otherwise display the message "Take an umbrella". If they did not answer yes to the first question, display the answer "Enjoy your day".
- Ask the user's age. If they are 18 or over, display the message "You can vote", if they are aged 17. display the message "You can learn to drive", if they are 16, display the message "You can buy a lottery ticket", if they are under 16, display the message "You can go Trick-or-Treating"
Tasks III
- Ask the user to enter a number. If it is under 10, display the message "Too low", if their number is between 10 and 20, display "Correct", otherwise display "Too high".
- Ask the user to enter 1, 2 or 3. If they enter a 1, display the message "Thank you", if they enter a 2, display "Well done", if they enter a 3, display "Correct". If they enter anything else, display "Error message".
B Python IF Statements
By David James
B Python IF Statements
Python - if-elif-else
- 875