|
244 | 244 | # >= |
245 | 245 | print(8 >= 8) |
246 | 246 |
|
247 | | -# == |
| 247 | +# == |
248 | 248 | print(7 == 7) |
249 | 249 |
|
250 | 250 | # < |
|
253 | 253 | # <= |
254 | 254 | print(7 <= 7) |
255 | 255 |
|
| 256 | +# match - cases are an alternative control structure that is more efficient |
| 257 | +# when it comes to matching single strings |
| 258 | +DRILLING_ID = 'D_06' |
| 259 | + |
| 260 | +match DRILLING_ID: |
| 261 | + case 'D_00': |
| 262 | + print(f'the drilling {DRILLING_ID} encountered {weathering_grades[2]} rock') |
| 263 | + case 'D_01': |
| 264 | + print(f'the drilling {DRILLING_ID} encountered {weathering_grades[1]} rock') |
| 265 | + case _: |
| 266 | + print('unknown drilling') |
| 267 | + |
256 | 268 |
|
257 | 269 | ### control structures: loops: while loop, for loop |
258 | 270 | # loops are used to repeat parts of code |
|
276 | 288 | # session 2 on 16. September 2025 |
277 | 289 | ########################### |
278 | 290 |
|
279 | | -# 1. Repetition |
| 291 | +# Exercise 5 |
280 | 292 |
|
| 293 | +fibonaccis = [0] |
281 | 294 |
|
282 | | -# 2. new topics in accition to yesterday |
| 295 | +running_number = 1 |
283 | 296 |
|
284 | | -# list concatenation with + |
285 | | -# range() |
286 | | -# == vs is |
287 | | -# != vs is not |
288 | | -# match cases |
| 297 | +STOP = 200 |
289 | 298 |
|
| 299 | +while running_number < STOP: |
| 300 | + fibonaccis.append(running_number) |
| 301 | + running_number = fibonaccis[-1] + fibonaccis[-2] |
290 | 302 |
|
| 303 | +print(fibonaccis) |
291 | 304 |
|
292 | 305 |
|
| 306 | +# for loop |
| 307 | +# for loops are there to iterate over finite numbers of elements |
293 | 308 |
|
| 309 | +soiltype_list = ['silt', 'sand', 'cobbles', 'clay', 'silty clay', 'sandy silt'] |
| 310 | +soil_cohesions = [10, 0, 0, 30, 20, 5] |
294 | 311 |
|
| 312 | +for i in range(len(soiltype_list)): |
| 313 | + print(soiltype_list[i], soil_cohesions[i]) |
295 | 314 |
|
| 315 | +# Exercise 6 |
| 316 | +STOP = 1000 |
296 | 317 |
|
| 318 | +numbers = [] |
297 | 319 |
|
| 320 | +for i in range(STOP): |
| 321 | + if i % 3 == 0 or i % 5 == 0: |
| 322 | + numbers.append(i) |
| 323 | +print(sum(numbers)) |
298 | 324 |
|
299 | 325 |
|
| 326 | +# Exercise 7 |
300 | 327 |
|
301 | 328 |
|
| 329 | +rocks = ['granite', 'sandstone', 'basalt', 'limestone', 'tuff', 'quartzite', |
| 330 | + 'kaolin', 'phonolite', 'gneiss', 'sand', 'diabase', 'black coal', |
| 331 | + 'slate', 'andesite', 'andesite', 'gypsum and anhydrite', 'greywacke', |
| 332 | + 'suevite'] |
302 | 333 |
|
| 334 | +year = 2007 |
303 | 335 |
|
| 336 | +years = list(range(year, year + len(rocks))) |
304 | 337 |
|
| 338 | +# for loop approach 1 |
| 339 | +for i in range(len(rocks)): |
| 340 | + print(f'the rock of the year {years[i]} was {rocks[i]}') |
305 | 341 |
|
306 | | -# Exercise 5 |
| 342 | +# for loop approach 2 |
| 343 | +for i, rock in enumerate(rocks): |
| 344 | + print(f'the rock of the year {years[i]} was {rock}') |
307 | 345 |
|
308 | | -# Exercise 6 |
| 346 | +# for loop approach 3 |
| 347 | +for rock, year in zip(rocks, years): |
| 348 | + print(f'the rock of the year {year} was {rock}') |
309 | 349 |
|
310 | | -# Exercise 7 |
| 350 | +# Exercise 3 bonus: |
311 | 351 |
|
| 352 | +# create a new empty list |
312 | 353 |
|
313 | | -# Exercise 3 bonus: |
| 354 | +empty_list = [] |
314 | 355 |
|
| 356 | +# compute the number of characters of the strings 'marl', 'gneiss', 'limestone', 'eclogite' |
| 357 | + |
| 358 | +rocks = ['marl', 'gneiss', 'limestone', 'eclogite'] |
| 359 | + |
| 360 | +# append the numbers to the empty list in the above given order |
| 361 | + |
| 362 | +for rock in rocks: |
| 363 | + n_characters = len(rock) |
| 364 | + empty_list.append(n_characters) |
| 365 | + |
| 366 | +# print the list |
| 367 | + |
| 368 | +print(empty_list) |
| 369 | + |
| 370 | +# compute the sum of the last three elements of the list |
| 371 | + |
| 372 | +sum_of_last_3 = sum(empty_list[-3:]) |
| 373 | + |
| 374 | +# print the result as a string “the result is: XX” two times by using the .format() method and an f-string. |
| 375 | + |
| 376 | +print(f'the result is: {sum_of_last_3}') |
315 | 377 |
|
316 | | -########################### |
317 | | -# session 3 |
318 | | -########################### |
319 | 378 |
|
320 | 379 | # Exercise 8 |
321 | 380 |
|
| 381 | +# create two lists with the following content: |
| 382 | +rock_list = ['gneiss', 'marl', 'limestone'] |
| 383 | +ucs = [150, 45, 90] |
| 384 | + |
| 385 | +# create a dictionary called “rock_dict” with the first list as keys and the |
| 386 | +# second list as values |
| 387 | + |
| 388 | +rock_dict = dict(zip(rock_list, ucs)) |
| 389 | + |
| 390 | +# add a new entry to the dictionary consisting of a new rock type and an |
| 391 | +# according UCS value |
| 392 | + |
| 393 | +rock_dict['dolomite'] = 120 |
| 394 | + |
| 395 | +# print the content of the whole dictionary to the console through formatted |
| 396 | +# strings saying “The XX has a UCS of YY MPa”. Use a for-loop for this |
| 397 | + |
| 398 | +for rock in rock_dict.keys(): |
| 399 | + print(f'The {rock} has a UCS of {rock_dict[rock]} MPa.') |
| 400 | + |
| 401 | + |
322 | 402 | # Exercise 9 |
323 | 403 |
|
| 404 | +# compute the mean value, the median, the variance and the standard deviation |
| 405 | +# for that list: |
| 406 | +c = [1, 2, 3, 1, 3, 3, 2, 1, 4, 6, 4, 1] |
| 407 | + |
| 408 | +# print the results with values rounded to 2 digits |
| 409 | + |
| 410 | +# mean |
| 411 | +mean_value = sum(c) / len(c) |
| 412 | +print(f'mean value: {round(mean_value, 2)}') |
| 413 | + |
| 414 | +# median |
| 415 | +c = sorted(c) |
| 416 | + |
| 417 | +if len(c) % 2 == 0: # in case of an even list |
| 418 | + mid_upper = int(len(c) / 2) # first we get the upper index |
| 419 | + mid_lower = mid_upper - 1 # then we get the lower index |
| 420 | + median = (c[mid_upper] + c[mid_lower]) / 2 |
| 421 | +else: # in case of an uneven list |
| 422 | + mid = int(len(c) / 2) |
| 423 | + median = c[mid] |
| 424 | + |
| 425 | +print(f'median value: {round(median, 2)}') |
| 426 | + |
| 427 | +# variance |
| 428 | +sum_list = [] |
| 429 | + |
| 430 | +for x_i in c: |
| 431 | + sum_list.append((x_i - mean_value)**2) |
| 432 | + |
| 433 | +sum_of_sum_list = sum(sum_list) |
| 434 | + |
| 435 | +variance = sum_of_sum_list / len(c) |
| 436 | + |
| 437 | +print(f'variance value: {round(variance, 2)}') |
| 438 | + |
| 439 | +# standard deviation |
| 440 | +standard_deviation = variance**0.5 |
| 441 | + |
| 442 | +print(f'standard deviation: {round(standard_deviation, 2)}') |
| 443 | + |
| 444 | + |
| 445 | + |
| 446 | + |
| 447 | + |
| 448 | +########################### |
| 449 | +# session 3 on 17. September 2025 |
| 450 | +########################### |
| 451 | + |
| 452 | +# 1. repetition |
| 453 | + |
324 | 454 | ### functions |
325 | 455 |
|
326 | 456 | # Exercise 10 |
|
0 commit comments