Skip to content

Commit 837eee3

Browse files
author
abregman
committed
Add new exercises on type hinting
Also fixed issue #2 regarding missing class exercise.
1 parent d4b77c1 commit 837eee3

17 files changed

+180
-11
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
:information_source:  This repo contains questions and exercises to learn and practice Python
44

5-
:bar_chart:  There are currently **70** exercises and questions
5+
:bar_chart:  There are currently **80** exercises and questions
66

77
# Python Exercises
88

@@ -79,11 +79,18 @@
7979
| Access List Items | [Exercise](exercises/hello_world/access_list_items.md) | [Solution](solutions/hello_world/access_list_items.md) | |
8080
| Equal Lists | [Exercise](exercises/lists/equal_lists.md) | [Solution](solutions/lists/equal_lists.md) | |
8181
| Length | [Exercise](exercises/hello_world/length.md) | | |
82+
| Append & Insert | [Exercise](exercises/lists/append_and_insert.md) | [Solution](solutions/lists/append_and_insert.md) | |
8283
| Min Max | [Exercise](exercises/lists/min_max.md) | [Solution](solutions/lists/min_max.md) | |
8384
| Three Biggest Items | [Exercise](exercises/lists/three_biggest_items.md) | [Solution](solutions/lists/three_biggest_items.md) | |
8485
| What is the result? - Level 1 | [Exercise](exercises/lists/what_is_the_result_lvl_1.md) | [Solution](solutions/lists/what_is_the_result_lvl_1.md) | |
8586
| What is the result? - Level 2 | [Exercise](exercises/lists/what_is_the_result_lvl_2.md) | [Solution](solutions/lists/what_is_the_result_lvl_2.md) | |
8687

88+
## Dictionaries
89+
90+
|Name|Objective & Instructions|Solution|Comments|
91+
|--------|--------|------|----|
92+
| Facts Only | [Exercise](exercises/dicts/facts_only.md) | [Solution](solutions/dict/facts_only.md) | |
93+
8794
## Loops
8895

8996
|Name|Objective & Instructions|Solution|Comments|
@@ -164,6 +171,15 @@
164171
| Walrus | [Exercise](exercises/improve_the_code/walrus.md) | [Solution](solutions/improve_the_code/walrus.md) | |
165172
| Vowel Letters | [Exercise](exercises/improve_the_code/vowel_letters.md) | [Solution](solutions/improve_the_code/vowel_letters.md) | |
166173

174+
## Type Hinting
175+
176+
|Name|Objective & Instructions|Solution|Comments|
177+
|--------|--------|------|----|
178+
| Type Hinting Facts Only | [Exercise](exercises/type_hinting/facts_only.md) | [Solution](solutions/type_hinting/facts_only.md) | |
179+
| Pros & Cons | [Exercise](exercises/type_hinting/pros_and_cons.md) | [Solution](solutions/type_hinting/pros_and_cons.md) | |
180+
| Type Hinting 101 | [Exercise](exercises/type_hinting/type_hinting_101.md) | [Solution](solutions/type_hinting/type_hinting_101.md) | |
181+
| Type Hinting 102 | [Exercise](exercises/type_hinting/type_hinting_102.md) | [Solution](solutions/type_hinting/type_hinting_102.md) | |
182+
167183
## Misc
168184

169185
|Name|Objective & Instructions|Solution|Comments|

exercises/classes/attributes.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Attributes
2+
3+
Explain class attributes vs. instance attributes

exercises/lists/append_and_insert.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Append & Insert
2+
3+
1. Given the list `li = [2, 0, 2, 2]`
4+
5+
1. Append to the end of the list the number 7
6+
2. Insert to the beginning of the list the number 1
7+
3. Insert in the second place the number 20
8+
9+
2. What will be the result of running the following line? `li.insert(20, 0)`
10+
11+
### Solution
12+
13+
Click [here](solutions/lists/append_and_insert.md) to view the solution.

exercises/lists/what_is_the_result_lvl_1.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
## What is the result?
22

3-
1. What is the result of the following block of code?
4-
5-
```python
6-
x = ['a', 'b', 'c']
7-
for i in x:
8-
if i == 'b':
9-
x = ['z', 'y']
10-
print(i)
11-
```
3+
1. `'x = 'pizza,';print(x)`
4+
2. `x = ('pizza',);print(x)`
5+
3. `x = ('pizza');print(x)`
6+
4. `x = 'pizza', 'pasta';print(x)`
7+
5. `print(list('hello')`
8+
6. `print('/n\n/n/n'.split())`
9+
7. `(1, 2) < (1, 2, 3)`
1210

1311
### Solution
1412

exercises/type_hinting/facts_only.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Facts Only
2+
3+
Which of the following statements regarding type hinting is true?
4+
5+
1. Type hinting is a special syntax used in functions and variables declarations to provide the developer with the information on the type of the variable
6+
2. Type Hinting is a fairly new feature of Python, introduced in Python 3
7+
3. Python enforces type hints meaning you can set a variable with the type hint of string with an integer value
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Pros and Cons
2+
3+
Specify some of the pros and cons of Type Hinting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Type Hinting 101
2+
3+
Apply type hinting for each one of the following variables:
4+
5+
1. `x` is a string
6+
2. `y` is an integer
7+
3. `z` is a list of strings
8+
4. `omela` is a function that takes two arguments, both strings and it also returns a string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Type Hinting 102
2+
3+
Apply type hinting for each one of the following variables:
4+
5+
1. `abc` is a list of of two integers
6+
2. `qwery` is dictionary where the keys are string and values are integers
7+
3. `elmer` is None but at some point may set as a string
8+
4. `norie` is a variable containing a function that takes two integer arguments and returns a string

exercises/variables/types.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
1. How to check the type of a variable in Python?
44
2. How to check if a variable points to an object of an integer type?
55
3. How to check type of a literal?
6+
4. What `is` operator is used for?
7+
5. if `x = "str"`, what would be the result of `x is str`?

solutions/classes/attributes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Attributes
1+
## Attributes - Solution
22

33
Explain class attributes vs. instance attributes
44

solutions/lists/append_and_insert.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Append & Insert
2+
3+
1. Given the list `li = [2, 0, 2, 2]`
4+
5+
1. Append to the end of the list the number 7
6+
2. Insert to the beginning of the list the number 1
7+
3. Insert in the second place the number 20
8+
9+
2. What will be the result of running the following line? `li.insert(20, 0)`
10+
11+
### Solution
12+
13+
1.
14+
1. `li.append(7)`
15+
2. `li.insert(0, 1)`
16+
3. `li.insert(1, 20)`
17+
18+
2. It will append 0 to the end of the list

solutions/lists/what_is_the_result_lvl_1.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
2. `x = ('pizza',);print(x)`
55
3. `x = ('pizza');print(x)`
66
4. `x = 'pizza', 'pasta';print(x)`
7+
5. `print(list('hello')`
8+
6. `print('/n\n/n/n'.split())`
9+
7. `(1, 2) < (1, 2, 3)`
710

811
### Solution
912

1013
1. `pizza,`
1114
2. `('pizza')`
1215
3. `pizza`
1316
4. `('pizza', 'pasta')`
17+
5. `['h', 'e', 'l', 'l', 'o']`
18+
6. `['/n', '/n/n']`
19+
7. `True`

solutions/type_hinting/facts_only.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Facts Only - Solution
2+
3+
Which of the following statements regarding type hinting is true?
4+
5+
1. Type hinting is a special syntax used in functions and variables declarations to provide the developer with the information on the type of the variable - True
6+
2. Type Hinting is a fairly new feature of Python, introduced in Python 3 - True. It was introduced with Python 3.5 to be accurate
7+
3. Python enforces type hints meaning you can set a variable with the type hint of string with an integer value - False. Python doesn't enforce type hints, meaning you can still change the variable type even with specific type hint
8+
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Pros and Cons - Solution
2+
3+
Specify some of the pros and cons of Type Hinting
4+
5+
### Solution
6+
7+
Pros:
8+
* Enhance IDE experience by receiving feedback (warnings, notifications, ...) on how the code is written
9+
* Improves code documentation
10+
Cons:
11+
* Requires investment (like time) which could perhaps be used for other aspects of the project
12+
* While minor in most cases, might have effect on the time it takes your code to run
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Type Hinting 101 - Solution
2+
3+
Apply type hinting for each one of the following variables:
4+
5+
1. `x` is a string
6+
2. `y` is an integer
7+
3. `z` is a list of strings
8+
4. `omela` is a function that takes two arguments, both strings and it also returns a string
9+
10+
### Solution
11+
12+
1. `x: str`
13+
2. `y: int`
14+
3.
15+
16+
```python
17+
from typing import List
18+
z: List[str]
19+
```
20+
21+
4.
22+
```python
23+
def omela(x: str, y: str) -> str:
24+
...
25+
```
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Type Hinting 102 - Solution
2+
3+
Apply type hinting for each one of the following variables:
4+
5+
1. `abc` is a list of of two integers
6+
2. `qwery` is dictionary where the keys are string and values are integers
7+
3. `elmer` is None but at some point may set as a string
8+
4. `norie` is a variable containing a function that takes two integer arguments and returns a string
9+
10+
### Solution
11+
12+
1.
13+
```
14+
from typing import List
15+
16+
abc: List[int, int]
17+
```
18+
19+
2.
20+
```
21+
from typing import Dict
22+
23+
abc: Dict[str, int]
24+
```
25+
26+
3.
27+
```
28+
from typing import Optional
29+
30+
elmer: Optional[str]
31+
```
32+
33+
4.
34+
```
35+
from typing import Callable
36+
37+
norie: Callable[[int, int], str]
38+
```

solutions/variables/types.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
1. How to check the type of a variable in Python?
44
2. How to check if a variable points to an object of an integer type?
55
3. How to check type of a literal?
6+
4. What `is` operator is used for?
7+
5. if `x = "str"`, what would be the result of `x is str`?
68

79
### Solution
810

911
1. `type(some_var)`
1012
2. `isinstance(some_var, int)`
1113
3. Same way: `type(2017)` and `isinstance(2022, int)`
14+
4. `is` operator is used to check if two variables/values are located on the same memory area
15+
5. `False` since it checks if x and str type are on the same memory area and it's not)

0 commit comments

Comments
 (0)