Skip to content

Commit ff6b990

Browse files
author
abregman
committed
Add more exercises
1 parent 2673ad5 commit ff6b990

File tree

11 files changed

+110
-0
lines changed

11 files changed

+110
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<p align="center"><img src="images/python_exercises.png"/></p>
2+
13
# Python Exercises
24

35
## Hello World
@@ -6,6 +8,7 @@
68
|--------|--------|------|----|
79
| Hello World! | [Exercise](exercises/hello_world/hello_world.md) | | |
810
| 22 times | [Exercise](exercises/hello_world/22_times.md) | | |
11+
| Access List Items | [Exercise](exercises/hello_world/access_list_items.md) | [Solution](solutions/hello_world/access_list_items.md) | |
912
| Length | [Exercise](exercises/hello_world/length.md) | | |
1013

1114
## Data Types
@@ -15,6 +18,7 @@
1518
| Data Types? I know a few | [Exercise](exercises/data_types/I_know_a_few.md) | [Solution](solutions/exceptions/I_know_a_few.md) | |
1619
| Choose a Data Type - level 1 | [Exercise](exercises/data_types/choose_datatype_level_1.md) | [Solution](solutions/data_types/choose_datatype_level_1.md) | |
1720
| Choose a Data Type - level 2 | [Exercise](exercises/data_types/choose_datatype_level_2.md) | [Solution](solutions/data_types/choose_datatype_level_2.md) | |
21+
| Objects 101 | [Exercise](exercises/data_types/objects_101.md) | [Solution](solutions/data_types/objects_101.md) | |
1822

1923
## Loops
2024

@@ -27,6 +31,12 @@
2731
|Name|Objective & Instructions|Solution|Comments|
2832
|--------|--------|------|----|
2933
| Calculator | [Exercise](exercises/functions/calculator.md) | [Solution](solutions/functions/calculator.md) | |
34+
35+
## Magic Methods
36+
37+
|Name|Objective & Instructions|Solution|Comments|
38+
|--------|--------|------|----|
39+
| Length Magic Method | [Exercise](exercises/magic_methods/length.md) | | |
3040

3141
## Unit Testing
3242

@@ -54,3 +64,9 @@
5464
| File exists | [Exercise](exercises/os/file_exists.md) | | |
5565
| Print all the files in a directory | [Exercise](exercises/os/print_all_files_in_dir.md) | | |
5666

67+
## Misc
68+
69+
|Name|Objective & Instructions|Solution|Comments|
70+
|--------|--------|------|----|
71+
| Random Number | [Exercise](exercises/misc/random_number.md) | | |
72+
| Random Item | [Exercise](exercises/misc/random_item.md) | | |

exercises/data_types/objects_101.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Objects 101
2+
3+
1. What information an object in Python holds? or what attributes an object has?
4+
a. Explain each one of them
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Access List Items
2+
3+
Given the following list `li = [2, 0 ,1, 7, 2, 0, 2, 2]` perform the following:
4+
5+
1. Print the first item of the list
6+
2. Print the last item of the list
7+
8+
Bonus:
9+
10+
1. Print random item of the list

exercises/magic_methods/length.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Length Magic Method
2+
3+
1. Why the code below fails when executed?
4+
2. Fix it so it works
5+
6+
```
7+
import collections
8+
9+
Mushroom = collections.namedtuple('Mushroom', ['name', 'poisonous'])
10+
11+
class MushroomsBusket:
12+
13+
def __init__(self):
14+
15+
self._mushrooms = [Mushroom('Oyster', False), Mushroom('Portobello', False)]
16+
17+
mushroom_basket = MushroomsBusket()
18+
print(len(mushroom_basket))
19+
```

exercises/misc/random_item.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Random Item
2+
3+
Write code to pick up a random item from a given list

exercises/misc/random_number.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Random Number
2+
3+
Generate random number between 1 and 10

images/python_exercises.png

52.6 KB
Loading

solutions/data_types/objects_101.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Objects 101
2+
3+
1. What information an object in Python holds? or what attributes an object has?
4+
a. Explain each one of them
5+
6+
* ID: unique identifier of the object
7+
* value: what data the object actually stores (e.g. an integer, string, ...)
8+
* type: the data type of the value (int, boolean, string, ...)
9+
* reference count: how many times the object is references/used
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Access List Items
2+
3+
Given the following list `li = [2, 0 ,1, 7, 2, 0, 2, 2]` perform the following:
4+
5+
1. Print the first item of the list - `print(li[0])`
6+
2. Print the last item of the list - `print(li[-1])`
7+
8+
Bonus:
9+
10+
1. Print random item of the list
11+
12+
```python
13+
from random import choice
14+
print(choice(li))
15+
```

solutions/magic_methods/length.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Length Magic Method
2+
3+
1. It will fail because the class MushroomsBasked doesn't implements __len__ magic method
4+
5+
2.
6+
7+
```python
8+
import collections
9+
10+
Mushroom = collections.namedtuple('Mushroom', ['name', 'poisonous'])
11+
12+
class MushroomsBusket:
13+
14+
def __init__(self):
15+
16+
self._mushrooms = [Mushroom('Oyster', False), Mushroom('Portobello', False)]
17+
18+
def __len__(self):
19+
return len(self._mushrooms)
20+
21+
mushroom_basket = MushroomsBusket()
22+
print(len(mushroom_basket))
23+
```

solutions/misc/random_item.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
from random import choice
5+
6+
li = [2, 0, 1, 7]
7+
8+
print(choice(li))

0 commit comments

Comments
 (0)