Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python for Systems Engineers - Part 1

Python for Systems Engineers - Part 1

Python is one of the most popular programming languages of our time and the powerhouse behind most of Cisco’s API efforts. If there is a Cisco API chances are high that there will be a software development kit for that API in python. In this 3-part webinar series we will get our hands dirty with python.

Part 1 covers the basics of the python language and some hands-on exercises to get you started on programming.

Marcel Neidinger

November 07, 2019
Tweet

More Decks by Marcel Neidinger

Other Decks in Technology

Transcript

  1. python for Beginners - Part 1
    ”Batteries included” Programming
    Marcel Neidinger
    Associate Solutions Engineer, Collaboration
    [email protected]
    7. November 2019

    View full-size slide

  2. What is the plan?
    • Part 1: Introduction to python3
    • Part 2: Introduction to some useful python packages
    • Part 3: Hands-On - Let’s code a small MVP in one hour
    1

    View full-size slide

  3. About these Slides
    These slides are deliberately filled with content and can serve as a reference for you.
    2

    View full-size slide

  4. About these Slides
    These slides are deliberately filled with content and can serve as a reference for you.
    Code sample
    1 p r i n t (”This i s a code snippet ”)
    Exercise
    2

    View full-size slide

  5. About these Slides
    These slides are deliberately filled with content and can serve as a reference for you.
    Code sample
    1 p r i n t (”This i s a code snippet ”)
    Exercise
    Exercise: Some trivial proofs
    Proof, for any decission problem P1 ∈
    NP and P2 ∈ P that
    P1 >p
    P2
    holds.
    2

    View full-size slide

  6. Introduction

    View full-size slide

  7. What is a python?
    • Cross plattform (Mac, Linux, Windows, Embedder)
    • Easy to write - Easier to read
    • ”Batteries included”
    • Well integrated in the Cisco ecosystem
    3

    View full-size slide

  8. What is a python?
    • Cross plattform (Mac, Linux, Windows, Embedder)
    • Easy to write - Easier to read
    • ”Batteries included”
    • Well integrated in the Cisco ecosystem
    In short python is perfect for
    • Large software projects
    • Quick proofs of concept
    • People starting their programming journey
    3

    View full-size slide

  9. Who uses python?
    python where we can - C++ where we must!
    - Googles early development mantra
    4

    View full-size slide

  10. Who uses python?
    python where we can - C++ where we must!
    - Googles early development mantra
    4

    View full-size slide

  11. Hello World!
    1 p r i n t (” Hello world ! ”)
    5

    View full-size slide

  12. https://repl.it/languages/python3
    5

    View full-size slide

  13. Variables and Datatypes

    View full-size slide

  14. Variablen
    1 a = 10
    b = ” Hallo ”
    3 pi = 3.14
    7

    View full-size slide

  15. Datatypes
    • Python is not statically typed
    8

    View full-size slide

  16. Datatypes
    • Python is not statically typed
    • In English: Variables can have any datatype they want
    8

    View full-size slide

  17. Datatypes
    • Python is not statically typed
    • In English: Variables can have any datatype they want
    • (3 + 1) important (simple) data types
    8

    View full-size slide

  18. Datatypes
    • Python is not statically typed
    • In English: Variables can have any datatype they want
    • (3 + 1) important (simple) data types
    String for text (and characters)
    8

    View full-size slide

  19. Datatypes
    • Python is not statically typed
    • In English: Variables can have any datatype they want
    • (3 + 1) important (simple) data types
    String for text (and characters)
    int for whole numbers
    8

    View full-size slide

  20. Datatypes
    • Python is not statically typed
    • In English: Variables can have any datatype they want
    • (3 + 1) important (simple) data types
    String for text (and characters)
    int for whole numbers
    float for floating numbers
    8

    View full-size slide

  21. Datatypes
    • Python is not statically typed
    • In English: Variables can have any datatype they want
    • (3 + 1) important (simple) data types
    String for text (and characters)
    int for whole numbers
    float for floating numbers
    (None for Nothing)
    8

    View full-size slide

  22. Strings
    1 # Strings are defined with quotation marks
    a = ”This i s a s t r i n g ”
    3 b = ’ This i s also a s t r i n g ’
    5 # You can put them over multiple l i n e s with three ”””
    c = ”””
    7 This s t r i n g goes over
    multiple
    9 l i n e s
    ”””
    11
    # And they can be joined together
    13 d = ”A s t r i n g ”
    e = ”can be e a s i l y connected”
    15 f = d + ” ” + e
    9

    View full-size slide

  23. ints
    1 # D e f i n i t i o n
    a = 10
    3
    # Simple arithmetics with others
    5 a = 10 + 5
    b = a − 5
    7 c = b ∗ 3
    # and with themselfes
    9 a = a + 3
    11 # In a more pythonic way
    a += 3
    13 a ∗= 3
    10

    View full-size slide

  24. floats
    1 # D e f i n i t i o n (mind the ”.”)
    a = 10.0
    3
    # Simple arithmetics
    5 a = 10.0 + 5.0
    b = a/3.0
    7
    # We can also add with other numbers
    9 c = 10
    c = i n t ( c )
    11 d = 3.0 ∗ c
    13 type (d)
    #
    11

    View full-size slide

  25. None
    • What if we want to mark something as nothing?
    • 0 and -1 can be ambigious
    • In python we use None
    a = None
    12

    View full-size slide

  26. Converting data types
    • python keeps track of (what it thinks) the data type of your variable is
    • Often operations can only be applied to variables of the same type (Examples: +,-,...)
    ⇒ We need to convert between data types
    1 # Simple conversion
    a = 10.0
    3 b = i n t (a)
    5 # Strings and numbers
    d = 42
    7 s = ”Das i s t ein String mit der Zahl ”
    s = s + ” ” + s t r (a)
    9 # To check the type
    type (d)
    11 #
    13

    View full-size slide

  27. Exercise for Data Types
    Exercise: Simple Calculations and Conversions
    a) Use python to calculate the following simple instructions
    • 10 + 3 the result should be a float
    • 10/3 the result should be a int
    • 5/2 the result should be a int
    • 10/4 the result should be a float
    b) What is wrong with the following code fragment? Run it to find out!
    1 a = 10
    p r i n t (”Wir haben insgesamt ” + a + ” Probleme”)
    14

    View full-size slide

  28. Solutions
    Solution: Exercise for Data Types
    a)
    a1 = f l o a t (10 + 3)
    2 # >>> 13.0
    a2 = i n t (10/3)
    4 # >>> 3
    a3 = i n t (5/2)
    6 # >>> 2
    a4 = f l o a t (10/4)
    8 # >>> 2.5
    b)
    a is variable of type int. We get a type error since only strings can be concatinated.
    15

    View full-size slide

  29. Loops and Control Structures

    View full-size slide

  30. for-loops
    • for loops allow to iterate over a set of commands n times
    for i in range (0 , 10) :
    2 p r i n t (”This i s the ” + s t r ( i ) + ” . i t e r a t i o n ”)
    16

    View full-size slide

  31. for-loops
    • for loops allow to iterate over a set of commands n times
    1 for i in range (0 , 10) :
    p r i n t (”This i s the ” + s t r ( i ) + ” . i t e r a t i o n ”)
    • The increment isn’t fixed to 1
    for i in range (0 ,10 ,2) :
    2 p r i n t (”Now we only show every second number in the range ( i=” + s t r ( i ) + ”)
    ”)
    16

    View full-size slide

  32. for-loops
    • for loops allow to iterate over a set of commands n times
    1 for i in range (0 , 10) :
    p r i n t (”This i s the ” + s t r ( i ) + ” . i t e r a t i o n ”)
    • The increment isn’t fixed to 1
    for i in range (0 ,10 ,2) :
    2 p r i n t (”Now we only show every second number in the range ( i=” + s t r ( i ) + ”)
    ”)
    • for loops can be nested (i.e. to iterate over the pixels in an image)
    for x in range (0 ,10) :
    2 for y in range (0 ,10) :
    pr i nt (” Searching in coordinate (” + s t r (x) + ” , ” + s t r (y) + ”)”)
    16

    View full-size slide

  33. Exercise for for-loops
    Exercise: Calculating multiplication tables
    In this Exercise we will calculate multiplication tables like this
    1 M u l t i p l i c a t i o n table for 2
    2 x 1 = 2
    3 2 x 2 = 4
    2 x 3 = 6
    5 2 x 4 = 8
    [ . . . ]
    7 2 x 10 = 20
    Tip Use two nested for loops to calculate all multiplication tables from 1 to 10
    17

    View full-size slide

  34. Exercise for for-loops (contd.)
    Solution: Calculating multiplication tables
    1 for num in range (1 , 11) :
    p r i nt (” M u l t i p l i c a t i o n table for ” + s t r (num) )
    3 for m u l t i p l i e r in range (1 , 11) :
    res = num ∗ m u l t i p l i e r
    5 pr i nt ( s t r (num) + ” x ” + s t r ( m u l t i p l i e r ) + ” = ” + s t r ( res ) )
    18

    View full-size slide

  35. Control structures - the if clause
    • Using if-clauses we can change the program flow based on variable values
    • if-clauses need a comparisson
    • Instructions caried out when a comparisson is true are called a branch
    1 a = 10
    i f a == 10:
    3 # This i s a branch
    p r i n t (”a has the value ” + s t r (a) )
    19

    View full-size slide

  36. Control structures - the if clause
    • Using if-clauses we can change the program flow based on variable values
    • if-clauses need a comparisson
    • Instructions caried out when a comparisson is true are called a branch
    1 a = 10
    i f a == 10:
    3 # This i s a branch
    p r i n t (”a has the value ” + s t r (a) )
    • You can add other possible outcomes
    for i in range (0 , 100) :
    2 i f i%2 == 0:
    pr i nt ( s t r ( i ) + ” can be divided by two without a remainder ”)
    4 e l i f i%5 == 0:
    pr i nt ( s t r ( i ) + ” can be divided by f i v e without a remainder ”)
    6 e l s e :
    pr i nt ( s t r ( i ) + ” can not be divided by e i t h e r f i v e or two without a
    remainder ”)
    19

    View full-size slide

  37. Exercise for if-clauses
    Exercise: FizzBuzz - A programming interviewers best friend
    FizzBuzz is a simple test done in programming interviews around the world.
    Write a program that prints the numbers from 1 to (including) 20
    • For multiples of three print Fizz instead of the number
    • For multiples of five print Buzz instead of the number
    • For multiples of both three and five print FizzBuzz instead of the number
    Tip a is a multiple of b if a%b == 0
    Tip You can chain multiple conditions with and
    1 i f age > 18 and age < 50:
    p r i nt (”You are young enough”)
    3
    20

    View full-size slide

  38. Exercise for if-clauses (contd.)
    Solution: FizzBuzz - A programming interviewers best friend
    for num in range (1 , 21) :
    2 i f num % 3 == 0 and num % 5 == 0:
    pr i n t ( ’ FizzBuzz ’ )
    4 e l i f num % 3 == 0:
    pr i n t ( ’ Fizz ’ )
    6 e l i f num % 5 == 0:
    pr i n t ( ’Buzz ’ )
    8 e l s e :
    pr i n t ( s t r (num) )
    21

    View full-size slide

  39. Functions
    • Often we need to re-use parts of our code
    22

    View full-size slide

  40. Functions
    • Often we need to re-use parts of our code
    • Copy & Paste is never the correct answer
    22

    View full-size slide

  41. Functions
    • Often we need to re-use parts of our code
    • Copy & Paste is never the correct answer
    • Example: FizzBuzz from the last Exercise
    1 def fizzbuzz () :
    for num in range (1 , 21) :
    3 i f num % 3 == 0 and num % 5 == 0:
    p r i n t ( ’ FizzBuzz ’ )
    5 e l i f num % 3 == 0:
    p r i n t ( ’ Fizz ’ )
    7 e l i f num % 5 == 0:
    p r i n t ( ’Buzz ’ )
    9 e l s e :
    p r i n t (num)
    11 fizzbuzz ()
    22

    View full-size slide

  42. Functions (contd.)
    • Functions can have arguments
    1 def fizzbuzz (n) :
    for num in range (1 , n) :
    3 . . .
    • Functions can return stuff
    1 def fizzbuzz_generator (num) :
    i f num % 3 == 0 and num % 5 == 0:
    3 return ”FizzBuzz”
    e l i f num % 3 == 0:
    5 return ” Fizz ”
    e l i f num % 5 == 0:
    7 return ”Buzz”
    e l s e :
    9 return s t r (num)
    for i in range (1 , 21) :
    11 p r i nt ( fizzbuzz_generator ( i ) )
    23

    View full-size slide

  43. Exercise for Functions
    Exercise: Maximum of Three Numbers
    Write a function that returns the maximum of three numbers x,y and z.
    Tip Start with a function that returns the maximum of two numbers and then use that function
    inside the function that calculates the maximum of three.
    Tip The Signature should be
    1 def max_of_two(x , y) :
    . . .
    3
    def max_of_three (x , y , z ) :
    5 . . .
    24

    View full-size slide

  44. Exercise for Functions (contd.)
    Solution: Maximum of Three Numbers
    1 def max_of_two(x , y) :
    i f x > y :
    3 return x
    e l s e :
    5 return y
    7 def max_of_three (x , y , z ) :
    return max_of_two(x , max_of_two(y , z ) )
    9 p r i n t ( s t r ( max_of_three (9 , 4 , 3) ) )
    25

    View full-size slide

  45. Advanced Data structures

    View full-size slide

  46. Why do we need even more structures? Chaos is awesome!
    • We already have variables. What else could we need?
    • What if we don’t know the number of variables?
    • What if we have a list of variables?
    • What if a function should return more then one variable?
    ⇒ We need mooooore
    26

    View full-size slide

  47. Lists
    • lists save lists of objects
    1 l = [ ]
    • We can add objects of different types to lists
    1 l = [ ”One” , 2 , ” three ” , None ]
    p r i n t ( l )
    3 # [ ’One ’ , 2 , ’ three ’ , None ]
    • We can access list items by their index (Attention Index starts at 0)
    1 l = [ ”one” , ”two” , ” three ” ]
    p r i n t ( l [ 0 ] )
    3 # one
    27

    View full-size slide

  48. Lists (contd.)
    • We can append, manipulate and delete items from a list
    1 l = [ ”one” , ”two” , ” three ” ]
    # Remove the f i r s t element with VALUE one
    3 l . remove (”one”)
    # [” two” , ” three ”]
    5 # Remove the element at index 1
    del l [ 1 ]
    7 # [” two ”]
    # Append to the end of the l i s t
    9 l . append (” four ”)
    # [” two” , ” four ”]
    11 # Append at a s p e c i f i c posit ion
    l . i n s e r t (0 , ” s i x ”)
    13 # [” s i x ” , ”two” , ” four ”]
    # L i s t s can contain duplicates (more on t h i s l a t e r )
    15 l = [1 , 2 , 3]
    l . append (1)
    17 # [1 ,2 ,3 ,1]
    28

    View full-size slide

  49. for-loops revisited
    • You have been using lists all along
    1 # This function returns a l i s t
    range (0 , 10)
    3
    • We can iterate over a list using range and the number of elements
    l = [ ”one” , ”two” , ” three ” , ” four ” ]
    2 for i in range (0 , len ( l ) ) :
    p r i n t ( l [ i ] )
    4
    • You can also loop over lists with in (for-each syntax)
    1 l = [ ”one” , ”two” , ” three ” ]
    for element in l :
    3 p r i n t (”− ” + s t r ( element ) )
    29

    View full-size slide

  50. Where is waldo with lists
    • You can easily check if an item is in a list
    1 l = [ ”one” , ”two” , ” three ” ]
    i f ”one” in l :
    3 p r i n t (”One i s in the l i s t ”)
    30

    View full-size slide

  51. Exercises for Lists
    Exercise: Set theory for lists
    In set theory we have the intersection(all elements in both lists) and the union(all unique
    elements in both lists combined).
    Example:
    l1 = [1, 2, 3]
    l2 = [2, 3, 5]
    then
    l1 ∩ l2 = [2, 3]
    Write a function that calculate the intersection of lists of integers. Test with the following
    two lists:
    1 l1 = [1 , 2 , 3]
    l2 = [2 ,4 ,5]
    31

    View full-size slide

  52. Exercises for Lists
    Solution: Set theory for lists
    l1 = [1 , 2 , 3]
    2 l2 = [2 , 4 , 5]
    def i n t e r s e c t i o n ( l1 , l2 ) :
    4 s = [ ]
    for e in l1 :
    6 i f e in l2 :
    s . append ( e )
    8 return s
    10 p r i n t (”The i n t e r s e c t i o n i s ” + s t r ( i n t e r s e c t i o n ( l1 , l2 ) ) )
    32

    View full-size slide

  53. Dictionaries
    • Typical problem: Saving key-value pairs
    • Settings
    • User data (name ⇒ Marcel, age ⇒ 22)
    • Naiive idea: Two lists with coresponding indices
    keys = [ ]
    2 values = [ ]
    4 def get ( key , keys , values ) :
    for i in range (0 , len ( keys ) ) :
    6 i f key == keys [ i ] :
    return values [ i ]
    Just because it works that doesn’t mean that it is good
    • Problems?
    33

    View full-size slide

  54. Dictionaries
    • Typical problem: Saving key-value pairs
    • Settings
    • User data (name ⇒ Marcel, age ⇒ 22)
    • Naiive idea: Two lists with coresponding indices
    1 keys = [ ]
    values = [ ]
    3
    def get ( key , keys , values ) :
    5 for i in range (0 , len ( keys ) ) :
    i f key == keys [ i ] :
    7 return values [ i ]
    Just because it works that doesn’t mean that it is good
    • Problems?
    • python comes with a build-in data type for this
    33

    View full-size slide

  55. Dictionaries (contd.)
    • Initialization
    1 d = { ’ one ’ : ’ h e l l o ’ , ’two ’ : ’ world ’ , ’ three ’ : ’ ! ’}
    p r i n t (d [ ’ one ’ ] )
    3 p r i n t (d [ ’two ’ ] )
    p r i n t (d [ ’ three ’ ] )
    • We often want to access all elements in a dictionary
    for key , value in d . items () :
    2 p r i nt ( s t r ( key ) + ” =
    > ” + s t r ( value ) )
    # one =
    > h e l l o
    4 # two =
    > world
    # three =
    > !
    • Sometimes we want just the keys (or the values)
    1 d = { ’ one ’ : ’ h e l l o ’ , ’two ’ : ’ world ’ , ’ three ’ : ’ ! ’}
    for key in d . keys () :
    3 i f key == ”one” :
    p r i n t (”Value i s ” + s t r (d [ key ] ) )
    34

    View full-size slide

  56. Exercises for Dictionaries
    Exercise: Counting Words and Characters in a text
    Copy the text from the webex teams chat and count …
    a) The number of occurences for each letter
    b) The number of occurences for each word (ignoring upper and lowercase)
    Tip Use the split to split a space-seperated string into its words.
    a = ”This i s a text ”
    2 s p l i t = a . s p l i t (” ”)
    p r i n t ( s p l i t )
    4 # [ ’ This ’ , ’ i s ’ , ’a ’ , ’ text ’]
    Tip You can iterate over the letters in a string like it is a list
    a = ” spaces ”
    2 for char in a :
    p r i nt (” l e t t e r : ” + s t r ( char ) )
    4
    Tip Call the lower() method to convert a string into all lowercase. 35

    View full-size slide

  57. Exercises for Dictionaries (contd.)
    Solution: Counting Words and Characters in a text
    a) Number of letters
    1 long_string = ” . . . ”
    count = {}
    3 for l e t t e r in long_string :
    i f l e t t e r in count . keys () :
    5 count [ l e t t e r ] += 1
    e l s e :
    7 count [ l e t t e r ] = 1
    p r i n t (” I found the following l e t t e r s ”)
    9 p r i n t ( s t r ( count ) )
    36

    View full-size slide

  58. Exercises for Dictionaries (contd.)
    Solution: Counting Words and Characters in a text
    b) Number of words
    1 word_cnt = {}
    words = [ ]
    3 for w in long_string . s p l i t (” ”) :
    words . append ( s t r (w) . lower () )
    5
    for word in words :
    7 i f word in word_cnt . keys () :
    word_cnt [ word ] += 1
    9 e l s e :
    word_cnt [ word ] = 1
    11 p r i n t ( s t r ( word_cnt ) )
    37

    View full-size slide

  59. Input/Output

    View full-size slide

  60. Reading files
    • Reading files is as easy as eating a pie in python.
    1 f i l e _ i n = open (” te s t . txt ” , ” r ”)
    for l i n e in f i l e _ i n . r e a d l i n e s () :
    3 pr i nt (” Line : ” + s t r ( l i n e ) )
    f i l e _ i n . close ()
    38

    View full-size slide

  61. Writing files
    • Writing files is as easy as eating a pie in python.
    l i n e s = [ ” t h i s i s the f i r s t l i n e ” , ”This i s the second l i n e ” ]
    2 file_out = open (” test_out . txt ” , ”w”)
    for l i n e in l i n e s :
    4 file_out . write ( l i n e + ”\n”)
    file_out . close ()
    39

    View full-size slide

  62. Exercise for File I/O
    Exercise: A Simple Copy Software
    Write a simple script that copies a source file into the destination file. You only need to
    handle text files and can read the content.
    Tip Start with this code snippet
    1 IN_FILE_NAME = ”. txt ”
    OUT_FILE_NAME = ”. txt ”
    3
    f i l e _ i n = open (IN_FILE_NAME, ” r ”)
    5 file_out = open (OUT_FILE_NAME, ”w”)
    7 # Your code here
    40

    View full-size slide

  63. Übungen zu File I/O
    Solution: A Simple Copy Software
    1 IN_FILE_NAME = ”. txt ”
    OUT_FILE_NAME = ”. txt ”
    3
    f i l e _ i n = open (IN_FILE_NAME, ” r ”)
    5 file_out = open (OUT_FILE_NAME, ”w”)
    7 for l in f i l e _ i n . r e a d l i n e s () :
    out . write ( l )
    9
    f i l e _ i n . close ()
    11 file_out . close ()
    p r i n t (”Copied ” + s t r (IN_FILE_NAME) + ” to ” + s t r (OUT_FILE_NAME) )
    41

    View full-size slide

  64. The standard library

    View full-size slide

  65. Batteries included?
    • pythones comes with a lot of functionality included
    42

    View full-size slide

  66. Build-in functions
    • Don’t reinvent the wheel!
    • → Your programming skills are (very probably) worse then those who implemented the
    standard libary
    • Batteries included programming
    43

    View full-size slide

  67. random - Accessing the randomness
    import random
    • Get a random integer in a range
    1 rand_int = random . randint (0 , 100)
    • Random element from a list
    1 l = [ ”one” , ”two” , ” three ” ]
    rand_elem = random . choice ( l )
    44

    View full-size slide

  68. A (few) Words of Wisdom
    To think that you can become good at programming by watching a lecture or reading
    a book is like thinking that you can learn to drive by watching Formula One.
    - Unknown
    • This course is about the basics. Now you have to program.
    Fail early - Fail often
    - Facebook Engineering Mantra
    • Failing (and the associated searching of errors) is part of the programming experience!
    Programmieren ist wie Puzzle spielen nur das alle 5 Minuten ein wild gewordener Affe
    die Stücke wieder durcheinander bringt.
    - Unknown
    45

    View full-size slide

  69. Projects - Introduction
    • Following are three proposed programming projects you can work on
    • Projects one and two are slightly more complex (and math-heavy)
    • If you have your own idea: go with it
    • Feel free to collaborate on the projects
    46

    View full-size slide

  70. Projects - Introduction
    • Following are three proposed programming projects you can work on
    • Projects one and two are slightly more complex (and math-heavy)
    • If you have your own idea: go with it
    • Feel free to collaborate on the projects
    Walk-In Clinic
    Monday to Friday 11:15 - 12:00, 4th floor
    46

    View full-size slide

  71. Project 1 - Numerical Integration with Polynoms
    The goal of this project is to calculate the area under a polynom in the interval [0, 10]. The
    polynom is defined in a text file and is passed on to the program at runtime.
    $ python3 integrate . py polynom . txt
    As a reminder, polynoms can be defined by their coefficients as
    p(x) =
    n

    i=0
    αi
    xi
    and for integration the trapez rule defines, for a interval [c, d] and a function f
    d

    c
    f dx ≈ QT
    [c,d]
    (f) =
    (d − c)
    2
    [f(c) + f(d)]
    Bonus: For better precission you can use the summed trapez rule that uses N subintervals
    (xi, xi+1)∀i = 1, . . . , N + 1 mit xi = c + (i − 1)/h with fixed interval width h = (d−c)
    N
    47

    View full-size slide

  72. Project 2 - Least-Square Fitter
    The goal of this project is to calculate a best-fit line between datapoints. The datapoints(x,y
    pairs) should be read from a text file and the program should return the fit-parameters.
    As a reminder: We look at the over-determined system of linear equations (in matrix notation)
    n

    j=1
    Xijβj = yi , (i = 1, . . . , m)
    with m linear equations and n coefficients. In matrix notation: Xβ = y. Since the system is
    over-determined there is no gurantee that a perfect solution exists. We therefor look at the
    optimization problem
    ˆ
    β = arg min
    β
    S(β) with cost function S(β) = ||y − Xβ||2
    Some calculations then yield
    ˆ
    β = (XTX)−1XTy
    as a solution. Tip XTX is p.s.d. ⇒ Use Cholesky decomposition(C = LTL) that defines C−1 = (L)−1(LT)−1.
    L is a triangular matrix. Inversion follows trivially from back-substitution. 48

    View full-size slide

  73. Project 3 - Encrypting Text
    The goal of this project is to develop a simple encryption/decryption software based on the
    Caesar algorithm. The path to a single text file or a directory containing text files is passed to
    the program as an argument. The content of each of these files should then be encrypted and
    put into a .crypt.txt file. For encryption use the shift constant K that can be passed by the user.
    1 $ python3 caesar . py 3 docs/
    As a reminder: The Caesar algorithm uses a constant shift constant K. For a text each letter is
    shifted by that constant in the alphabet. As an example for K = 3 A ⇒ D , B ⇒ E , . . ..
    Bonus: Write another program that can decrypt the files again.
    Bonus: The caesar encryption is not cryptographically sound. If you have access to a sufficiently
    long text you can find the constant K by counting the occurences of each letter. The most
    common letter in the English language is the e. Write a program that ”cracks”your encryption.
    49

    View full-size slide

  74. Questions? Nothing working?
    [email protected]
    49

    View full-size slide

  75. Additional advanced data structures

    View full-size slide

  76. Tuples
    • tuples are immutable lists
    • In English: Once created the elements in a tuple can not be altered
    1 t = (1 , 2 , 3)
    t = ( ’ Marcel ’ , 22)
    • Accessing an element
    p r i n t (”The element i s ” + s t r ( t [ 0 ] ) )
    2
    • Tuples with one element still need a comma
    1 t = (1 ,)
    • We can’t add/remove/delete parts of a tuple ⇒ create a new one with parts of others
    1 t1 = (1 ,)
    t2 = (3 , 4 , 5)
    3 t = t1 + t2 [ 1 : 3 ]

    View full-size slide

  77. Sets
    • sets are unorderd, unique lists
    • In English: The order in which elements are added is ignored
    • In English: Every value occures only once in a set
    • Operations are same as on lists
    A = {1 , 2 , 3}
    2 A. add (4)
    # Discard w i l l ignore non−e x i s t i n g elements . remove w i l l throw an e r r o r
    4 A. discard (5)
    A. remove (5)
    • Basic set operations are also supported
    1 A = {1 , 2 , 3}
    B = {3 , 4 , 5}
    3 A. union (B)
    A. i n t e r s e c t i o n (B)
    5

    View full-size slide

  78. Additional parts of the standard library

    View full-size slide

  79. os - Access to the Operating System
    import os
    • Create a folder
    1 os . mkdir (” testordner ”)
    • Rename a folder or a file
    1 os . rename(” testordner ” , ” testordner2 ”)
    • Join paths
    1 path = os . path . j o i n (”/home/marcel” , ”Downloads”)
    • Delete files and folders
    1 os . remove (” datei . txt ”) # Delete f i l e
    os . rmdir (” testordner /”) # Delete f o l d e r
    3 os . rmdirs (” testornder /”) # Delete f o l d e r r e c u r s i v l y

    View full-size slide

  80. datetime - Calculating dates and times
    1 import datetime
    • Get current time
    1 now = datetime . datetime . now()
    • Specify a future time
    1 # year , month , day , hour , minute , seconds , microseconds
    l a t e r = datetime . datetime (2019 , 2 , 21 , 10 , 8 , 22 , 0)
    • Calculating differences
    delta = l a t e r − now
    2 p r i n t (”The d i f f e r e n c e in seconds i s ” + s t r ( delta . seconds ) )

    View full-size slide

  81. sys - Accessing system information
    import sys
    • Accessing command line parameters (i.e. when calling python3 test.py parameter1
    parameter2)
    1 p r i n t (”Name of the s c r i p t : ” + s t r ( sys . argv [ 0 ] ) )
    # In the example : te s t . py
    3 p r i n t (” F i r s t parameter : ” + s t r ( sys . argv [ 1 ] ) )
    # In the example : parameter1

    View full-size slide

  82. math - Basic mathematics
    import math
    • Constants like π, e, ...
    1 PI = math . pi
    • Triangular functions (note that all values are in radians)
    1 math . sin (0.5)
    math . cos (0.5)
    • Convert angle from radians to degrees and vice-versa
    math . degrees (0.5) # radians to degrees
    2 math . radians (290) # degrees to radians
    • Math functions
    math . pow(x , y) # x^y
    2 math . sqrt (x) # square root of x

    View full-size slide

  83. Useful resources

    View full-size slide

  84. Links
    • The python3 standard library:
    https://docs.python.org/3/library/
    • Installing python for development:
    Mac: https://docs.python-guide.org/starting/install3/osx/
    Windows: https://docs.python-guide.org/starting/install3/win/
    Linux: https://docs.python-guide.org/starting/install3/linux/
    • The core concepts of python:
    https://www.python-course.eu/python3_history_and_philosophy.php
    • Setup the atom editor for python development:
    https://hackernoon.com/
    setting-up-a-python-development-environment-in-atom-466d7f48e297

    View full-size slide