Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Introduction

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

6

Slide 14

Slide 14 text

Variables and Datatypes

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Datatypes • Python is not statically typed 8

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Loops and Control Structures

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Functions

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Advanced Data structures

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Input/Output

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Ü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

Slide 66

Slide 66 text

The standard library

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

Projects 44

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

Questions? Nothing working? [email protected] 49

Slide 78

Slide 78 text

Additional advanced data structures

Slide 79

Slide 79 text

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 ]

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

Additional parts of the standard library

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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 ) )

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

Useful resources

Slide 87

Slide 87 text

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