From edfba368186ce113bab75158b8121f62ae32cc58 Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:06:49 +0200 Subject: [PATCH 01/11] Completed Python Beginner 'use of _' answer --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2cd45b6..2af2d93 100644 --- a/README.md +++ b/README.md @@ -1705,7 +1705,7 @@ Shortest way is str[::-1] but not the most efficient. What _ is used for in Python?
1. Translation lookup in i18n -2. Hold the result of the last executed expression or statement +2. Hold the result of the last executed expression or statement in the interactive interpreter. 3. As a general purpose "throwaway" variable name. For example: x, y, _ = get_data() (x and y are used but since we don't care about third variable, we "threw it away"). From 279c727ea02c5c2004ca35985c5509524ddf059c Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:19:57 +0200 Subject: [PATCH 02/11] add Python begginner reverse a string answer --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2af2d93..d152ea3 100644 --- a/README.md +++ b/README.md @@ -1751,8 +1751,14 @@ set([food for bro in x for food in bro['food']])
How to reverse a string?
-Shortest way is: my_string[::-1] but it doesn't mean it's the most efficient one. -
+Shortest way is: my_string[::-1] but it doesn't mean it's the most efficient one.
+Cassic way is: +def reverse_string(string): + temp = "" + for char in string: + temp = char + temp + return temp +
Write a function to determine if a given string is a palindrome
From b59c26f1cfb1600daaaa0878152c9d685bb047d5 Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:21:42 +0200 Subject: [PATCH 03/11] formating fixed --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d152ea3..ab9b7fb 100644 --- a/README.md +++ b/README.md @@ -1752,13 +1752,13 @@ set([food for bro in x for food in bro['food']]) How to reverse a string?
Shortest way is: my_string[::-1] but it doesn't mean it's the most efficient one.
-Cassic way is: +Cassic way is: ``` def reverse_string(string): temp = "" for char in string: temp = char + temp return temp -
+```
Write a function to determine if a given string is a palindrome
From cd1f0db6ffd98dae420f34e910923de5be7e210c Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:22:34 +0200 Subject: [PATCH 04/11] formating fixed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ab9b7fb..ba9f66d 100644 --- a/README.md +++ b/README.md @@ -1758,7 +1758,7 @@ def reverse_string(string): for char in string: temp = char + temp return temp -```
+
Write a function to determine if a given string is a palindrome
From 4b245d96f9793f503c129cfb57d203b6e6efdfe7 Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:23:31 +0200 Subject: [PATCH 05/11] formating fixed --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ba9f66d..a00df5a 100644 --- a/README.md +++ b/README.md @@ -1752,14 +1752,17 @@ set([food for bro in x for food in bro['food']]) How to reverse a string?
Shortest way is: my_string[::-1] but it doesn't mean it's the most efficient one.
-Cassic way is: ``` +Cassic way is: +``` def reverse_string(string): temp = "" for char in string: temp = char + temp return temp +```
+
Write a function to determine if a given string is a palindrome
From b4e9ec46c076abecd56310891ddf65d19f3dcc91 Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:53:10 +0200 Subject: [PATCH 06/11] Add Python begginner question 'Python characteristics' answer --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index a00df5a..c70089d 100644 --- a/README.md +++ b/README.md @@ -1642,6 +1642,17 @@ Swarm management which means you can create new swarms in Docker Cloud.
What are some characteristics of the Python programming language?
+``` +1. It is a high level general purpose programming language created in 1991 by Guido Van Rosum. +2. The language is interpreted, being the CPython (Written in C) the most used/maintained implementation. +3. It is strongly typed. The typing discipline is duck typing and gradual. +4. Python focuses on readability and makes use of whitespaces/identation instead of brackets { } +5. The python packate manager is called PIP "pip installs packages", having more than 200.000 available packages. +6. Python comes with pip installed and a big standard library that offers the programmer many precooked solutions. +7. In python **Everything** is an object. + +There are many other characteristics but these are the main ones that every python programmer should know. +```
@@ -1719,6 +1730,10 @@ Shortest way is str[::-1] but not the most efficient.
How to write to a file?
+``` +with open('file.txt', 'w') as file: + file.write("My insightful comment") +```
From 0fb14b93a98164f233c9175220d45ece701db4c0 Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:53:53 +0200 Subject: [PATCH 07/11] Fixed formating --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c70089d..6508846 100644 --- a/README.md +++ b/README.md @@ -1642,6 +1642,7 @@ Swarm management which means you can create new swarms in Docker Cloud.
What are some characteristics of the Python programming language?
+ ``` 1. It is a high level general purpose programming language created in 1991 by Guido Van Rosum. 2. The language is interpreted, being the CPython (Written in C) the most used/maintained implementation. @@ -1652,6 +1653,7 @@ Swarm management which means you can create new swarms in Docker Cloud. 7. In python **Everything** is an object. There are many other characteristics but these are the main ones that every python programmer should know. + ```
From 2522f5c32428cd0992713fb0c3c21edd1022ef2c Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 09:58:47 +0200 Subject: [PATCH 08/11] Add python answers --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 6508846..dc4ba3c 100644 --- a/README.md +++ b/README.md @@ -1707,7 +1707,18 @@ PEP8 is a list of coding conventions and style guidelines for Python ``` Shortest way is str[::-1] but not the most efficient. + +"Classic" way: + +foo = '' + +for char in 'pizza': + foo = char + foo + +>> 'azzip' + ``` +
@@ -1732,10 +1743,12 @@ Shortest way is str[::-1] but not the most efficient.
How to write to a file?
+ ``` with open('file.txt', 'w') as file: file.write("My insightful comment") ``` +
@@ -1757,8 +1770,22 @@ sorted(x, key=lambda l: l[1]) Extract all type of foods. Final output should be: {'mushrooms', 'goombas', 'turtles'}
``` +brothers_menu = \ +[{'name': 'Mario', 'food': ['mushrooms', 'goombas']}, {'name': 'Luigi', 'food': ['mushrooms', 'turtles']}] + +# "Classic" Way +def get_food(brothers_menu) -> set: + temp = [] + + for brother in brothers_menu: + for food in brother['food']: + temp.append(food) + + return set(temp) + set([food for bro in x for food in bro['food']]) ``` +
From 337b74d8d0819000273d7dc263f4d572df3f9b2b Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 10:27:22 +0200 Subject: [PATCH 09/11] add Python inheritance answers --- README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/README.md b/README.md index dc4ba3c..55e2b30 100644 --- a/README.md +++ b/README.md @@ -1692,6 +1692,69 @@ PEP8 is a list of coding conventions and style guidelines for Python
Explain inheritance and how to use it in Python
+``` +By definition inheritance is the mechanism where an object acts as a base of another object, retaining all its +properties. + +So if Class B inherits from Class A, every characteristics from class A will be also available in class B. +Class A would be the 'Base class' and B class would be the 'derived class'. + +This comes handy when you have several classes that share the same functionalities. + +The basic syntax is: + +class Base: pass + +class Derived(Base): pass + +A more forged example: + +class Animal: + def __init__(self): + print("and I'm alive!") + + def eat(self, food): + print("ñom ñom ñom", food) + +class Human(Animal): + def __init__(self, name): + print('My name is ', name) + super().__init__() + + def write_poem(self): + print('Foo bar bar foo foo bar!') + +class Dog(Animal): + def __init__(self, name): + print('My name is', name) + super().__init__() + + def bark(self): + print('woof woof') + + +michael = Human('Michael') +michael.eat('Spam') +michael.write_poem() + +bruno = Dog('Bruno') +bruno.eat('bone') +bruno.bark() + +>>> My name is Michael +>>> and I'm alive! +>>> ñom ñom ñom Spam +>>> Foo bar bar foo foo bar! +>>> My name is Bruno +>>> and I'm alive! +>>> ñom ñom ñom bone +>>> woof woof + +Calling super() calls the Base method, thus, calling super().__init__() we called the Animal __init__. + +There is a more advanced python feature called MetaClasses that aid the programmer to directly control class creation. +``` +
From e497b6bcf0898eecf6dc25f37f8838ebb8474634 Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 10:28:01 +0200 Subject: [PATCH 10/11] fix format --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 55e2b30..abd9bb8 100644 --- a/README.md +++ b/README.md @@ -1692,6 +1692,7 @@ PEP8 is a list of coding conventions and style guidelines for Python
Explain inheritance and how to use it in Python
+ ``` By definition inheritance is the mechanism where an object acts as a base of another object, retaining all its properties. From deaf3736c267aff4ade4ee5369494e8eb51d6d7f Mon Sep 17 00:00:00 2001 From: surister Date: Tue, 22 Oct 2019 10:29:03 +0200 Subject: [PATCH 11/11] fix format --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index abd9bb8..f3c886c 100644 --- a/README.md +++ b/README.md @@ -1754,6 +1754,7 @@ bruno.bark() Calling super() calls the Base method, thus, calling super().__init__() we called the Animal __init__. There is a more advanced python feature called MetaClasses that aid the programmer to directly control class creation. + ```