160 lines
2.9 KiB
Plaintext
160 lines
2.9 KiB
Plaintext
Base types
|
|
==========
|
|
|
|
- types_
|
|
|
|
.. _types: https://realpython.com/python-data-types/
|
|
|
|
|
|
Python is dynamically typed
|
|
----------------------------
|
|
|
|
it means
|
|
|
|
- variables don't have fixed type, the types can change
|
|
- the variable's type is specified at runtime during the assignment
|
|
|
|
>>> a = 1
|
|
>>> type(a)
|
|
<class 'int'>
|
|
>>> a = 2.
|
|
>>> a
|
|
2.0
|
|
>>> type(a)
|
|
<class 'float'>
|
|
>>> b = "b"
|
|
>>> type(b)
|
|
<class 'str'>
|
|
>>> c = "3"
|
|
>>> type(c)
|
|
<class 'str'>
|
|
>>> int(c)
|
|
3
|
|
>>> c2 = int(c)
|
|
>>> c2
|
|
3
|
|
>>> type(c2)
|
|
<class 'int'>
|
|
>>>
|
|
|
|
Some builtin types
|
|
------------------
|
|
|
|
base types:
|
|
|
|
- integer,
|
|
- float,
|
|
- string,
|
|
- boolean
|
|
|
|
base container types:
|
|
|
|
- list,
|
|
- dictionnary,
|
|
- set
|
|
|
|
list type sample::
|
|
|
|
>>> l = range(10)
|
|
>>> l
|
|
range(0, 10)
|
|
>>> for i in l:
|
|
... print(i)
|
|
...
|
|
0
|
|
(...)
|
|
9
|
|
|
|
dictionary type sample::
|
|
|
|
>>> d = dict(a=2, b=5)
|
|
>>> d
|
|
{'b': 5, 'a': 2}
|
|
>>> type(d)
|
|
<class 'dict'>
|
|
|
|
a dict **is not** a ordered type. If you need an ordered dict, use a more
|
|
advanced collection type like an ordered dict
|
|
|
|
>>> from collections import OrderedDict
|
|
>>> d = OrderedDict({'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2})
|
|
>>> d
|
|
OrderedDict([('orange', 2), ('apple', 4), ('banana', 3), ('pear', 1)])
|
|
>>>
|
|
|
|
set sample::
|
|
|
|
>>> set(range(10))
|
|
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
|
|
a set **is not** ordered
|
|
|
|
>>> s = set(range(10))
|
|
>>> 3 in s
|
|
True
|
|
|
|
Mutable types and non-mutable types
|
|
-------------------------------------
|
|
|
|
A set is immutable, whereas a list is mutable::
|
|
|
|
>>> s = set([1,2])
|
|
>>> l = [1,2]
|
|
>>> l.append(3)
|
|
>>> list(l)
|
|
[1, 2, 3]
|
|
>>> s.append(3)
|
|
Traceback (most recent call last):
|
|
File "<stdin>", line 1, in <module>
|
|
AttributeError: 'set' object has no attribute 'append'
|
|
>>>
|
|
|
|
A string is immutable. If you want to modify it, build another string:
|
|
|
|
>>> s = "hello"
|
|
>>> s.replace('ll', 'bb')
|
|
'hebbo'
|
|
>>> s+"you"
|
|
'helloyou'
|
|
>>> s
|
|
'hello'
|
|
|
|
you can see that s hasn't changed.
|
|
|
|
Even base types are objects
|
|
---------------------------
|
|
|
|
It means that in python, classes are types (let's understand it like that at first glance)
|
|
|
|
>>> a = 2
|
|
>>> isinstance(a, int)
|
|
True
|
|
>>> s = "a"
|
|
>>> dir(s)
|
|
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
|
|
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
|
|
(...)
|
|
'__init__', '__iter__', '__le__', '__len__', '__lt__', ]
|
|
|
|
`s` has methods. It's an objects. The type of s is the class `int`.
|
|
|
|
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}
|
|
>>> isinstance(d, dict)
|
|
True
|
|
>>>
|
|
|
|
special methods: the iterator sample
|
|
|
|
>>> l = range(10)
|
|
>>> for i in l:
|
|
... print(i)
|
|
|
|
We can use the `for` statement only because the `list` type has a special `__iter__`
|
|
method::
|
|
|
|
>>> l.__iter__()
|
|
<range_iterator object at 0x7f28b76e1e10>
|
|
>>>
|
|
|
|
we will see the special methods later.
|