|
| 1 | +# Day 5 - Conditions & Control Flow |
| 2 | + |
| 3 | +__Reference__ |
| 4 | +``` |
| 5 | +>>> True |
| 6 | +>>> False |
| 7 | +
|
| 8 | +>>> True == False |
| 9 | +
|
| 10 | +>>> True != False |
| 11 | +
|
| 12 | +>>> 10 > 31 |
| 13 | +
|
| 14 | +>>> 94 < 123 |
| 15 | +
|
| 16 | +>>> 133 >= 133 |
| 17 | +
|
| 18 | +>>> 133 <= 201 |
| 19 | +
|
| 20 | +>>> "abc" == "ABC".lower() |
| 21 | +
|
| 22 | +>>> "XyZ".upper() == "XYZ" |
| 23 | +
|
| 24 | +>>> str(True).lower() == "true": |
| 25 | +
|
| 26 | +>>> 1 > 10 |
| 27 | +
|
| 28 | +>>> (3123 / 30) == 32 |
| 29 | +
|
| 30 | +>>> 24 % 5 == 3 |
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +__Conditions & Conditional Statements__ |
| 36 | +``` |
| 37 | +>>> True |
| 38 | +True |
| 39 | +>>> False |
| 40 | +False |
| 41 | +>>> if True: |
| 42 | +... print("hi there") |
| 43 | +... |
| 44 | +hi there |
| 45 | +>>> if False: |
| 46 | +... print("nothing") |
| 47 | +... |
| 48 | +>>> if not False: |
| 49 | +... print("whats up") |
| 50 | +... |
| 51 | +whats up |
| 52 | +>>> True == False |
| 53 | +False |
| 54 | +>>> var_ = "this" |
| 55 | +>>> True = False |
| 56 | + File "<stdin>", line 1 |
| 57 | +SyntaxError: cannot assign to True |
| 58 | +>>> for = True |
| 59 | + File "<stdin>", line 1 |
| 60 | + for = True |
| 61 | + ^ |
| 62 | +SyntaxError: invalid syntax |
| 63 | +>>> 10 > 32 |
| 64 | +False |
| 65 | +>>> 424 < 323433 |
| 66 | +True |
| 67 | +>>> 10 > 32 or 10 < 32 |
| 68 | +True |
| 69 | +>>> not 10 > 32 |
| 70 | +True |
| 71 | +>>> not (10 > 32) |
| 72 | +True |
| 73 | +>>> 200 > 200 |
| 74 | +False |
| 75 | +>>> 200 >= 200 |
| 76 | +True |
| 77 | +>>> 200 > 200 or 200 == 200 |
| 78 | +True |
| 79 | +>>> true, false |
| 80 | +Traceback (most recent call last): |
| 81 | + File "<stdin>", line 1, in <module> |
| 82 | +NameError: name 'true' is not defined |
| 83 | +>>> true |
| 84 | +Traceback (most recent call last): |
| 85 | + File "<stdin>", line 1, in <module> |
| 86 | +NameError: name 'true' is not defined |
| 87 | +>>> str(True).lower() == "true" |
| 88 | +True |
| 89 | +>>> str("true").title() == "True" |
| 90 | +True |
| 91 | +>>> str("true") |
| 92 | +'true' |
| 93 | +>>> "abc".title() |
| 94 | +'Abc' |
| 95 | +>>> bool(str("true").title()) |
| 96 | +True |
| 97 | +>>> abc = [1,2,3,4,5] |
| 98 | +>>> abc_sq = [] |
| 99 | +>>> for num in abc: |
| 100 | +... new_number = num ** 2 |
| 101 | +... abc_sq.append(new_number) |
| 102 | +... |
| 103 | +>>> abc_sq |
| 104 | +[1, 4, 9, 16, 25] |
| 105 | +>>> is_even = [] |
| 106 | +>>> is_odd = [] |
| 107 | +>>> for num in abc_sq: |
| 108 | +... if num % 2 == 0: |
| 109 | +... print("this is even") |
| 110 | +... is_even.append(num) |
| 111 | +... else: |
| 112 | +... is_odd.append(num) |
| 113 | +... |
| 114 | +this is even |
| 115 | +this is even |
| 116 | +>>> print(is_even, is_odd) |
| 117 | +[4, 16] [1, 9, 25] |
| 118 | +>>> is_factor_of_3 = [] |
| 119 | +>>> for num in abc_sq: |
| 120 | +... if num % 3 == 0: |
| 121 | +... is_factor_of_3.append(num) |
| 122 | +... elif num % 2 == 0: |
| 123 | +... is_even.append(num) |
| 124 | +... else: |
| 125 | +... is_odd.append(num) |
| 126 | +... |
| 127 | +>>> print(is_factor_of_3) |
| 128 | +[9] |
| 129 | +>>> print(is_even, is_odd) |
| 130 | +[4, 16, 4, 16] [1, 9, 25, 1, 25] |
| 131 | +>>> is_even = [] |
| 132 | +>>> is_odd = [] |
| 133 | +>>> is_factor_of_3 = [] |
| 134 | +>>> for num in abc_sq: |
| 135 | +... if num % 3 == 0: |
| 136 | +... is_factor_of_3.append(num) |
| 137 | +... else: |
| 138 | +... if num % 2 == 0: |
| 139 | +... is_even.append(num) |
| 140 | +... else: |
| 141 | +... is_odd.append(num) |
| 142 | +... |
| 143 | +>>> print(is_even, is_odd) |
| 144 | +[4, 16] [1, 25] |
| 145 | +>>> print(is_factor_of_3) |
| 146 | +[9] |
| 147 | +>>> for num in abc_sq: |
| 148 | +... if num % 3 == 0: |
| 149 | +... is_factor_of_3.append(num) |
| 150 | +... if num % 2 == 0: |
| 151 | +... is_even.append(num) |
| 152 | +... is_odd.append(num) |
| 153 | +... |
| 154 | +>>> print(is_even, is_odd) |
| 155 | +[4, 16, 4, 16] [1, 25, 1, 4, 9, 16, 25] |
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | +__While Loops & Control Flow__ |
| 160 | +``` |
| 161 | +🕹 % python3 |
| 162 | +Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18) |
| 163 | +[Clang 6.0 (clang-600.0.57)] on darwin |
| 164 | +Type "help", "copyright", "credits" or "license" for more information. |
| 165 | +>>> x = 10 |
| 166 | +>>> i = 0 |
| 167 | +>>> while i < x: |
| 168 | +... print(i) |
| 169 | +... i = i + 1 |
| 170 | +... |
| 171 | +0 |
| 172 | +1 |
| 173 | +2 |
| 174 | +3 |
| 175 | +4 |
| 176 | +5 |
| 177 | +6 |
| 178 | +7 |
| 179 | +8 |
| 180 | +9 |
| 181 | +>>> while i < x: |
| 182 | +... print(i) |
| 183 | +... |
| 184 | +>>> i = 0 |
| 185 | +>>> while i < x: |
| 186 | +... print(i) |
| 187 | +... |
| 188 | +0 |
| 189 | +0 |
| 190 | +
|
| 191 | +Traceback (most recent call last): |
| 192 | + File "<stdin>", line 2, in <module> |
| 193 | +KeyboardInterrupt |
| 194 | +>>> |
| 195 | +KeyboardInterrupt |
| 196 | +>>> |
| 197 | +KeyboardInterrupt |
| 198 | +>>> x = 10 |
| 199 | +>>> i = 0 |
| 200 | +>>> z = 12 |
| 201 | +>>> while i < x: |
| 202 | +... z = z * 2 |
| 203 | +... if z > 342900: |
| 204 | +... break |
| 205 | +... print(z, i) |
| 206 | +... i = i + .00000000001 |
| 207 | +... |
| 208 | +24 0 |
| 209 | +48 1e-11 |
| 210 | +96 2e-11 |
| 211 | +192 3e-11 |
| 212 | +384 4e-11 |
| 213 | +768 4.9999999999999995e-11 |
| 214 | +1536 6e-11 |
| 215 | +3072 7e-11 |
| 216 | +6144 8.000000000000001e-11 |
| 217 | +12288 9.000000000000001e-11 |
| 218 | +24576 1.0000000000000002e-10 |
| 219 | +49152 1.1000000000000002e-10 |
| 220 | +98304 1.2000000000000003e-10 |
| 221 | +196608 1.3000000000000002e-10 |
| 222 | +>>> x = 10 |
| 223 | +>>> i = 0 |
| 224 | +>>> while i < x: |
| 225 | +... if i % 2 == 0: |
| 226 | +... print("even") |
| 227 | +... else: |
| 228 | +... continue |
| 229 | +... i += 1 |
| 230 | +... |
| 231 | +even |
| 232 | +^CTraceback (most recent call last): |
| 233 | + File "<stdin>", line 2, in <module> |
| 234 | +KeyboardInterrupt |
| 235 | +>>> |
| 236 | +KeyboardInterrupt |
| 237 | +>>> while i < x: |
| 238 | +... print(i) |
| 239 | +... if i % 2 == 0: |
| 240 | +... print("even") |
| 241 | +... else: |
| 242 | +... continue |
| 243 | +... i = i + 1 |
| 244 | +... |
| 245 | +1 |
| 246 | +1 |
| 247 | +1 |
| 248 | +Traceback (most recent call last): |
| 249 | + File "<stdin>", line 2, in <module> |
| 250 | +KeyboardInterrupt |
| 251 | +>>> |
| 252 | +KeyboardInterrupt |
| 253 | +>>> |
| 254 | +KeyboardInterrupt |
| 255 | +>>> |
| 256 | +>>> |
| 257 | +>>> |
| 258 | +>>> x = 10 |
| 259 | +>>> i = 0 |
| 260 | +>>> while i < x: |
| 261 | +... if i % 2 == 0: |
| 262 | +... continue |
| 263 | +... else: |
| 264 | +... print('odd') |
| 265 | +... i += 1 |
| 266 | +... |
| 267 | +^CTraceback (most recent call last): |
| 268 | + File "<stdin>", line 1, in <module> |
| 269 | +KeyboardInterrupt |
| 270 | +>>> |
| 271 | +KeyboardInterrupt |
| 272 | +>>> while i < x: |
| 273 | +... i += 1 |
| 274 | +... if i % 2 == 0: |
| 275 | +... continue |
| 276 | +... print(i) |
| 277 | +... |
| 278 | +1 |
| 279 | +3 |
| 280 | +5 |
| 281 | +7 |
| 282 | +9 |
| 283 | +>>> |
| 284 | +
|
| 285 | +``` |
0 commit comments