# Conditionals --- CS 65 // 2021-03-02 ## Extra Credit Activities - MathCS Seminar (Dr. Enes Akbuga) + **Topic:** Mathematics Education + **When:** Friday, 2:00pm to 3:00pm + **Where:** Zoom (email me for the link) - [Global Citizen Forum](https://www.drake.edu/diversity/initiatives/globalcitizenforum2021/) + Conference on equity and inclusion in higher ed + March 3-5 # Questions ## ...about anything? # Conditionals ## Conditionals - Conditionals allow us to ask **true/false questions** about the state of our program and based on the answer to the question, do something specific - **Is the lid of the PB still on?** + If so, call the “take off lid” subroutine before continuing - **Did the user enter bad input?** + If so, ask them to type in something not broken + Otherwise, continue with the program normally ## Conditionals - Conditionals in Python have the following form: ```py if first_condition: print("First condition was true!") elif second_condition: print("First condition was false AND second was true!") else: print("None of the above conditions were true!") ``` - The conditions must be expressions that evaluate to `True` or `False` - These are called **Boolean expressions** - The `elif` and `else` parts are optional and may be omitted # Boolean Values ## The `bool` Type - A **bool** in Python is a value that is `True` or `False` - **Purpose:** + Allow us to evaluate True/False questions and handle cases separately - **Express:** + `True`, `False` - **Operations:** + `and`, `or`, `not`, ... ## Boolean Expressions - **Boolean expressions** are expressions that eventually evaluate to a `bool` - Boolean expressions can consist of **and** and **or** operations + `x and y` + `x or y` - **Comparison operators** also return bools + `x < 3` + `y == "hello"` + `z != 5` ## Boolean Expressions - You can also use comparators on strings + What do you think `"hello` < "world" will do? - What happens if you combine operations into more complicated expressions? + (3 < 7) and (7 == 6) + (3 < 7) or (7 == 6) # Self Check # Lab Time