programmeren

in python

Jaar 3 - Module 0 - Les 1: Python achtergrond

VANDAAG GAAN WE...

 

  • Meer theorie leren over Python

  • Leren wat een datatype is

  • Leren welke datatypes er zijn

  • Leren waarom een datatype belangrijk is

Variabelen

 

  • Een variabele in Python

a = 1928
print(a)
1928
b = 'blablabla'
print(b)
blablabla
c = 3.14
print(c)
3.14
d = ['blabla', 'bliblibli', 
     'blobloblo']
print(d)
['blabla', 'bliblibli', 
 'blobloblo']

Variabelen en datatype

 

  • Elke variabele heeft een datatype

a = 1928
print(a)
print(type(a))
1928
<class 'int'>
b = 'blablabla'
print(b)
print(type(b))
blablabla
<class 'str'>
c = 3.14
print(c + 1)
print(type(c))
4.14
<class 'float'>
d = ['blabla', 'bliblibli', 
     'blobloblo']
print(d+1)
print(type(d))
['blabla', 'bliblibli', 
 'blobloblo']
<class 'list'>

Variabelen en datatype

 

  • Het datatype bepaalt wat je kan doen

  • Tel één erbij op (+1)

a = 1928
print(a+1)
print(type(a))
1929
<class 'int'>
b = 'blablabla'
print(b+1)
print(type(b))
TypeError: can only concatenate str (not "int") to str
<class 'str'>
c = 3.14
print(c+1)
print(type(c))
4.14
<class 'float'>
d = ['blabla', 'bliblibli', 
     'blobloblo']
print(d+1)
print(type(d))
TypeError: can only concatenate list (not "int") to list
<class 'list'>

Variabelen en datatype

  • Het datatype bepaalt wat je kan doen

  • vermengivuldig met 3 (*3)

a = 1928
print(a * 3)
print(type(a))
5784
<class 'int'>
b = 'blablabla'
print(b * 3)
print(type(b))
'blablablablablablablablabla'
<class 'str'>
c = 3.14
print(c * 3)
9.42
<class 'float'>
d = ['blabla', 'bliblibli', 
     'blobloblo']
print(d * 3)
['blabla', 'bliblibli', 'blobloblo', 
 'blabla', 'bliblibli', 'blobloblo', 
 'blabla', 'bliblibli', 'blobloblo']
<class 'list'>

demo

Waarom belangrijk?

  • Herken fouten
lijst = ['fiets', 'auto', 'step']
print(lijst[0])
print(lijst[2.0])
fiets
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(lijst[2.0])
TypeError: list indices must be integers or slices, not float

Waarom belangrijk?

  • Herken fouten
import turtle
pen = turtle.Turtle()

pen.forward('100')
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    pen.forward('100')
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/turtle.py", line 1638, in forward
    self._go(distance)
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/turtle.py", line 1605, in _go
    ende = self._position + self._orient * distance
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/turtle.py", line 257, in __mul__
    return Vec2D(self[0]*other, self[1]*other)
TypeError: can't multiply sequence by non-int of type 'float'

Waarom belangrijk?

  • Mogelijkheden, wat kan ik met een variabele?
lijst = ['kaas', 'worst', 'aardappel']
lijst.remove('kaas')
print(lijst)

woord = 'het gaat goed'
woord.remove('goed')
print(woord)
['worst', 'aardappel']
Traceback (most recent call last):
  File "main.py", line 6, in <module>
    woord.remove('goed')
AttributeError: 'str' object has no attribute 'remove'

opdracht

  • Maak een nieuw excel bestand
     
  • Voeg de kolommen 'variabele' en 'datatype' toe
     
  • Gebruik je script van de simulatie, vul in excel alle variabelen en het datatype in die je in je script gebruikt

opdracht

import turtle
pen = turtle.Turtle()

afstand = 100
hoek = 35.9
for i in range(25):
  pen.forward(afstand)
  pen.left(hoek)
  • Maak een nieuw excel bestand
     
  • Voeg de kolommen 'variabele' en 'datatype' toe
     
  • Gebruik je script van de simulatie, vul in excel alle variabelen en het datatype in die je in je script gebruikt

opdracht

  • Stuur je excel naar mij op voor dinsdag 16:59
Made with Slides.com