class: center, middle # Standard data types in Python https://github.com/jbub/spy-data-types --- # Agenda 1. Type system of Python 2. None 3. Booleans 4. Numbers 5. String 6. Lists 7. Tuples 8. Sets 9. Dictionaries 10. Ranges 12. Iterators and generators 13. Collections --- # Type system of Python Python is **strongly** typed. - every change of type requires explicit conversion ```python # this will fail a = 3 + "45" ``` Python is **dynamically** typed. - types are checked at runtime - duck typing ```python # assign an integer a = 4 # then change it to string a = "hello" # duck a.lower() ``` Everything in Python is an **object**. - every object has **identity**, **type** and **value** --- # None - there is a single object with this value - it is used to signify the absence of a value - truth value is `False` Functions return `None` by default: ```python def my_func(a, b): print(a + b) # this function will return None # because there is no explicit return ``` `None` can be used to mark function parameter as optional: ```python def my_func(a, b, c=None): res = a + b if c is not None: res += c return res ``` --- # Booleans - represented by two constant objects `False` and `True` - built-in function `bool()` can be used to convert any value to boolean - following expressions are always evaluated to `False`: ```python None # None False # boolean 0 # integer number 0.0 # float number 0j # complex number "" # empty string () # empty tuple set() # empty set [] # empty list {} # empty dictionary ``` - users can control this using `__bool__()` or `__len__()` methods: ```python class Car(object): def __bool__(self): return False ``` - all other values evaluate to `True` --- # Numbers - integers `int()`, floating point numbers `float()` - complex numbers `complex()` - immutable data types - booleans are also sub-type of integers (`False` -> `0`, `True` -> `1`) ```python a = 2 # int b = 3.24 # float c = 2 + 3j # complex ``` There are many packages for working with numbers in standard library: - **math** provides more mathematical functions - **cmath** provides access to mathematical functions for complex numbers - **decimal** supports numbers with custom precision ```python a = decimal.Decimal('0.1') b = decimal.Decimal('0.2') c = a + b # >>> Decimal('0.3') ``` - **fractions** adds support for rational numbers --- # Strings - immutable sequence of characters (Unicode code points) ```python my_string = 'hello world!' my_second_string = "hey!" ``` - one can access individual characters using indexes `my_string[2]` - strings can also be sliced using the `my_string[x:y]` syntax - `len(my_string)` can tell us the length of the string - to join two strings one can use `+` (can be ineffective) ```python a = 'hello' b = 'world' c = a + b # >>> 'hello world' ``` - to loop over characters simply use `for` loop ```python for c in my_string: print(c) ``` - to work with binary data one can use `bytes` and `bytearray` types --- # Lists - ordered sequence of objects - dynamic size - homogeneous (items are usually of the same meaning) - implemented as array of pointers to objects - checking for membership of value is slow (depends on the size of list) ```python my_list = [1, 2, 3] 2 in my_list # >>> True ``` - one can also compute list using list comprehension syntax ```python my_list = [expression for variable in sequence if expression] ``` - to iterate over list values, we can use simple for loop ```python for x in my_list: print(x) ``` --- # Tuples - immutable sequence of objects - fixed size - heterogeneous (items are usually of different meaning) - can be used as a dictionary key - can be used inside sets (lists can not) ```python my_tuple = ('John', 'Doe', 35,) ``` - it is not possible to update tuple, we must create a new one ```python my_tuple = ('John', 'Doe', 35,) my_new_tuple = my_tuple + ('Bratislava',) ``` - checking for membership of value is the same as in lists ```python my_tuple = ('John', 'Doe', 35,) 'Doe' in my_tuple # >>> True ``` --- # Sets - unordered sequence of distinct objects - only hashable types can be stored inside set - checking for membership of value is fast ```python my_list = [1, 2, 2, 4, 4] my_set = set(my_list) # >>> set([1, 2, 4]) ``` - adding new existing items will do no operation ```python my_set.add(2) ``` - sets are implemented using dictionaries - one can use `frozenset` type to get the immutable behaviour - set provides many useful methods for comparing with other sets - `union()`, `intersection()`, `difference()`, `symmetric_difference()`, `issubset()`, `issuperset()` --- # Dictionaries - maps keys to values - keys are unique - unordered - only hashable types can be set as keys - values can be of any type - checking for membership of key is fast ```python my_dict = { 'key1': 123, 'key2': 456, } my_dict['key1'] # >>> 123 my_dict['key2'] # >>> 456 my_dict['key3'] # this will fail with KeyError: 'key3' ``` - we can update existing dictionary ```python my_dict['key3'] = 789 my_dict.update({ 'key4': 555, 'key5': 666 }) ``` --- # Ranges - immutable sequence of numbers - used for looping specific number of types - small memory footprint (only start, stop and step are stored) ```python for x in range(0, 4, 1): print(x) ``` - it returns `range` object, we can convert it to list or tuple ```python my_range = range(0, 4, 1) # >>> range(0, 4) my_list = list(my_range) my_tuple = tuple(my_range) ``` - checking for membership of value is also possible ```python my_range = range(0, 4, 1) 1 in my_range # >>> True ``` --- # Iterators and generators - iterator is an object which implements the iterator protocol - classes must implement `__iter__()` and `__next__()` to satisfy the protocol - built-in function `iter()` returns iterator for object ```python my_list = [1, 2] my_iterator = iter(my_list) next(my_iterator) # >>> 1 next(my_iterator) # this will fail with StopIteration ``` - generators generate sequences - generator yields one value at a time - ability to work with infinite sequences - saves memory ```python def generator(n): for i in range(n): yield i iterator = generator(3) next(iterator) ``` --- # Collections - namedtuple - deque - Counter - OrderedDict - defaultdict --- # The end Thank you.