This project contains tasks for learning to use variable/type annotations in Python 3.
-
0. Basic annotations - add
0-add.py contains a type-annotated functionaddthat takes a floataand a floatbas arguments and returns their sum as a float.. -
1. Basic annotations - concat
1-concat.py contains a type-annotated functionconcatthat takes a stringstr1and a stringstr2as arguments and returns a concatenated string. -
2. Basic annotations - floor
2-floor.py contains a type-annotated functionfloorwhich takes a floatnas argument and returns the floor of the float. -
3. Basic annotations - to string
3-to_str.py contains a type-annotated functionto_strthat takes a floatnas argument and returns the string representation of the float. -
4. Define variables
4-define_variables.py contains a script that define and annotate the following variables with the specified values:a, an integer with a value of 1.pi, a float with a value of 3.14.i_understand_annotations, a boolean with a value of True.school, a string with a value of “Holberton”.
-
5. Complex types - list of floats
5-sum_list.py contains a type-annotated functionsum_listwhich takes a listinput_listof floats as argument and returns their sum as a float. -
6. Complex types - mixed list
6-sum_mixed_list.py contains a type-annotated functionsum_mixed_listwhich takes a listmxd_lstof integers and floats and returns their sum as a float. -
7. Complex types - string and int/float to tuple
7-to_kv.py contains a type-annotated functionto_kvthat takes a stringkand an int OR floatvas arguments and returns a tuple. The first element of the tuple is the stringk. The second element is the square of the int/floatvand should be annotated as a float. -
8. Complex types - functions
8-make_multiplier.py contains a type-annotated functionmake_multiplierthat takes a floatmultiplieras argument and returns a function that multiplies a float bymultiplier. -
9. Let's duck type an iterable object
9-element_length.py contains an annotation of the function's (shown below) parameters and return values with the appropriate types.def element_length(lst): return [(i, len(i)) for i in lst]
-
10. Duck typing - first element of a sequence
100-safe_first_element.py contains an augmentation of the following code with the correct duck-typed annotations:def safe_first_element(lst): if lst: return lst[0] else: return None
-
11. More involved type annotations
101-safely_get_value.py contains a script that includes the code below with type annotations added to it.def safely_get_value(dct, key, default = None): if key in dct: return dct[key] else: return default
-
12. Type Checking
102-type_checking.py contains the code below and usesmypyto validate it and apply any necessary changes.def zoom_array(lst: Tuple, factor: int = 2) -> Tuple: zoomed_in: Tuple = [ item for item in lst for i in range(factor) ] return zoomed_in array = [12, 72, 91] zoom_2x = zoom_array(array) zoom_3x = zoom_array(array, 3.0)