Booleans and Conditionals
This lesson will teach you how to use booleans and conditional statements to create branches in code.
Just like how variables can equal an integer like 5 or a string like
Hello, they can also be equal to True or False.
Variables that equal True or False are called Booleans.
We can make our programs think by asking true or false questions based on information we provide.
We can even have some specific code run if the answer is True, and have something completely different happen if it is False.
This is called a conditional statement.
We can use many comparison operators with conditional statements to check if the condition is true or false.
|Comparison Operator||Conditional Check|
|==||Checks if two values are equal.|
|!=||Checks if two values are not equal.|
|<||Checks if a value is less than another value.|
|>||Checks if a value is greater than another value.|
|<=||Checks if a value is less than or equal to another value.|
|>=||Checks if a value is greater than or equal to another value.|
We can use the operators with if statements to check if a condition is true or false.
To do this we write if followed by the condition, then followed by a colon :.
Then we write what we want to happen if the condition is true.
x = 6 y = 10 if x == 10: print("X was equal to 10.") if y == 10: print ("Y was equal to 10.") # Output: # Y was equal to 10.
We can add another conditional statement after if-statements called elif. This can be read as else-if.
We can chain as many elif-statements as we want one after another.
An elif-statement will only be checked if the prior conditional statement is false.
x = 6 if x == 10: print("X was equal to 10.") elif x == 6: print ("X was equal to 6.") # Output: # X was equal to 6.
We can add another conditional statement called else after doing an if-statement or an elif-statement.
Else-statements do not need a condition and they will automatically run if the prior conditional statement is false.
We can only have one else-statement in a conditional block and it must always come at the end of the block.
Similarly, an if-statement must always come at the start of a conditional block.
x = 6 if x == 10: print("X was equal to 10.") elif x == 5: print ("X was equal to 5.") else: print ("X is something else.") # Output: # X is something else.
We can also use logical operators to add multiple conditions to a single conditional statement.
The logical operators are: and, or, and not.
Using the and and or operators will involve a condition on the left side of the operator and another on the right side. The not operator will only have a right-hand side condition.
|and||The if statement will only return True if both conditions connected by and are true.|
|or||The if statement will return True if either of the conditions connected by or are true.|
|not||The if statement will only return True if the condition is False.|
x = 6 if x > 4 and x < 10: print("X was between 4 and 10.") elif x > 4 and not x < 10: print ("X was greater than 4 and not less than 10.") elif x == 6: print ("X was equal to 6 exactly.") else: print ("X was something else.") # Output: # X was between 4 and 10.
Challenge: Input Drawing
- Create a new file and save it as input_drawing.py
- Use the input() function to ask the user what shape they want to draw.
- Then use conditional statements to draw the correct shape.
a. Required Shapes: Circle, Square, Triangle.
b. Use the string function lower() to convert the user input for the shape to lowercase.
This is because the == distinguishes between strings of different case.
triangleis not the same as
Using the lower() function on the user's input will ensure that you can compare it to a lower-case string regardless of how the input was typed in by the user.
For example, when the user types
CIRcle, you will only need to check for
Turtle function reminders: circle(), forward(), right()