From 02553393894085dcd78e729168c8429dace63aea Mon Sep 17 00:00:00 2001 From: abregman Date: Sat, 25 Jun 2022 22:17:42 +0300 Subject: [PATCH] Add a couple of exercises Also TOC for easier navigation. --- README.md | 93 +++++++++++++++++-- exercises/classes/101.md | 10 ++ exercises/classes/attributes.md | 8 +- exercises/hello_world/hello_world.md | 10 +- .../hello_world/python_characteristics.md | 7 +- exercises/variables/locations_or_names.md | 2 +- solutions/classes/101.md | 26 ++++++ solutions/classes/attributes.md | 15 ++- solutions/hello_world/hello_world.md | 16 ++++ .../hello_world/python_characteristics.md | 9 +- solutions/loops/refactor_1.py | 15 ++- solutions/variables/locations_or_names.md | 4 +- tox.ini | 23 +---- 13 files changed, 191 insertions(+), 47 deletions(-) create mode 100644 exercises/classes/101.md create mode 100644 solutions/classes/101.md create mode 100644 solutions/hello_world/hello_world.md diff --git a/README.md b/README.md index f275ad5..1827351 100644 --- a/README.md +++ b/README.md @@ -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 + + ## 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) | | | + ## Objects & Data Types |Name|Objective & Instructions|Solution|Comments| @@ -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) | | + ## Variables |Name|Objective & Instructions|Solution|Comments| @@ -38,6 +68,7 @@ | 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) | | + ## Booleans |Name|Objective & Instructions|Solution|Comments| @@ -45,6 +76,7 @@ | 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) | | + ## Strings |Name|Objective & Instructions|Solution|Comments| @@ -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) | | + ## Numbers |Name|Objective & Instructions|Solution|Comments| @@ -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) | | + ## Lists & Tuples |Name|Objective & Instructions|Solution|Comments| @@ -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) | | + ## Dictionaries |Name|Objective & Instructions|Solution|Comments| |--------|--------|------|----| | Facts Only | [Exercise](exercises/dicts/facts_only.md) | [Solution](solutions/dict/facts_only.md) | | + ## Loops |Name|Objective & Instructions|Solution|Comments| @@ -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) | | + ## Functions |Name|Objective & Instructions|Solution|Comments| @@ -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 + +## 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 + +## 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 @@ -186,3 +226,38 @@ |--------|--------|------|----| | Random Number | [Exercise](exercises/misc/random_number.md) | | | | Random Item | [Exercise](exercises/misc/random_item.md) | | | + +# Questions + + +## Hello World + +
+How to print "Hello World"?
+ +`print("Hello World")` +
+ + +## Classes + +
+Define a class that does nothing
+ +```python +class SomeClass: + pass +``` +
+ +
+True or False? If c is an instance of a class, then in c.x = 1, x is a variable of the value 1
+ +False. `x` is an attribute in the case `c.x = 1` +
+ +
+True or False? Every object in Python has attributes
+ +True. You can think on attributes as private dictionaries but instead of accessing them with `[]` or `.get`, they are accessed by using a dot. +
diff --git a/exercises/classes/101.md b/exercises/classes/101.md new file mode 100644 index 0000000..b722eae --- /dev/null +++ b/exercises/classes/101.md @@ -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) diff --git a/exercises/classes/attributes.md b/exercises/classes/attributes.md index 5e429d9..f316ca9 100644 --- a/exercises/classes/attributes.md +++ b/exercises/classes/attributes.md @@ -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 diff --git a/exercises/hello_world/hello_world.md b/exercises/hello_world/hello_world.md index 144f761..955f0d2 100644 --- a/exercises/hello_world/hello_world.md +++ b/exercises/hello_world/hello_world.md @@ -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) diff --git a/exercises/hello_world/python_characteristics.md b/exercises/hello_world/python_characteristics.md index cda4aa4..713d12f 100644 --- a/exercises/hello_world/python_characteristics.md +++ b/exercises/hello_world/python_characteristics.md @@ -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) diff --git a/exercises/variables/locations_or_names.md b/exercises/variables/locations_or_names.md index 89260fc..8a88095 100644 --- a/exercises/variables/locations_or_names.md +++ b/exercises/variables/locations_or_names.md @@ -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. diff --git a/solutions/classes/101.md b/solutions/classes/101.md new file mode 100644 index 0000000..b41a503 --- /dev/null +++ b/solutions/classes/101.md @@ -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` diff --git a/solutions/classes/attributes.md b/solutions/classes/attributes.md index 4bbe1c2..74c0c07 100644 --- a/solutions/classes/attributes.md +++ b/solutions/classes/attributes.md @@ -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): @@ -13,3 +17,6 @@ class MyClass(object): def __init__(self, y): self.y = y ``` + +2. False. Every object in Python has attributes +3. True diff --git a/solutions/hello_world/hello_world.md b/solutions/hello_world/hello_world.md new file mode 100644 index 0000000..d9551ff --- /dev/null +++ b/solutions/hello_world/hello_world.md @@ -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!") diff --git a/solutions/hello_world/python_characteristics.md b/solutions/hello_world/python_characteristics.md index 9e0a8f8..9b01413 100644 --- a/solutions/hello_world/python_characteristics.md +++ b/solutions/hello_world/python_characteristics.md @@ -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. @@ -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 diff --git a/solutions/loops/refactor_1.py b/solutions/loops/refactor_1.py index aa680e9..1e30b5b 100644 --- a/solutions/loops/refactor_1.py +++ b/solutions/loops/refactor_1.py @@ -3,6 +3,7 @@ from collections import namedtuple import timeit + def before_refactor(): Mushroom = namedtuple('Mushroom', ['name', 'poisonous']) @@ -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! :) @@ -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)) diff --git a/solutions/variables/locations_or_names.md b/solutions/variables/locations_or_names.md index 3ef7984..663a6d1 100644 --- a/solutions/variables/locations_or_names.md +++ b/solutions/variables/locations_or_names.md @@ -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. diff --git a/tox.ini b/tox.ini index c104fc8..c692b1c 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 3.8.0 -envlist = linters,unit,coverage +envlist = linters skipsdist = True ignore_basepython_conflict = True skip_missing_interpreters = False @@ -15,26 +15,5 @@ install_command = pip install -U {opts} {packages} deps = pre-commit>=1.21.0 pylint>=2.12.0 - -r {toxinidir}/requirements.txt - -r {toxinidir}/test-requirements.txt commands = python -m pre_commit run -a - -[testenv:unit] -deps = - -r {toxinidir}/requirements.txt - -r {toxinidir}/test-requirements.txt -commands = - python -m unittest - -[gh-actions] -python = - 3.9: py39 - -[testenv:coverage] -deps = - -r {toxinidir}/requirements.txt - -r {toxinidir}/test-requirements.txt -commands = - coverage run -m unittest discover - coverage report --fail-under=90