Time: 5-10 minutes
Purpose: Give you practice with using conditionals in python.
Introduction: Try to work out the following tasks in python and run them to make sure they do what you expect.
Tasks:
- Set the value of my_boolean below so that the code below will print out the statement ‘in if’
my_boolean =
if my_boolean:
print('in if')
else:
print ('in else')
- repeat the above code, but this time set the value of my_boolean so that it will print out the statement ‘in else’
- Use an if statement to create a code branch that will only be executed if a given variable value is greater than 1
- Create code that will print out ‘Greater than 1’ if a given variable value is greater than one and that will print out ‘Less than 1’ if the value is less than one and that will print out ‘One!’ if the value is equal to one
- Create code that will only be executed if a given variable value is between 0 and 10
- create code that will only be executed if a given variable value is less than 0 or greater than 10
Further learning resources:
- https://realpython.com/python-conditional-statements/
- https://www.w3schools.com/python/python_conditions.asp
A file showing a possible solution is available on github here