# Datatypes

# Booleans

Boolean

Een boolean (Ned. Booleaanse waarde) heeft de waarde False of True en heeft als datatype bool.


 



# ./python/syntax/data_types/booleans__1.py
b = False

print(b, type(b))
1
2
3
4
$ python3 ./python/syntax/data_types/booleans__1.py
False <class 'bool'>
$ _

Een ander datatype casten (Eng. cast) naar een boolean doe je met de functie bool()



 




# ./python/syntax/data_types/booleans__2.py
i = 1
b = bool(i)

print(i, type(i))
print(b, type(b))
1
2
3
4
5
6
$ python3 ./python/syntax/data_types/booleans__2.py
1 <class 'int'>
True <class 'bool'>
$ _


 




# ./python/syntax/data_types/booleans__2.py
s = 'true'
b = bool(s)

print(s, type(s))
print(b, type(b))
1
2
3
4
5
6
$ python3 ./python/syntax/data_types/booleans__3.py
true <class 'str'>
True <class 'bool'>
$ _

# Numeric Types

Type EN NL
int Integer Geheel getal
float Floating point number Vlottendekommagetal
complex Complex number Complex getal

# Integers

Een integer (Ned. geheel getal) is een positief of negatief getal zonder komma en heeft als datatype int.


 




# ./python/syntax/data_types/integers__1.py
i = -123

print(i)
print(type(i))
1
2
3
4
5
$ python3 ./python/syntax/data_types/integers__1.py
-123
<class 'int'>
$ _

Een ander datatype casten (Eng. cast) naar een integer doe je met de functie int()


 




# ./python/syntax/data_types/integers__2.py
i = int("789")

print(i)
print(type(i))
1
2
3
4
5
$ python3 ./python/syntax/data_types/integers__2.py
789
<class 'int'>
$ _

# Floating-Point Numbers


 




# ./python/syntax/data_types/floats__1.py
f = 1.23

print(f)
print(type(f))
1
2
3
4
5
$ python3 ./python/syntax/data_types/floats__1.py
1.23
<class 'float'>
$ _

 




# ./python/syntax/data_types/floats__2.py
f = float('-789')

print(f)
print(type(f))
1
2
3
4
5
$ python3 ./python/syntax/data_types/floats__2.py
-789.0
<class 'float'>
$ _

# Complex Numbers


 




# ./python/syntax/data_types/complex_numbers__1.py
c = 1 + 23j

print(c)
print(type(c))
1
2
3
4
5
$ python3 ./python/syntax/data_types/complex_numbers__1.py
(1+23j)
<class 'complex'>
$ _

 




# ./python/syntax/data_types/complex_numbers__2.py
c = complex(-78 - 9j)

print(c)
print(type(c))
1
2
3
4
5
$ python3 ./python/syntax/data_types/complex_numbers__2.py
(-78-9j)
<class 'complex'>
$ _

# Sequence Types

Type Ordered Indexed Mutable Item Data Type
list om het even
tuple om het even
range enkel int

Sequence

Een sequence (Ned. sequentie, opeenvolging) is een geordende en geïndexeerde opeenvolging van gegevens.

# Lists

List

Een list (Ned. lijst) is een wijzigbare (Eng. mutable) sequence.


 




# ./python/syntax/data_types/lists__1.py
ls = [123, 1.23, 'Lorem ipsum dolor sit amet.']

print(ls)
print(type(ls))
1
2
3
4
5
$ python3 ./python/syntax/data_types/lists__1.py
[789, 78.9, 'Lorem ipsum dolor sit amet.']
<class 'list'>
$ _

Elk item van de array heeft een index. Het eerste item heeft als index 0.


 









# ./python/syntax/data_types/lists__2.py
ls = ['l', 'o', 'r', 'e', 'm', ' ', 'i', 'p', 's', 'u', 'm']

print('1.', ls[0])
print('2.', ls[1])
print('3.', ls[-1])
print('4.', ls[:5])
print('5.', ls[4:7])
print('6.', ls[6:])
print('7.', ls[0::3])
1
2
3
4
5
6
7
8
9
10
$ python3 ./python/syntax/data_types/lists__2.py
1. l
2. o
3. m
4. ['l', 'o', 'r', 'e', 'm']
5. ['m', ' ', 'i']
6. ['i', 'p', 's', 'u', 'm']
7. ['l', 'e', 'i', 'u']
$ _

# Methoden

  • «list».append(«value»)
  • «list».clear()
  • «list».copy()
  • «list».count(«value»)
  • «list».extend(«list»)
  • «list».index(«value»)
  • «list».insert(«index», «value»)
  • «list».pop()
  • «list».pop(«index»)
  • «list».remove(«value»)
  • «list».reverse()
  • «list».sort()

# Tuples

Tuple

Een tuple (Ned. tupel) is een onwijzigbare (Eng. immutable) sequence.

Behalve dat een tuple onwijzigbaar is, is er geen verschil met een list.


 




# ./python/syntax/data_types/tuples__.py
t = (789, 78.9, 'Lorem ipsum dolor sit amet.')

print(t)
print(type(t))
1
2
3
4
5
$ python3 ./python/syntax/data_types/tuples__1.py
(789, 78.9, 'Lorem ipsum dolor sit amet.')
<class 'tuple'>
$ _

 









# ./python/syntax/data_types/tuples__2.py
t = ('l', 'o', 'r', 'e', 'm', ' ', 'i', 'p', 's', 'u', 'm')

print('1.', t[0])
print('2.', t[1])
print('3.', t[-1])
print('4.', t[:5])
print('5.', t[4:7])
print('6.', t[6:])
print('7.', t[0::3])
1
2
3
4
5
6
7
8
9
10
$ python3 ./python/syntax/data_types/tuples__2.py
1. l
2. o
3. m
4. ('l', 'o', 'r', 'e', 'm')
5. ('m', ' ', 'i')
6. ('i', 'p', 's', 'u', 'm')
7. ('l', 'e', 'i', 'u')
$ _

# Methoden

  • «tuple».count(«value»)
  • «tuple».index(«value»)

# Ranges

Range

Een range (Ned. reeks) is een onwijzigbare (Eng. immutable) opeenvolging van gehele getallen (Eng. integers).



 





# ./python/syntax/data_types/ranges__1.py
stop = 5
r = range(stop)

print(r)
print(type(r))
print(list(r))
1
2
3
4
5
6
7
$ python3 ./python/syntax/data_types/ranges__1.py
range(0, 5)
<class 'range'>
[0, 1, 2, 3, 4]
$ _



 





# ./python/syntax/data_types/ranges__2.py
start = 1
stop = 6
r = range(start, stop)

print(r)
print(type(r))
print(list(r))
1
2
3
4
5
6
7
8
$ python3 ./python/syntax/data_types/ranges__2.py
range(1, 6)
<class 'range'>
[1, 2, 3, 4, 5]
$ _




 





# ./python/syntax/data_types/ranges__3.py
start = 5
stop = 0
step = -1
r = range(start, stop, step)

print(r)
print(type(r))
print(list(r))
1
2
3
4
5
6
7
8
9
$ python3 ./python/syntax/data_types/ranges__3.py
range(5, 0, -1)
<class 'range'>
[5, 4, 3, 2, 1]
$ _

Tip

Als je een range met floats nodig hebt, dan kan je numpy.arange() gebruiken. Je moet de module NumPy wel nog installeren met pip.

# Methoden

  • «range».count(«value»)
  • «range».index(«value»)
  • «range».start(«int»)
  • «range».step(«int»)
  • «range».stop(«int»)

# Text Sequences

Text Sequence

Een text sequence (Ned. tekenreeks) is een wijzigbare (Eng. mutable) sequence van tekens (Eng. characters).

# Enkele lijn


 




# ./python/syntax/data_types/strings__1.py
s = 'Lorem ipsum dolor sit amet.'

print(s)
print(type(s))
1
2
3
4
5
$ python3 ./python/syntax/data_types/strings__1.py
Lorem ipsum dolor sit amet.
<class 'str'>
$ _

 




# ./python/syntax/data_types/strings__2.py
s = "Lorem ipsum dolor sit amet."

print(s)
print(type(s))
1
2
3
4
5
$ python3 ./python/syntax/data_types/strings__2.py
Lorem ipsum dolor sit amet.
<class 'str'>
$ _

# Meerdere lijnen


 





# ./python/syntax/data_types/strings__3.py
s = '''Lorem ipsum
       dolor sit amet.'''

print(s)
print(type(s))
1
2
3
4
5
6
$ python3 ./python/syntax/data_types/strings__3.py
Lorem ipsum
      dolor sit amet.
<class 'str'>
$ _

 





# ./python/syntax/data_types/strings__4.py
s = """Lorem ipsum
       dolor sit amet."""

print(s)
print(type(s))
1
2
3
4
5
6
$ python3 ./python/syntax/data_types/strings__4.py
Lorem ipsum
      dolor sit amet.
<class 'str'>
$ _

# Speciale tekens

Escape Sequence Betekenis Teken
\' Single quote '
\" Double quote "
\\ Backslash \
\a ASCII Bell BEL
\b ASCII Backspace BS
\f ASCII Formfeed FF
\n ASCII Linefeed LF
\r ASCII Carriage Return CR
\t ASCII Horizontal Tab TAB
\v ASCII Vertical Tab VT

# Functies

  • len(«str»)

# Methoden

Een str heeft een aantal methoden:

  • Kast (Eng. case)
    • «str».capitalize()
    • «str».lower() en «str».islower()
    • «str».swapcase()
    • «str».title() en «str».istitle()
    • «str».upper() en «str».isupper()
  • Zoeken en vervangen
    • «str».endswith()
    • «str».find()
      • «str».lfind()
      • «str».rfind()
    • «str».index()
      • «str».lindex()
      • «str».rindex()
    • «str».replace()
    • «str».startswith()
  • Samenvoegen en splitsen
    • «str».join()
    • «str».partition()
      • «str».lpartition()
      • «str».rpartition()
    • «str».split()
      • «str».lsplit()
      • «str».rsplit()
  • Witruimte
    • «str».center()
      • «str».ljust()
      • «str».rjust()
    • «str».strip()
      • «str».lstrip()
      • «str».rstrip()
    • «str».isspace()
  • Overige
    • «str».clear()
    • «str».copy()
    • «str».count()
    • «str».encode()
    • «str».format()

Geraadpleegde bronnen

# Set Types

Type Ordered Indexed Mutable
set
frozenset

Sets zijn niet geordend, ze onthouden niet in welke volgorde iets is toegevoegd.

# Set


 






# ./python/syntax/data_types/sets.py
s = {1, 2.0, 'drie'}

print(s)
print(type(s))
for item in s:
    print(item, type(item))
1
2
3
4
5
6
7
$ python3 ./python/syntax/data_types/sets.py
{1, 2.0, 'drie'}
<class 'set'>
1 <class 'int'>
2.0 <class 'float'>
drie <class 'str'>
$ _

# Functies

  • len(«set»)

# Methoden

Een set heeft een aantal methoden:

  • «set».add(«element»)
  • «set».clear()
  • «set».copy()
  • «set».discard(«element»)
  • «set».pop()
  • «set».remove(«element»)

Geraadpleegde bronnen

# Frozenset

Kan gemaakt worden van een list, tuple, set of dict.

# Mapping Types

Type Ordered Indexed Mutable
dict

# Dictionaries

Een dictionary (Ned. woordenlijst) is een geordende set (volgens de volgorde van aanmaak) van key-value pairs (Eng. sleutel-waarde-paren.


 






# ./python/syntax/data_types/dictionaries__1.py
d = dict(one=1, two=2.0, three='drie')

print(d)
print(type(d))
for key in d:
    print(key, d[key], type(d[key]))
1
2
3
4
5
6
7
$ python3 ./python/syntax/data_types/dictionaries__1.py
{'one': 1, 'two': 2.0, 'three': 'drie'}
<class 'dict'>
one 1 <class 'int'>
two 2.0 <class 'float'>
three drie <class 'str'>
$ _

 






# ./python/syntax/data_types/dictionaries__2.py
d = {'one': 1, 'two': 2.0, 'three': 'drie'}

print(d)
print(type(d))
for key in d:
    print(key, d[key], type(d[key]))
1
2
3
4
5
6
7
$ python3 ./python/syntax/data_types/dictionaries__2.py
{'one': 1, 'two': 2.0, 'three': 'drie'}
<class 'dict'>
one 1 <class 'int'>
two 2.0 <class 'float'>
three drie <class 'str'>
$ _

 










# ./python/syntax/data_types/dictionaries__3.py
d = dict([
    ('one', 1),
    ('two', 2.0),
    ('three', 'drie'),
    ])

print(d)
print(type(d))
for key in d:
    print(key, d[key], type(d[key]))
1
2
3
4
5
6
7
8
9
10
11
$ python3 ./python/syntax/data_types/dictionaries__3.py
{'one': 1, 'two': 2.0, 'three': 'drie'}
<class 'dict'>
one 1 <class 'int'>
two 2.0 <class 'float'>
three drie <class 'str'>
$ _

 










# ./python/syntax/data_types/dictionaries__4.py
d = dict({
    'one': 1,
    'two': 2.0,
    'three': 'drie',
    })

print(d)
print(type(d))
for key in d:
    print(key, d[key], type(d[key]))
1
2
3
4
5
6
7
8
9
10
11
$ python3 ./python/syntax/data_types/dictionaries__4.py
{'one': 1, 'two': 2.0, 'three': 'drie'}
<class 'dict'>
one 1 <class 'int'>
two 2.0 <class 'float'>
three drie <class 'str'>
$ _

# Functies

  • len(«dict»)
  • list(«dict»)

# Methoden

Een set heeft een aantal methoden:

  • «dict».clear()
  • «dict».copy()
  • «dict».get(«key»)
  • «dict».items()
  • «dict».keys()
  • «dict».pop(«key»)
  • «dict».popitem()
  • «dict».values()

# Bronnen

© 2024 Arteveldehogeschool Laatst bijgewerkt: 14/2/2020, 11:07:52