Programs can be made to make decisions.
This is called selection.
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.')
: (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.
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 |
if num > 10:
print('This is over 10')
else:
print('This is not over 10')
if num > 10:
print('This is over 10')
elif num == 10:
print('This is equal to 10')
else:
print('This is under 10')
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()
num = int(input("Enter a number between 10 and 20: "))
if num >= 10 and num <= 20:
print('Thank you')
else:
print('Out of range')
num = int(input("Enter an even number between 1 and 5: "))
if num == 2 or num == 4:
print('Thank you')
else:
print('Incorrect')