Basic Python

  1. Simple arithmetics (5 min). Calculate:

    1. $2^5$
    2. 31/2
    3. 015 + 32
    4. 11 mod 2

    Are they results like you expected?

  2. Lists (30 min). Define following lists:

    1. Odd numbers from 1 to 11. Hint: Use range method.
    2. A list of arbitrary number of zeros
    3. Sequence of letters from "a" to "k". Hint: use chr method.
    4. Elements of power series ($A_i = \sum_{k=1}^{i} x^i$, x = 1.2) smaller than a 10.
    5. 10x10 identity matrix.
  3. Introspection (20 min). In Python interpreter define following variables:

    1. empty string
    2. empty list
    3. empty dictionary

    Using built-in dir and help method find out which methods these data structures offer. Give a working example documenting at least one of the listed methods.

  4. Enigma (20 min). Recover the message hidden in the string using the following translation: a ➔ c, b ➔ d, ..., z ➔ b.

    g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr
    amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q
    ufw rfgq rcvr gq qm jmle. sqgle qrp.rpylqjyrc() gq pcamkkclbcb!
    
  5. Binary conversion (20 min). Write a program converting integers to their binary representation. You can store the result either in a list or in a string.

  6. Cashier (1,5 h). Implement a simple cashier machine with the following features :

    1. read the price list from a file (for example, using the csv module)
    2. implement an interactive entry of a codebar or a product name. Hint: use raw_input
    3. make sure that the product names can be given in upper-, lower- or mixed-case (they should be case insesitive)
    4. try to add an item not on the price list to the basket. What happens? Add exception handling (try... except... clause) for this case.
    5. add a function which nicely formats the bill (with fixed column widths and column headers) and prints it to standard output
    6. refactor code so that each seperate task is handled by seperate function (reading price list, checking out, printing the bill)
    7. write simple tests to check if the functions return a correct output for a pre-defined inputs. Store the tests in a seperate file called tests.py.