Contents
1. Input & Input Functions........................................................................................................................................................................... 6
2. Variables, Datatypes & Operators........................................................................................................................................................ 6
2.1 Basic Datatypes for variables......................................................................................................................................................... 6
2.2 Some predefined functions : Built-in functions....................................................................................................................... 6
2.3 Hoe type van een variabele kennen?........................................................................................................................................... 7
2.4 Variable names..................................................................................................................................................................................... 8
2.5 Overschakelen van datatype........................................................................................................................................................... 8
2.6 Some other built-in functions......................................................................................................................................................... 9
2.7 Operators................................................................................................................................................................................................ 9
2.7.1 Arithmetic Operators : + - * / // ** %................................................................................................................................. 9
2.7.2 Assignments Operators : = += -= *= /= //= **= %=.................................................................................................... 10
2.7.3 Comparison Operators : < > <= >= == !=......................................................................................................................... 10
2.7.4 Logical Operators : and or not............................................................................................................................................. 11
2.8 Exercices on Variables & Operators.......................................................................................................................................... 11
Oefening 1 : Comparaison................................................................................................................................................................ 11
Oefening 2 : Quiz.................................................................................................................................................................................. 12
Oefening 3 : Quiz.................................................................................................................................................................................. 12
Oefening 4 : Average Electricity Bill............................................................................................................................................ 12
Oefening 5 : Calculations for a tiler.............................................................................................................................................. 12
Oefening 6 : Assign and Modify Variables................................................................................................................................. 13
Oefening 7 : Sales for the week...................................................................................................................................................... 13
3. Working with strings.............................................................................................................................................................................. 14
3.1 Create a string.................................................................................................................................................................................... 14
3.2 Escape Characters............................................................................................................................................................................ 15
3.3 String Operators: + en *.................................................................................................................................................................. 16
3.4 Length of a string = number of characters............................................................................................................................. 16
3.5 String Functions / Methods.......................................................................................................................................................... 17
# allemaal kleine letters................................................................................................................................................................... 17
# Boolean Vraag : allemaal kleine letters ? -> booleaanse vraag !!!................................................................................ 17
# allemaal hoofdletters..................................................................................................................................................................... 17
# Boolean Vraag : allemaal hoofdletters ?................................................................................................................................. 17
# Enkel eerste letter VAN HET EERSTE WOORD VAN DE ZIN in hoofdletter.............................................................17
# enkel eerste letter VAN ELK WOORD is hoofdletter......................................................................................................... 17
# use functions one after the other.............................................................................................................................................. 18
Fadia Farhat | VUB 2021-20221
, # check if numbers inside a string............................................................................................................................................... 18
# strip methode................................................................................................................................................................................... 18
# string.strip(characters) -> Characters to remove.............................................................................................................. 18
# Strings are indexed -> all characters have an index.......................................................................................................... 20
# Werken met 2 parameters........................................................................................................................................................... 20
# Format methods for strings........................................................................................................................................................ 20
# Join-Method....................................................................................................................................................................................... 21
# Split-Method...................................................................................................................................................................................... 22
# to call a character => put his index into [ ]............................................................................................................................ 22
# Indexing and Slicing....................................................................................................................................................................... 22
# Membership operators => Boolean question...................................................................................................................... 23
3.6 Exercices on Strings......................................................................................................................................................................... 23
Oefening 1 : Write a Server Log Message.................................................................................................................................. 23
Oefening 2 : Concatenation............................................................................................................................................................. 23
4. Working with Booleans......................................................................................................................................................................... 24
5. Working with numbers (float & integer)........................................................................................................................................ 25
5.1 Volgorde van de bewerkingen..................................................................................................................................................... 25
6. Datastructures........................................................................................................................................................................................... 25
6.1 Lists........................................................................................................................................................................................................ 25
6.1.1 Wat is er zo speciaal aan een list ?..................................................................................................................................... 26
6.1.2 Kenmerken lijst......................................................................................................................................................................... 26
6.1.3 Lijst aanmaken.......................................................................................................................................................................... 26
6.1.4 Datatype van een list ?........................................................................................................................................................... 27
6.1.4 Wat kan je doen met een lijst ?........................................................................................................................................... 27
6.1.5 Python Building Blocks for Lists........................................................................................................................................ 27
6.2 Tuple...................................................................................................................................................................................................... 31
6.2.1 Kenmerken van een Tuple.................................................................................................................................................... 31
6.2.2 Tuple aanmaken....................................................................................................................................................................... 31
6.2.3 Access attributes of the tuple.............................................................................................................................................. 32
6.2.4 Python Blocks for tuples....................................................................................................................................................... 32
6.3 Set........................................................................................................................................................................................................... 32
6.3.1 Kenmerken van een Set......................................................................................................................................................... 32
6.3.2 Set aanmaken............................................................................................................................................................................. 33
6.3.3 Python Blocks for Sets............................................................................................................................................................ 33
6.3.4 Sets gebruiken als verzamelingen.......................................................................................................................................... 33
6.4 Dictionary............................................................................................................................................................................................ 34
6.4.1 Dictionary Aanmaken............................................................................................................................................................. 34
6.4.2 Nagaan op welke plaats een “kolom” of de key met zijn waarde staat...............................................................34
Fadia Farhat | VUB 2021-20221
, 6.4.3 Kolom (= element of key met zijn waarde) toevoegen............................................................................................. 34
6.4.4 Nagaan of een keynaam in dictionary bestaat (kolom-naam)............................................................................... 35
6.4.5 Weergeven van alles keys = titels van de kolommen................................................................................................ 35
6.4.6 Weergeven van alle waarden van de dictionary.......................................................................................................... 35
6.4.7 Bepaalde waarde oproepen met zijn key....................................................................................................................... 35
6.4.8 Waarden oproepen met de .get() methode.................................................................................................................... 35
6.4.9 Pop Methode.............................................................................................................................................................................. 35
6.4.10 Weergeven van alle elementen van de dictionary (key met waarde)..............................................................35
6.4.11 Weergeven van de values................................................................................................................................................... 35
6.4.12 Weergeven van de keys....................................................................................................................................................... 36
6.4 Compound Data Structures.......................................................................................................................................................... 36
6.4.1 Dictionary in Dictionary........................................................................................................................................................ 36
6.4.2 Tuple in List................................................................................................................................................................................ 36
6.4.3 Tuple in Dictionary.................................................................................................................................................................. 36
6.5 Summary.............................................................................................................................................................................................. 37
6.7 Excercices............................................................................................................................................................................................ 37
Oefening1 : Quiz................................................................................................................................................................................... 37
Oefening 2 : List Indexing................................................................................................................................................................ 37
Oefening 3 : Slicing Lists................................................................................................................................................................... 38
Oefening 4 : Count, Split & Convert.............................................................................................................................................. 38
Oefening 5 : Data Structures........................................................................................................................................................... 39
Oefening 6 : Sentence Dictionary.................................................................................................................................................. 40
7 Selecties........................................................................................................................................................................................................ 41
7.1 Conditional Statements.................................................................................................................................................................. 41
7.1.1 if...................................................................................................................................................................................................... 41
7.1.2 elif ( if elif)................................................................................................................................................................................... 41
7.1.3 else with elif (if elif else)....................................................................................................................................................... 41
7.1.4 else without elif (if else)........................................................................................................................................................ 42
7.2 Ternary Operators or Conditional Expressions................................................................................................................... 42
7.2.1 Short Hand If.............................................................................................................................................................................. 42
7.2.2 Short Hand If Else..................................................................................................................................................................... 42
7.2.3 And................................................................................................................................................................................................. 42
7.2.4 Or.................................................................................................................................................................................................... 42
7.2.5 Nested If....................................................................................................................................................................................... 42
7.2.6 The Pass Statement................................................................................................................................................................. 43
7.3 Excercises on Conditional Statements..................................................................................................................................... 43
Oefening 1 : Which Prize.................................................................................................................................................................. 43
Oefening 2 : Compare Guess to Result........................................................................................................................................ 43
Fadia Farhat | VUB 2021-20221
, Oefening 3 : VAT.................................................................................................................................................................................. 44
Oefening 4 : improve statements.................................................................................................................................................. 44
Oefening 5 : Fixing Errors................................................................................................................................................................ 44
8 Iteraties (Loops)........................................................................................................................................................................................ 46
8.1 While-Loop.......................................................................................................................................................................................... 46
8.1.1 The While.................................................................................................................................................................................... 46
8.1.2 The else-statement.................................................................................................................................................................. 46
8.1.3 Break............................................................................................................................................................................................. 46
8.1.4 Continue....................................................................................................................................................................................... 47
8.2 For-Loop............................................................................................................................................................................................... 47
8.2.1 For in a range............................................................................................................................................................................. 47
8.2.2 For in a string............................................................................................................................................................................. 48
8.2.3 For in a list.................................................................................................................................................................................. 49
Exercises on Loops.................................................................................................................................................................................. 49
Oefening 1 : Even Getallen............................................................................................................................................................... 49
Oefening 2 : Oneven getallen.......................................................................................................................................................... 49
Oefening 3 : Examples – FOR loop................................................................................................................................................ 49
Oefening 4 : Examples Building dictionaries............................................................................................................................ 50
Oefening 5 : Factorials with While loops................................................................................................................................... 50
Oefening 6 : Factorials with for loops......................................................................................................................................... 51
Oefening 7 : Exercise For Loops – Usernames......................................................................................................................... 51
Oefening 8 : Iterating through Dictionaries - Fruit Basket (Count Method)...............................................................52
Oefening 9 : While Loops - Count by........................................................................................................................................... 53
Oefening 10 : While Loops - Nearest Square............................................................................................................................ 53
9 Procedures and/or Functions.............................................................................................................................................................. 54
9.1 Predefined functions....................................................................................................................................................................... 54
9.2 Self-defined functions..................................................................................................................................................................... 54
# Functions performing a task or procedure........................................................................................................................... 54
# Functions with an output value................................................................................................................................................. 54
# Writing documentation................................................................................................................................................................ 55
# Make an argument optional........................................................................................................................................................ 55
# Using None as a default argument............................................................................................................................................ 55
9.3 Exercices on Functions................................................................................................................................................................... 56
Oefening 1 : Function: population_density............................................................................................................................... 56
Oefening 2 : Function: readable_timedelta............................................................................................................................... 56
Oefening 3 : Function: divisible by 9........................................................................................................................................... 57
Oefening 4 : Football.......................................................................................................................................................................... 58
10 Reading & Writing files........................................................................................................................................................................ 59
Fadia Farhat | VUB 2021-20221