You are on page 1of 47

Python

introductie
Wat is programmeren?

 Programming is a way to “instruct the computer to perform various tasks”.

2
Wat is een programmeur?

3
Wat is Python

 Ontwikkelaar: Guido Van Rossum, Nederlandse computer scientist en wiskundige


 1989 - 1991
 Taal
 Communicatie person -> computer
 Communicatie tussen personen (programmeurs)
 Interpreted language
Voordelen van Python

 Portability (Windows, Linux en Mac)


 Logisch (logisch)
 Developer productiviteit (minder regels aan code)
 Uitgebreide “library”
 Applicatie kwaliteit (leesbaarheid, logisch en kwaliteit)
 Integratie
Nadeel van Python

 Langzamer dan gecompileerde talen (compiled languages: bijv. Java)


Wie gebruikt Python?

 Google
 Youtube
 Dropbox
 Netflix
 Battlefield 2
 Civilization IV
Python 2 vs Python 3

 Python 3 -> 2008


 Nog steeds applicaties die Python 2 gebruiken
 Niet alles van Python 3 is bruikbaar in python 2 of omgekeerd (compatible)
 If it aint broke don’t try to fix it
 Niet alle libraries zijn overgezet
 End of live 2.7 is 2020 en er zal geen 2.8 uitkomen
 Meest recente versie is 3.9
Installing Python

 Al geinstalleerd (volgens boek)


 Windows 10 via de store
 Linux
 Online ->
 https://codecollab.io
 https://www.onlinegdb.com/online_python_compiler
Virtualenv

 Virtuele omgeving
 Meerdere omgevingen
 sudoapt-get install python-virtualenv (linux)
 https://www.liquidweb.com/kb/how-to-setup-a-python-virtual-environment-on-windows-
10/ (windows)
Run Python

 Script (cron job)


 Interactive shell (IDLE, IDE)
 Als een service
 GUI (graphical user interface) applicatie
Python code organisation

 Files -> .py


 windows en Mac moeten settings aanpassen om extensie te zien
 Maximaal een paar honderd regels
 Files onderdeel van modules
 Modules onderdeel van packages
 Folder
 __init__.py (3.3 of lager)

 Don’t repeat yourself! (DRY)


Python execution model

 Names en Namespaces
 Scopes
 Objects en Classes
Names en Namespaces

 Objects geef je een naam


 Namespace is “mapping names to objects”
 Built-in names
 Global names (in a module)
 Local names (in a function)
Scope

 Local
 Enclosing
 Global
 Built-in
Objects en Classes

 Objecten zijn “instances of classes”


 Classes worden gebruikt om objecten te maken
 Definitie van een class -> class statement
Object met properties en methods

Object
Properties Methods
Color Kruipen
Length Zwemmen
Weight Bijten
Geboortedatum Wurgen
Giftig? Slapen
Programming conventions

 common conventions
 Naming
 Comment
 Indent style
 Reduction of complexity
 Refactoring
Pauze

 15 minutes
Built-inDataTypes

 Objects
 Mutable/immutable
 Numbers
 Sequences
 Set types
 Mapping types (dictionaries)
 Collections
 Enums
Objects

 Alles in Python is een object


 Krijgt een ID
 Objecten hebben:
 Id
 Type
 Value
 Mutable/immutable
Numbers

 Integers
 Booleans
 Real numbers
 Complex numbers
 Fractions en Decimals
Integers

 Integers must fit computer memory


 alle wiskundige functies uitvoerbaar (*, +, -, /, //, **, %)
 Positief, negatief of 0
 Afronden naar beneden -> int()
 Underscore voor het leesbaar maken van nummers 1_024
Booleans

 True or false
 1 of 0
 Combineren met “And”, “Or” en “Not”
Real Numbers

 double-precision binary floating-point format


 Other programming languages:
 Single (32-bit memory)
 Double (64-bit memory)
 Probleem met berekeningen
 0.3-0.1*3
Complex numbers

 science
Breuken en Decimalen

 From fractions import Fraction


 From decimal import Decimal as D
 Nog steeds het probleem met berekeningen
 Gebruik een ‘string’ als oplossing:
 From decimal import Decimal as D
 Decimal('0.3')-Decimal('0.1')*Decimal('3')
Herhaling

 Introductie
 Data Types
Beroepsproduct
Vragen

 ?
 ?
 ?
Agenda

 Herhaling college 1 (10 min)


 Installatie python
 Installatie pycharm
 Built-in data types vervolg (45 min)
 Iterating & making decisions (30 min)
 Functions, the building blocks of code (30 min)
 Afsluiting (15 min)
Immutable Sequences

 Strings
 Bytes
 Tuples
String

 Unicode code points


 Geen char type in Python
 Encode string->bytes object
 Single, double of triple quotes
 Triple quotes (meerdere regels)
Indexing and slicing strings

 Precise position (indexing)


 Subsequence (slicing)
Formatting strings

 % operator -> deprecated (niet meer gebruiken)


 Format string method (placeholders)
 Positional / Indexes
 Names
 String formatting
 f -> formatted string literals
Tuples

 Een rij van objecten


 Door comma’s gescheiden
Mutable Sequences

 List
 Byte array
List

 gebruik []
 List()
Byte Array
Set Types

 Lijkt veel op list


 Geen duplicaten
 Niet geordend

 frozenset
Mapping types-Dictionaries

 Koppelt sleutels (keys) aan waarden (values)


 Dictionaries zijn aanpasbaar
The collections module

 namedtuple
 defaultdict
 chainmap
Enums

 Niet echt “Built-in”


 Groep van gerelateerde waarden
Iterating & making Decisions

 Conditional programming
 Looping
Conditional programming

 If
 Else
 Elif

45
Looping

 For
 iterating
 While

46
Functions, the building blocks of code

 Doel van functies


 Scopes en name resolution
 Input parameters
 Return values

47

You might also like