Python Basic and Advanced (22.3.2021 - 25.3.2021)

Agenda

  • Day 1: Programming Basics

    • Variables

    • Control Flow: if, while, for

    • Functions

    • Exercises

  • Day 2: Python, Advanced

    • Data Structures: list, set, dict

    • Iteration

    • File I/O (CSV Files?)

    • Exercises

  • Day 3: even more advanced

    • Object Oriented Programming

    • Unit Testing

    • Decorators

    • Exercises

  • Day 4: Consulting

Log

We’re lagging behind what’s in the agenda, don’t want to lose anybody.

Day 1

Day 2

  • Exercise: digit translation

    import sys
    
    lookup_table = {
        0: 'zero',
        1: 'one',
        2: 'two',
        3: 'three',
        4: 'four',
        5: 'five',
        6: 'six',
        7: 'seven',
        8: 'eight',
        9: 'nine',
    }
    
    if len(sys.argv) != 2:
        print('gib eine parameter an  mich, depp!', file=sys.stderr)
        sys.exit(1)
    
    digit = int(sys.argv[1])
    
    word = lookup_table.get(digit)
    if word == None:
        print('nix drin', file=sys.stderr)
        sys.exit(3)
    else:
        print(word)
    
  • while

  • Exercise: primeness (using while)

    import sys
    
    
    number = int(sys.argv[1])
    
    if number == 1:
        print(number, 'is not prime')
        sys.exit(0)
    
    divisor = 2
    
    while divisor < number:
        if number % divisor == 0:
            print(number, 'is not prime')
            break
    
        divisor += 1
    else:
        print(number, 'is prime')
    
  • for, range()

  • Exercise: primeness (using for)

    import sys
    
    
    number = int(sys.argv[1])
    
    if number == 1:
        print(number, 'is not prime')
        sys.exit(0)
    
    for divisor in range(2, number):
        if number % divisor == 0:
            print(number, 'is not prime')
            break
    else:
        print(number, 'is prime')
    
  • Functions

  • Exercise: primeness (function)

    import sys
    
    def is_prime(number):
        if number == 1:
            return False
        
        for divisor in range(2, number):
            if number % divisor == 0:
                return False
        else:
            return True
    
    n = int(sys.argv[1])
    if is_prime(n):
        print(n, 'is prime')
    else:
        print(n, 'is not prime')
        
    
    
    
    
    
    
  • Exercise: uniq function

    def uniq(seq):
        have = set()
        ret_l = []
        for element in seq:
            if element not in have:
                ret_l.append(element)
                have.add(element)
        return ret_l
    
    l = [4, 2, 4, 5, 6, 7, 8, 4, 2, 8, 9]
    
    uniq_l = uniq(l)
    
    for i in uniq_l:
        print(i)
    
    # 4
    # 2
    # 5
    # 6
    # 7
    # 8
    # 9
    
  • Large group exercise (extending until course finish). Something with CSV files. File IO for a purpose.

Day 3

Cold start in the morning:

  • uniq() transformed into generator

  • with

  • is

  • Using csv.DictReader; see here

  • Group exercise on Github

Day 4

  • Use collections.namedtuple for our dict objects that we use as records.

  • Test driven development

  • OO

  • MQTT