Skip to content

Commit 0255339

Browse files
author
abregman
committed
Add a couple of exercises
Also TOC for easier navigation.
1 parent 837eee3 commit 0255339

File tree

13 files changed

+191
-47
lines changed

13 files changed

+191
-47
lines changed

README.md

+84-9
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,48 @@
22

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

5-
:bar_chart:  There are currently **80** exercises and questions
6-
7-
# Python Exercises
8-
5+
:bar_chart:  There are currently **77** exercises and questions
6+
7+
* Exercises
8+
* [Hello World](#exercises-hello-world)
9+
* [Objects & Data Types](#exercises-objects)
10+
* [Variables](#exercises-variables)
11+
* [Booleans](#exercisese-booleans)
12+
* [Strings](#exercisese-strings)
13+
* [Numbers](#exercisese-numbers)
14+
* [Lists and Tuples](#exercisese-lists)
15+
* [Dictionaries](#exercises-dictionaries)
16+
* [Loops](#exercises-loops)
17+
* [Functions](#exercises-loops)
18+
* [Classes](#exercises-classes)
19+
* [OOP](#exercises-oop)
20+
* Questions
21+
* [Hello World](#questions-hello-world)
22+
* [Objects & Data Types](#questions-objects)
23+
* [Variables](#questions-variables)
24+
* [Booleans](#questions-booleans)
25+
* [Strings](#questions-strings)
26+
* [Numbers](#questions-numbers)
27+
* [Lists and Tuples](#questions-lists)
28+
* [Dictionaries](#questions-dictionaries)
29+
* [Loops](#questions-loops)
30+
* [Functions](#questions-loops)
31+
* [Classes](#questions-classes)
32+
* [OOP](#questions-oop)
33+
34+
# Exercises
35+
36+
<a name="exercises-hello-world"></a>
937
## Hello World
1038

1139
|Name|Objective & Instructions|Solution|Comments|
1240
|--------|--------|------|----|
13-
| Hello World! | [Exercise](exercises/hello_world/hello_world.md) | | |
41+
| Hello World! | [Exercise](exercises/hello_world/hello_world.md) | Solution](solutions/hello_world/hello_world.md | |
1442
| Python Characteristics | [Exercise](exercises/hello_world/python_characteristics.md) | [Solution](solutions/hello_world/python_characteristics.md) | |
1543
| What is the result? - Level 1 | [Exercise](exercises/hello_world/what_is_the_result_lvl_1.md) | [Solution](solutions/hello_world/what_is_the_result_lvl_1.md) | |
1644
| What is the result? - Level 2 | [Exercise](exercises/hello_world/what_is_the_result_lvl_2.md) | | |
1745

46+
<a name="exercises-objects"></a>
1847
## Objects & Data Types
1948

2049
|Name|Objective & Instructions|Solution|Comments|
@@ -27,6 +56,7 @@
2756
| Strongly Typed | [Exercise](exercises/data_types/strongly_typed.md) | [Solution](solutions/data_types/strongly_typed.md) | |
2857
| Object Creation | [Exercise](exercises/data_types/object_creation.md) | [Solution](solutions/data_types/object_creation.md) | |
2958

59+
<a name="exercises-variables"></a>
3060
## Variables
3161

3262
|Name|Objective & Instructions|Solution|Comments|
@@ -38,13 +68,15 @@
3868
| Copying Variables | [Exercise](exercises/variables/copying_variables.md) | [Solution](solutions/variables/copying_variables.md) | |
3969
| Mutable Objects | [Exercise](exercises/variables/mutable_objects.md) | [Solution](solutions/variables/mutable_objects.md) | |
4070

71+
<a name="exercises-booleans"></a>
4172
## Booleans
4273

4374
|Name|Objective & Instructions|Solution|Comments|
4475
|--------|--------|------|----|
4576
| True or False? | [Exercise](exercises/booleans/true_or_false.md) | [Solution](solutions/booleans/true_or_false.md) | |
4677
| Conversion | [Exercise](exercises/booleans/conversion.md) | [Solution](solutions/booleans/conversion.md) | |
4778

79+
<a name="exercises-strings"></a>
4880
## Strings
4981

5082
|Name|Objective & Instructions|Solution|Comments|
@@ -62,6 +94,7 @@
6294
| Slicing - Level 1 | [Exercise](exercises/strings/slicing_lvl_1.md) | [Solution](solutions/strings/slicing_lvl_1.md) | |
6395
| Slicing - Level 2 | [Exercise](exercises/strings/slicing_lvl_2.md) | [Solution](solutions/strings/slicing_lvl_2.md) | |
6496

97+
<a name="exercises-numbers"></a>
6598
## Numbers
6699

67100
|Name|Objective & Instructions|Solution|Comments|
@@ -72,6 +105,7 @@
72105
| Bases | [Exercise](exercises/numbers/bases.md) | [Solution](solutions/numbers/bases.md) | |
73106
| Palindrome | [Exercise](exercises/numbers/palindrome.md) | [Solution](solutions/numbers/palindrome.md) | |
74107

108+
<a name="exercises-lists"></a>
75109
## Lists & Tuples
76110

77111
|Name|Objective & Instructions|Solution|Comments|
@@ -85,12 +119,14 @@
85119
| 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) | |
86120
| 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) | |
87121

122+
<a name="exercises-dictionaries"></a>
88123
## Dictionaries
89124

90125
|Name|Objective & Instructions|Solution|Comments|
91126
|--------|--------|------|----|
92127
| Facts Only | [Exercise](exercises/dicts/facts_only.md) | [Solution](solutions/dict/facts_only.md) | |
93128

129+
<a name="exercises-loops"></a>
94130
## Loops
95131

96132
|Name|Objective & Instructions|Solution|Comments|
@@ -101,6 +137,7 @@
101137
| Stream of Numbers | [Exercise](exercises/loops/numbers_stream.md) | [Solution](solutions/loops/numbers_stream.md) | |
102138
| Refactor-1 | [Exercise](exercises/loops/refactor_1.md) | [Solution](solutions/loops/refactor_1.py) | |
103139

140+
<a name="exercises-functions"></a>
104141
## Functions
105142

106143
|Name|Objective & Instructions|Solution|Comments|
@@ -109,17 +146,20 @@
109146
| Calculator | [Exercise](exercises/functions/calculator.md) | [Solution](solutions/functions/calculator.md) | |
110147
| First Class Objects | [Exercise](exercises/functions/first_class_objects.md) | [Solution](solutions/functions/first_class_objects.md) | |
111148

112-
## OOP
149+
<a name="exercises-classes"></a>
150+
## Classes
113151

114152
|Name|Objective & Instructions|Solution|Comments|
115153
|--------|--------|------|----|
116-
| Inheritance | [Exercise](exercises/oop/inheritance.md) | [Solution](solutions/oop/inheritance.md) | |
154+
| Classes 101 | [Exercise](exercises/classes/101.md) | [Solution](solutions/classes/101.md) | |
155+
| Attributes | [Exercise](exercises/classes/attributes.md) | [Solution](solutions/classes/attributes.md) | |
117156

118-
## Classes
157+
<a name="exercises-oop"></a>
158+
## OOP
119159

120160
|Name|Objective & Instructions|Solution|Comments|
121161
|--------|--------|------|----|
122-
| Attributes | [Exercise](exercises/classes/attributes.md) | [Solution](solutions/classes/attributes.md) | |
162+
| Inheritance | [Exercise](exercises/oop/inheritance.md) | [Solution](solutions/oop/inheritance.md) | |
123163

124164
## Magic Methods
125165

@@ -186,3 +226,38 @@
186226
|--------|--------|------|----|
187227
| Random Number | [Exercise](exercises/misc/random_number.md) | | |
188228
| Random Item | [Exercise](exercises/misc/random_item.md) | | |
229+
230+
# Questions
231+
232+
<a name="questions-hello-world"></a>
233+
## Hello World
234+
235+
<details>
236+
<summary>How to print "Hello World"?</summary><br><b>
237+
238+
`print("Hello World")`
239+
</b></details>
240+
241+
<a name="questions-classes"></a>
242+
## Classes
243+
244+
<details>
245+
<summary>Define a class that does nothing</summary><br><b>
246+
247+
```python
248+
class SomeClass:
249+
pass
250+
```
251+
</b></details>
252+
253+
<details>
254+
<summary>True or False? If <code>c</code> is an instance of a class, then in <code>c.x = 1</code>, <code>x</code> is a variable of the value 1</summary><br><b>
255+
256+
False. `x` is an attribute in the case `c.x = 1`
257+
</b></details>
258+
259+
<details>
260+
<summary>True or False? Every object in Python has attributes</summary><br><b>
261+
262+
True. You can think on attributes as private dictionaries but instead of accessing them with `[]` or `.get`, they are accessed by using a dot.
263+
</b></details>

exercises/classes/101.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Classes 101 - Solution
2+
3+
## Objectives
4+
5+
1. Define a class that does nothing and name it `SomeClass`
6+
2. Create an instance of the class you defined and assign it to the variable `c`
7+
3. True or False? In case of `c.x = 1`, `x` is a variable of value 1
8+
4. How to retrieve the value of `x` attribute from the previous question?
9+
10+
Click [here to view the solution](../../solutions/classes/101.md)

exercises/classes/attributes.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
## Attributes
1+
# Attributes
22

3-
Explain class attributes vs. instance attributes
3+
## Objectives
4+
5+
1. Explain class attributes vs. instance attributes
6+
2. True or False? not every object in Python has attributes
7+
3. True or False? An attribute doesn't exist on the variable, but rather on the object that the variable refers to

exercises/hello_world/hello_world.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22

33
## Objectives
44

5-
Write one line of code that will print "Hello World!"
5+
1. Write one line of code that will print "Hello World!"
6+
2. Now modify the code so the output looks like this:
7+
8+
```
9+
Hello
10+
World
11+
```
12+
13+
Click [here to view the solution](../../solutions/hello_world.md)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
## Python Characteristics
1+
## Python Characteristics - Solution
2+
3+
## Objectives
24

35
1. What are some characteristics of the Python programming language?
6+
2. What type of applications use Python?
47

5-
Click here for the [solution](solutions/hello_world/python_characteristics.md)
8+
Click [here to view the solution](../../solutions/hello_world/python_characteristics.md)

exercises/variables/locations_or_names.md

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

33
What do you think about the following statement:
44

5-
In Python, variables are locations, not just names. Each
5+
In Python, variables are locations, not just names. So `x = 1` is really the value of 1 in the memory location called x.

solutions/classes/101.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Classes 101 - Solution
2+
3+
## Objectives
4+
5+
1. Define a class that does nothing and name it `SomeClass`
6+
2. Create an instance of the class you defined and assign it to the variable `c`
7+
3. True or False? In case of `c.x = 1`, `x` is a variable of value 1
8+
4. How to retrieve the value of `x` attribute from the previous question?
9+
10+
## Solution
11+
12+
1.
13+
14+
```python
15+
class SomeClass:
16+
pass
17+
```
18+
19+
2.
20+
21+
```python
22+
c = SomeClass()
23+
```
24+
25+
3. False. in `c.x`, `x` is an attribute of the instance `c` and not a variable
26+
4. `c.x`

solutions/classes/attributes.md

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

3-
Explain class attributes vs. instance attributes
3+
## Objectives
44

5-
### Solution
5+
1. Explain class attributes vs. instance attributes
6+
2. True or False? not every object in Python has attributes
7+
3. True or False? An attribute doesn't exist on the variable, but rather on the object that the variable refers to
68

7-
In the following block of code `x` is a class attribute while `self.y` is a instance attribute
9+
## Solution
10+
11+
1. In the following block of code `x` is a class attribute while `self.y` is a instance attribute
812

913
```
1014
class MyClass(object):
@@ -13,3 +17,6 @@ class MyClass(object):
1317
def __init__(self, y):
1418
self.y = y
1519
```
20+
21+
2. False. Every object in Python has attributes
22+
3. True

solutions/hello_world/hello_world.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Hello World
2+
3+
## Objectives
4+
5+
1. Write one line of code that will print "Hello World!"
6+
2. Now modify the code so the output looks like this:
7+
8+
```
9+
Hello
10+
World
11+
```
12+
13+
## Solution
14+
15+
1. print("Hello World!")
16+
2. print("Hello\n World!")

solutions/hello_world/python_characteristics.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
## Python Characteristics
1+
## Python Characteristics - Solution
2+
3+
## Objectives
24

35
1. What are some characteristics of the Python programming language?
6+
2. What type of applications use Python?
7+
8+
## Solution
49

510
1.
611

@@ -11,3 +16,5 @@
1116
* The python package manager is called PIP "pip installs packages", having more than 200.000 available packages.
1217
* Python comes with pip installed and a big standard library that offers the programmer many precooked solutions.
1318
* In python **Everything** is an object.
19+
20+
2. Web, Gaming, Machine Learning, Web Scrapers, Data (visualization, analytics) and more

solutions/loops/refactor_1.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections import namedtuple
44
import timeit
55

6+
67
def before_refactor():
78

89
Mushroom = namedtuple('Mushroom', ['name', 'poisonous'])
@@ -14,7 +15,8 @@ def before_refactor():
1415
for mushroom in mushrooms:
1516
i += 1
1617
name = mushroom.name
17-
print('%d:"%s"'%(i, name))
18+
print('%d:"%s"' % (i, name))
19+
1820

1921
def after_refactor(): # <- Solution is here! :)
2022

@@ -26,7 +28,12 @@ def after_refactor(): # <- Solution is here! :)
2628
for i, mushroom in enumerate(mushrooms):
2729
i += 1
2830
name = mushroom.name
29-
print('%d:"%s"'%(i, name))
31+
print('%d:"%s"' % (i, name))
32+
3033

31-
print(timeit.timeit("before_refactor()", setup="from __main__ import before_refactor", number=100))
32-
print(timeit.timeit("after_refactor()", setup="from __main__ import after_refactor", number=100))
34+
print(timeit.timeit(
35+
"before_refactor()",
36+
setup="from __main__ import before_refactor", number=100))
37+
print(timeit.timeit(
38+
"after_refactor()",
39+
setup="from __main__ import after_refactor", number=100))

solutions/variables/locations_or_names.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
What do you think about the following statement:
66

7-
In Python, variables are locations, not just names. Each
7+
In Python, variables are locations, not just names. So `x = 1` is really the value of 1 in the memory location called x.
88

99
### Solution
1010

1111
The statement above is wrong. In Python a variable is merely a name, a reference to the object, not the actual object.
1212
So when for example you perform an assignment, you don't copy the value into the variable, but you assign the object with a name.
13+
14+
So in case of `x = 1` what it means, is x refers to the integer 1.

0 commit comments

Comments
 (0)