Skip to content

Commit

Permalink
Add a couple of exercises
Browse files Browse the repository at this point in the history
Also TOC for easier navigation.
  • Loading branch information
abregman committed Jun 25, 2022
1 parent 837eee3 commit 0255339
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 47 deletions.
93 changes: 84 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,48 @@

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

:bar_chart:  There are currently **80** exercises and questions

# Python Exercises

:bar_chart:  There are currently **77** exercises and questions

* Exercises
* [Hello World](#exercises-hello-world)
* [Objects & Data Types](#exercises-objects)
* [Variables](#exercises-variables)
* [Booleans](#exercisese-booleans)
* [Strings](#exercisese-strings)
* [Numbers](#exercisese-numbers)
* [Lists and Tuples](#exercisese-lists)
* [Dictionaries](#exercises-dictionaries)
* [Loops](#exercises-loops)
* [Functions](#exercises-loops)
* [Classes](#exercises-classes)
* [OOP](#exercises-oop)
* Questions
* [Hello World](#questions-hello-world)
* [Objects & Data Types](#questions-objects)
* [Variables](#questions-variables)
* [Booleans](#questions-booleans)
* [Strings](#questions-strings)
* [Numbers](#questions-numbers)
* [Lists and Tuples](#questions-lists)
* [Dictionaries](#questions-dictionaries)
* [Loops](#questions-loops)
* [Functions](#questions-loops)
* [Classes](#questions-classes)
* [OOP](#questions-oop)

# Exercises

<a name="exercises-hello-world"></a>
## Hello World

|Name|Objective & Instructions|Solution|Comments|
|--------|--------|------|----|
| Hello World! | [Exercise](exercises/hello_world/hello_world.md) | | |
| Hello World! | [Exercise](exercises/hello_world/hello_world.md) | Solution](solutions/hello_world/hello_world.md | |
| Python Characteristics | [Exercise](exercises/hello_world/python_characteristics.md) | [Solution](solutions/hello_world/python_characteristics.md) | |
| 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) | |
| What is the result? - Level 2 | [Exercise](exercises/hello_world/what_is_the_result_lvl_2.md) | | |

<a name="exercises-objects"></a>
## Objects & Data Types

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

<a name="exercises-variables"></a>
## Variables

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

<a name="exercises-booleans"></a>
## Booleans

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

<a name="exercises-strings"></a>
## Strings

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

<a name="exercises-numbers"></a>
## Numbers

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

<a name="exercises-lists"></a>
## Lists & Tuples

|Name|Objective & Instructions|Solution|Comments|
Expand All @@ -85,12 +119,14 @@
| 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) | |
| 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) | |

<a name="exercises-dictionaries"></a>
## Dictionaries

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

<a name="exercises-loops"></a>
## Loops

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

<a name="exercises-functions"></a>
## Functions

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

## OOP
<a name="exercises-classes"></a>
## Classes

|Name|Objective & Instructions|Solution|Comments|
|--------|--------|------|----|
| Inheritance | [Exercise](exercises/oop/inheritance.md) | [Solution](solutions/oop/inheritance.md) | |
| Classes 101 | [Exercise](exercises/classes/101.md) | [Solution](solutions/classes/101.md) | |
| Attributes | [Exercise](exercises/classes/attributes.md) | [Solution](solutions/classes/attributes.md) | |

## Classes
<a name="exercises-oop"></a>
## OOP

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

## Magic Methods

Expand Down Expand Up @@ -186,3 +226,38 @@
|--------|--------|------|----|
| Random Number | [Exercise](exercises/misc/random_number.md) | | |
| Random Item | [Exercise](exercises/misc/random_item.md) | | |

# Questions

<a name="questions-hello-world"></a>
## Hello World

<details>
<summary>How to print "Hello World"?</summary><br><b>

`print("Hello World")`
</b></details>

<a name="questions-classes"></a>
## Classes

<details>
<summary>Define a class that does nothing</summary><br><b>

```python
class SomeClass:
pass
```
</b></details>

<details>
<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>

False. `x` is an attribute in the case `c.x = 1`
</b></details>

<details>
<summary>True or False? Every object in Python has attributes</summary><br><b>

True. You can think on attributes as private dictionaries but instead of accessing them with `[]` or `.get`, they are accessed by using a dot.
</b></details>
10 changes: 10 additions & 0 deletions exercises/classes/101.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Classes 101 - Solution

## Objectives

1. Define a class that does nothing and name it `SomeClass`
2. Create an instance of the class you defined and assign it to the variable `c`
3. True or False? In case of `c.x = 1`, `x` is a variable of value 1
4. How to retrieve the value of `x` attribute from the previous question?

Click [here to view the solution](../../solutions/classes/101.md)
8 changes: 6 additions & 2 deletions exercises/classes/attributes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Attributes
# Attributes

Explain class attributes vs. instance attributes
## Objectives

1. Explain class attributes vs. instance attributes
2. True or False? not every object in Python has attributes
3. True or False? An attribute doesn't exist on the variable, but rather on the object that the variable refers to
10 changes: 9 additions & 1 deletion exercises/hello_world/hello_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

## Objectives

Write one line of code that will print "Hello World!"
1. Write one line of code that will print "Hello World!"
2. Now modify the code so the output looks like this:

```
Hello
World
```

Click [here to view the solution](../../solutions/hello_world.md)
7 changes: 5 additions & 2 deletions exercises/hello_world/python_characteristics.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Python Characteristics
## Python Characteristics - Solution

## Objectives

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

Click here for the [solution](solutions/hello_world/python_characteristics.md)
Click [here to view the solution](../../solutions/hello_world/python_characteristics.md)
2 changes: 1 addition & 1 deletion exercises/variables/locations_or_names.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

What do you think about the following statement:

In Python, variables are locations, not just names. Each
In Python, variables are locations, not just names. So `x = 1` is really the value of 1 in the memory location called x.
26 changes: 26 additions & 0 deletions solutions/classes/101.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Classes 101 - Solution

## Objectives

1. Define a class that does nothing and name it `SomeClass`
2. Create an instance of the class you defined and assign it to the variable `c`
3. True or False? In case of `c.x = 1`, `x` is a variable of value 1
4. How to retrieve the value of `x` attribute from the previous question?

## Solution

1.

```python
class SomeClass:
pass
```

2.

```python
c = SomeClass()
```

3. False. in `c.x`, `x` is an attribute of the instance `c` and not a variable
4. `c.x`
15 changes: 11 additions & 4 deletions solutions/classes/attributes.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
## Attributes - Solution
# Attributes

Explain class attributes vs. instance attributes
## Objectives

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

In the following block of code `x` is a class attribute while `self.y` is a instance attribute
## Solution

1. In the following block of code `x` is a class attribute while `self.y` is a instance attribute

```
class MyClass(object):
Expand All @@ -13,3 +17,6 @@ class MyClass(object):
def __init__(self, y):
self.y = y
```

2. False. Every object in Python has attributes
3. True
16 changes: 16 additions & 0 deletions solutions/hello_world/hello_world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Hello World

## Objectives

1. Write one line of code that will print "Hello World!"
2. Now modify the code so the output looks like this:

```
Hello
World
```

## Solution

1. print("Hello World!")
2. print("Hello\n World!")
9 changes: 8 additions & 1 deletion solutions/hello_world/python_characteristics.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## Python Characteristics
## Python Characteristics - Solution

## Objectives

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

## Solution

1.

Expand All @@ -11,3 +16,5 @@
* The python package manager is called PIP "pip installs packages", having more than 200.000 available packages.
* Python comes with pip installed and a big standard library that offers the programmer many precooked solutions.
* In python **Everything** is an object.

2. Web, Gaming, Machine Learning, Web Scrapers, Data (visualization, analytics) and more
15 changes: 11 additions & 4 deletions solutions/loops/refactor_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import namedtuple
import timeit


def before_refactor():

Mushroom = namedtuple('Mushroom', ['name', 'poisonous'])
Expand All @@ -14,7 +15,8 @@ def before_refactor():
for mushroom in mushrooms:
i += 1
name = mushroom.name
print('%d:"%s"'%(i, name))
print('%d:"%s"' % (i, name))


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

Expand All @@ -26,7 +28,12 @@ def after_refactor(): # <- Solution is here! :)
for i, mushroom in enumerate(mushrooms):
i += 1
name = mushroom.name
print('%d:"%s"'%(i, name))
print('%d:"%s"' % (i, name))


print(timeit.timeit("before_refactor()", setup="from __main__ import before_refactor", number=100))
print(timeit.timeit("after_refactor()", setup="from __main__ import after_refactor", number=100))
print(timeit.timeit(
"before_refactor()",
setup="from __main__ import before_refactor", number=100))
print(timeit.timeit(
"after_refactor()",
setup="from __main__ import after_refactor", number=100))
4 changes: 3 additions & 1 deletion solutions/variables/locations_or_names.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

What do you think about the following statement:

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

### Solution

The statement above is wrong. In Python a variable is merely a name, a reference to the object, not the actual object.
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.

So in case of `x = 1` what it means, is x refers to the integer 1.
Loading

0 comments on commit 0255339

Please sign in to comment.