Python has a data type which can only take two different values, the boolean data type. This data type can only take either True or False as a value, and its values (so either True or False) must always be written with the first letter capitalized.
x = True # a boolean has to be typed with an uppercase letter as the first letter
y = False
z = false # this is incorrect and will give an error
Booleans are normally received by a comparison or a conditional statement rather than being written out individually.
print(2 > 3)
Output:
False
There are 2 types of operators in Python, both related to math in some way. The first batch of operators is the group of comparison operators, which are used to compare integers and/or floats with each other. The majority of them are very similar to the one in mathematics, some the exact same.
2 < 3 # less than
2 > 3 # greater than
2 <= 3 # less than or equal to
2 >= 3 # greater than or equal to
2 == 3 # equal to - Equality operator
2 != 3 # not equal to
Be careful as "=" is not the same thing as "==" in Python. With a single equals to sign, you would assign a value to something. For example, you would assign a variable a using a single equals to sign. However, if you want to compare if two things are equal or not, you would use two equals to signs (==). For example, if you want to see if variable a is equal to variable b, you would type "a ==b".
The second batch of operators is the group of logical operators. In mathematics, Basic Mathematical logics are a negation, conjunction, and disjunction. The symbolic form of mathematical logic is, ‘~’ for negation ‘^’ for conjunction and ‘ v ‘ for disjunction. Just like how these operators in mathematics have a truth table, the logical operators of Python also have a truth table and function as specified below:
not: Turns whatever boolean or condition is at hand into the opposite of what it is.
if a = True, not a = False
and: Used with at least two booleans and coditions at hand. If both of the conditions are true, expressions using the "and" keyword will also ouput True. Otherwise, it will always output False.
or: Used with at least two booleans and coditions at hand. If both of the conditions are False, expressions using the "or" keyword will also ouput False. Otherwise, it will always output True.
All of these operators can be put together into the same line and are demonstrated below:
# not - Turns the condition into the opposite of what it is
a = not 3 > 1
print(f"a = {a}") #The output is "a = False"!
b = not 3 < 1
print(f"b = {b}") #The output is "b = True"!
# and - If both of the conditions are true it is true, otherwise it is false
c = 4
d = 2
c > 5 and d > 1
#The output is not True!
c > 3 and d > 1:
#The output is True!
not (c > 10 and d > 1): # if we had not placed the brackets, the not would only apply to c > 10
#The output is True!
# or - If both of the conditions are false it is false, otherwise it is true
e = 4
f = 2
e > 5 or f > 1:
#The output is True!
if not (e > 5 or f > 4):
#The output is True!
The key which truly unlocks the magic for booleans and operators are if statements and logic. If statements allows us to give conditions to our code and gives us control over what is excecuted and under what conditions. If statements can consist of just one statement, which will be either True or False, or several statements where we would need to use else and elif.
The general syntax for if statements is as follows:
if condition: #only runs if the condition is true
do this
Therefore, we can programme situations which only happen when certain conditions are True and have been completed. For example, below there is a simple if statement.
if True:
print("This is how an if statement works.")
If we have multiple conditions at hand, we would use the else, and if necessary, elif keywords. If the condition under the "if" keyword is False, Python will inspect the condition under the "elif" keyword (if there are mutliple "elif" keywords, it will first check the first one) and complete the command of that keyword if the condition of it is True. In cases where both the condition of the "if" keyword and the conditions of the "elif" keywords are false, the command of the "else" keyword will be executed. Here is an example so you can better comprehend the situation.
if 2 > 3:
print("2 is greater than 3")
else:
print("2 is not greater than 3")
Here, Python will first check if the condition under the "if" keyword, so 2 > 3 is True or not. Since it isn't, the command beneath it will not execute and it will execute the command beneath the "else" keyword.
Output:
"2 is not greater than 3"
Here is a situation which uses the "elif" keyword as well:
z = 250
t = 250
if z > t:
print("z is greater than t")
elif z == t:
print("z and t are equal")
else:
print("z is not greater than t")
Here, Python will first see if the condition under the "if" keyword is True or not. Considering it isn't, it will move onto checking the condition under the "elif" keyword. Since z and t are equal and that's what the condition states, the command beneath the "elif" is executed without having to look at the else.
Step 1: Receive the user's height and age details and store them seperately in different variables.
Step 2: Make sure the age is not below 8 or the height is not below 140 cm, if both are fine, wish the user a pleasant ride.
Step 3: If either the age or the height is not fine, ask if they have a gaurdian or not. If they do, ask for the gaurdian's height and store it in a variable.
Step 4: If the gaurdian's height's variable's value is at least 140 cm, wish the user a good ride.
Step 5: If the user doesn't have a gaurdian or has a gaurdian whose height is below 140 cm, ask if they would like staff assistance and store their answer in a variable.
Step 6: If they accept the staff assistance wish them a nice ride.
Step 7: If they do not accept the staff assistance tell them they cannot go on the ride.
Please note that this is just a suggested answer and any code which completes the same function is valid. To see which job is most profitable, you must try this code with all of the jobs in the project scenario.
age = int(input("Please enter your age: "))
height = int(input("Please enter your height in cm: "))
if age < 8 or height < 140:
print("Sorry, but you cannot go on the ride alone.")
gaurdian = input("Do you have a guardian accompanying you? ").lower()
if gaurdian == "yes":
gaurdian_height = int(input("What is the height of your gaurdian in cm? "))
if gaurdian_height > 140:
print("Enjoy the ride!")
else:
staff_member = input("You are still not eligible to ride, would you like staff member assistance? ")
if staff_member == "yes":
print("Enjoy the ride!")
else:
print("Sorry, but you cannot go on the ride alone.")
else:
staff_member = input("You are still not eligible to ride, would you like staff member assistance? ")
if staff_member == "yes":
print("Enjoy the ride!")
else:
print("Sorry, but you cannot go on the ride alone.")
else:
print("Enjoy the ride!")
You have reached the end of Section 4: Python Conditional Flow and Logic!