Skip to content

Commit 1b74bc6

Browse files
committed
Update after 2nd course day
1 parent 9201b58 commit 1b74bc6

1 file changed

Lines changed: 145 additions & 15 deletions

File tree

src/PythonBasics.py

Lines changed: 145 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@
244244
# >=
245245
print(8 >= 8)
246246

247-
# ==
247+
# ==
248248
print(7 == 7)
249249

250250
# <
@@ -253,6 +253,18 @@
253253
# <=
254254
print(7 <= 7)
255255

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+
256268

257269
### control structures: loops: while loop, for loop
258270
# loops are used to repeat parts of code
@@ -276,51 +288,169 @@
276288
# session 2 on 16. September 2025
277289
###########################
278290

279-
# 1. Repetition
291+
# Exercise 5
280292

293+
fibonaccis = [0]
281294

282-
# 2. new topics in accition to yesterday
295+
running_number = 1
283296

284-
# list concatenation with +
285-
# range()
286-
# == vs is
287-
# != vs is not
288-
# match cases
297+
STOP = 200
289298

299+
while running_number < STOP:
300+
fibonaccis.append(running_number)
301+
running_number = fibonaccis[-1] + fibonaccis[-2]
290302

303+
print(fibonaccis)
291304

292305

306+
# for loop
307+
# for loops are there to iterate over finite numbers of elements
293308

309+
soiltype_list = ['silt', 'sand', 'cobbles', 'clay', 'silty clay', 'sandy silt']
310+
soil_cohesions = [10, 0, 0, 30, 20, 5]
294311

312+
for i in range(len(soiltype_list)):
313+
print(soiltype_list[i], soil_cohesions[i])
295314

315+
# Exercise 6
316+
STOP = 1000
296317

318+
numbers = []
297319

320+
for i in range(STOP):
321+
if i % 3 == 0 or i % 5 == 0:
322+
numbers.append(i)
323+
print(sum(numbers))
298324

299325

326+
# Exercise 7
300327

301328

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']
302333

334+
year = 2007
303335

336+
years = list(range(year, year + len(rocks)))
304337

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]}')
305341

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}')
307345

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}')
309349

310-
# Exercise 7
350+
# Exercise 3 bonus:
311351

352+
# create a new empty list
312353

313-
# Exercise 3 bonus:
354+
empty_list = []
314355

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}')
315377

316-
###########################
317-
# session 3
318-
###########################
319378

320379
# Exercise 8
321380

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+
322402
# Exercise 9
323403

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+
324454
### functions
325455

326456
# Exercise 10

0 commit comments

Comments
 (0)