Dear Study group attendees, this time we will do some basic exercise from http://www.practicepython.org/ We will do the assignments from the website blockwise and discuss the solutions in between - let's look how far we can reach during the lesson. If you want you can practice this assignments at home and use the python study group to ask questions. At the end of this site you will find some python functions we discussed with Rodolfo. Here you see the solutions for the first 3 Exercises: Example 1: Character Input {{{#!highlight python name = input('Please tell me your name ') print(f'Hello {name}\n') age = input('Please tell me your age ') difference = 100 - int(age) print(f'{name}, you will turn 100 years in {difference} years.\n') repeat = input('Please tell me another number ') # print(f'{name}, you will turn 100 years in {difference} years.\n' * int(repeat)) print(name + ', you will turn 100 years in ' + str(difference) + ' years.\n') }}} Example 2: Odd Or Even {{{#!highlight python number = int(input('Please tell me a number ')) if number % 4 == 0 : print('This number is a multiple of 4') else: if number % 2 == 0: print('Number is even') else: print('Number is odd') num = int(input('Please tell me a number ')) check = int(input('Tell me a number which devides you number ')) if num % check == 0: print(f'{num} devides evently through {check}') else: print(f'{num} does not evently devide through {check}.') }}} Example 3: List Less Than Ten {{{#!highlight python a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] emptylist = [] for elem in a: if elem < 5: print(elem) emptylist.append(elem) print(emptylist) number = int(input('Please tell me a number ')) print(f'Listelements smaller than {number} are') for elem in a: if elem < number: print(elem) }}} YIELD: The yield-command stops the code where it is and returns the value. When next element is requested it continues at the location it stopped before. {{{#!highlight python def myrange(stop): v = 0 while v < stop: yield v v += 1 del __builtins__.range for i in myrange(10): print(i) def some_people(): yield "thomas" yield "marion" yield "hop" yield "adi" def nice_people(): for person in some_people(): if person == "hop": continue yield person yield "my imaginary friend" def greet_the_nice_people(): for person in nice_people(): yield "Hello " + person print(list(some_people())) print(list(nice_people())) for line in greet_the_nice_people(): print(line) }}} This is the output of the yield-example: {{{#! 0 1 2 3 4 5 6 7 8 9 ['thomas', 'marion', 'hop', 'adi'] ['thomas', 'marion', 'adi', 'my imaginary friend'] Hello thomas Hello marion Hello adi Hello my imaginary friend }}} Things we discussed furthermore - credits to Rodolfo! [[https://docs.python.org/3/tutorial/interpreter.html#interactive-mode|help(...)]] [[https://docs.python.org/3/tutorial/datastructures.html#sets|set(...)]] [[https://docs.python.org/3/library/inspect.html?highlight=inspect#inspect.getsource|import inspect; inspect.getsource(...)]] [[https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions|List comprehensions]] [[https://docs.python.org/3/tutorial/classes.html#generators|Generator functions]] [[https://docs.python.org/3/tutorial/classes.html#generator-expressions|Generator expressions]] ---- CategoryShownotes