diff --git a/README.md b/README.md index 2cd45b6..f3c886c 100644 --- a/README.md +++ b/README.md @@ -1642,6 +1642,19 @@ 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. + +```
@@ -1679,6 +1692,71 @@ 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. + +``` +
@@ -1694,7 +1772,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' + ``` +
@@ -1705,7 +1794,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").
@@ -1719,6 +1808,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") +``` +
@@ -1740,8 +1835,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']]) ``` +
@@ -1751,9 +1860,18 @@ 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