|
| 1 | +--- |
| 2 | +layout: intro |
| 3 | +theme: neversink |
| 4 | +color: bowdoin |
| 5 | +routerMode: hash |
| 6 | +favicon: https://avatars.githubusercontent.com/u/9260792 |
| 7 | +--- |
| 8 | + |
| 9 | +## Booleans and Asking Questions <twemoji-red-question-mark /> |
| 10 | + |
| 11 | +<br> |
| 12 | + |
| 13 | +<hr><br> |
| 14 | + |
| 15 | +Christopher Martin - _Bowdoin College_ <a href="https://bowdoin.edu/" class="ns-c-iconlink"><mdi-open-in-new /></a> |
| 16 | + |
| 17 | + |
| 18 | +--- |
| 19 | +layout: top-title |
| 20 | +color: bowdoin-title |
| 21 | +--- |
| 22 | + |
| 23 | +:: title :: |
| 24 | + |
| 25 | +# Booleans |
| 26 | + |
| 27 | +:: content :: |
| 28 | + |
| 29 | +<br> |
| 30 | + |
| 31 | +# A boolean or `bool` is a ==primitive== data type that is only represented by either `True` or `False`. |
| 32 | + |
| 33 | +<br> |
| 34 | + |
| 35 | +#### In Python, they are always capitalized: `True` and `False`. |
| 36 | + |
| 37 | +<br> |
| 38 | + |
| 39 | +```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:16}} |
| 40 | +# Assigning Boolean literals to variables |
| 41 | +is_snowing: bool = True |
| 42 | +is_sunny: bool = False |
| 43 | + |
| 44 | +print("Is it snowing? ⛄", is_snowing) |
| 45 | +print(f"Is it sunny? 🌞 {is_sunny}") |
| 46 | +``` |
| 47 | + |
| 48 | +--- |
| 49 | +layout: section |
| 50 | +color: bowdoin |
| 51 | +--- |
| 52 | + |
| 53 | +### Okay, but how often will I use `True` or `False`? |
| 54 | + |
| 55 | +<br> |
| 56 | + |
| 57 | +#### By themselves? Probably not very often.. However, `True` and `False` are used heavily because they are the result of a new type of expression! |
| 58 | + |
| 59 | +<twemoji-thinking-face v-drag="[825,376,90,90]" /> |
| 60 | + |
| 61 | +--- |
| 62 | +layout: top-title |
| 63 | +color: bowdoin-title |
| 64 | +--- |
| 65 | + |
| 66 | +:: title :: |
| 67 | + |
| 68 | +# Relational Operators |
| 69 | + |
| 70 | +:: content :: |
| 71 | + |
| 72 | +## Relational operators allow us to ask basic questions about the state of our program's data. Here are the questions we can ask: |
| 73 | + |
| 74 | +<br> |
| 75 | + |
| 76 | +| Human Operator | Python Symbol | Example Expression | |
| 77 | +| :----------------------: | :-----------: | :----------------: | |
| 78 | +| Equal to | `==` | `5 == 5.0` | |
| 79 | +| Not equal to | `!=` | `True != False` | |
| 80 | +| Greater than | `>` | `7.0 > 3` | |
| 81 | +| Less than | `<` | `"A" < "B"` | |
| 82 | +| Greater than or equal to | `>=` | `4 >= 4` | |
| 83 | +| Less than or equal to | `<=` | `3.5 <= 5.0` | |
| 84 | + |
| 85 | +--- |
| 86 | +layout: top-title |
| 87 | +color: bowdoin-title |
| 88 | +--- |
| 89 | + |
| 90 | +:: title :: |
| 91 | + |
| 92 | +# Relational Operators |
| 93 | + |
| 94 | +:: content :: |
| 95 | + |
| 96 | +<br> |
| 97 | + |
| 98 | +## The result of these relational expressions will be a `bool` value. |
| 99 | + |
| 100 | +<br> |
| 101 | + |
| 102 | +## This allows us to store the result of asking a question and potentially use that answer later in our program. |
| 103 | + |
| 104 | +<br> |
| 105 | + |
| 106 | +```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:16}} |
| 107 | +word_1: str = "Apple" |
| 108 | +word_2: str = "Banana" |
| 109 | + |
| 110 | +in_order: bool = word_1 < word_2 |
| 111 | + |
| 112 | +print(f"{word_1} comes before {word_2}? {in_order}") |
| 113 | +``` |
| 114 | +--- |
| 115 | +layout: section |
| 116 | +color: bowdoin |
| 117 | +--- |
| 118 | + |
| 119 | +### Okay, so we can get meaningful `bool` values.. |
| 120 | + |
| 121 | +<br> |
| 122 | + |
| 123 | +### But what can we actually do with them? |
| 124 | + |
| 125 | +<twemoji-thinking-face v-drag="[825,376,90,90]" /> |
| 126 | + |
| 127 | +--- |
| 128 | +layout: top-title |
| 129 | +color: bowdoin-title |
| 130 | +--- |
| 131 | + |
| 132 | +:: title :: |
| 133 | + |
| 134 | +# The `if` Statement |
| 135 | + |
| 136 | +:: content :: |
| 137 | + |
| 138 | +#### An `if` statement, also called a "conditional" statement, allows us to ask questions about the state of data and *"conditionally"* execute code based the result. |
| 139 | + |
| 140 | +<br> |
| 141 | + |
| 142 | +#### `if` statements can evaluate <u>any</u> `True` or `False` expression! |
| 143 | + |
| 144 | +<br> |
| 145 | + |
| 146 | +#### Any indented code under the if statement only executes if the expression (also called the condition) evaluates to `True`. If the condition is `False`, the code doesn't run! |
| 147 | + |
| 148 | + |
| 149 | +```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:14}} |
| 150 | +temperature: float = 31.5 # Try changing the temp to see what happens! |
| 151 | + |
| 152 | +if temperature < 32.0: |
| 153 | + print("It is freezing! 🥶") |
| 154 | + print("Maybe it will snow! ⛄") |
| 155 | +print("Not inside of the if! 🤪") |
| 156 | +``` |
| 157 | + |
| 158 | +--- |
| 159 | +layout: top-title |
| 160 | +color: bowdoin-title |
| 161 | +--- |
| 162 | + |
| 163 | +:: title :: |
| 164 | + |
| 165 | +# The `else` Clause |
| 166 | + |
| 167 | +:: content :: |
| 168 | + |
| 169 | +#### We can optionally add an `else` clause to `if` statements that defines alternative code to runs in the event that the `if` statement's condition evaluates to `False`. |
| 170 | + |
| 171 | +<br> |
| 172 | + |
| 173 | +#### Each `if` statement can only have one `else` clause. |
| 174 | + |
| 175 | +<br> |
| 176 | + |
| 177 | +#### An `else` must be at the same indentation level as its corresponding `if` statement. |
| 178 | + |
| 179 | +<br> |
| 180 | + |
| 181 | +```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on', fontSize:14}} |
| 182 | +password: str = "1234" |
| 183 | + |
| 184 | +if password == "1101S25": |
| 185 | + print("Welcome to our CodeRunner! ✅") |
| 186 | +else: |
| 187 | + print("That password is incorrect! ❌") |
| 188 | + print("Please try again!") |
| 189 | +``` |
| 190 | + |
| 191 | +--- |
| 192 | +layout: top-title |
| 193 | +color: bowdoin-title |
| 194 | +--- |
| 195 | + |
| 196 | +:: title :: |
| 197 | + |
| 198 | +# Nesting |
| 199 | + |
| 200 | +:: content :: |
| 201 | + |
| 202 | +<twemoji-nesting-dolls v-drag="[854,86,90,90]" /> |
| 203 | + |
| 204 | +#### We can ==nest== conditional statements inside of other conditional statements! |
| 205 | + |
| 206 | +<br> |
| 207 | + |
| 208 | +#### We can also put conditional statements inside of functions! |
| 209 | + |
| 210 | +<br> |
| 211 | + |
| 212 | +```python {monaco-run} {autorun:true, editorOptions: { lineNumbers:'on'}} |
| 213 | +def check_number(num: int) -> bool: |
| 214 | + if num == 0: |
| 215 | + return f"{num} is zero!" |
| 216 | + else: |
| 217 | + if num > 0: |
| 218 | + return f"{num} is positive!" |
| 219 | + else: |
| 220 | + return f"{num} is negative!" |
| 221 | + |
| 222 | +print(check_number(10)) |
| 223 | +print(check_number(-5)) |
| 224 | +print(check_number(0)) |
| 225 | +``` |
0 commit comments