File tree Expand file tree Collapse file tree 7 files changed +189
-0
lines changed
Expand file tree Collapse file tree 7 files changed +189
-0
lines changed Original file line number Diff line number Diff line change 2727| Name| Objective & Instructions| Solution| Comments|
2828| --------| --------| ------| ----|
2929| Valid Names | [ Exercise] ( exercises/variables/valid_names.md ) | [ Solution] ( solutions/variables/valid_names.md ) | |
30+ | Locations or Names | [ Exercise] ( exercises/variables/locations_or_names.md ) | [ Solution] ( solutions/variables/locations_or_names.md ) | |
31+ | Types | [ Exercise] ( exercises/variables/types.md ) | [ Solution] ( solutions/variables/types.md ) | |
3032
3133## Booleans
3234
7678| My First Function | [ Exercise] ( exercises/functions/my_first_function.md ) | [ Solution] ( solutions/functions/my_first_function.md ) | |
7779| Calculator | [ Exercise] ( exercises/functions/calculator.md ) | [ Solution] ( solutions/functions/calculator.md ) | |
7880| First Class Objects | [ Exercise] ( exercises/functions/first_class_objects.md ) | [ Solution] ( solutions/functions/first_class_objects.md ) | |
81+
82+ ## OOP
83+
84+ | Name| Objective & Instructions| Solution| Comments|
85+ | --------| --------| ------| ----|
86+ | Inheritance | [ Exercise] ( exercises/oop/inheritance.md ) | [ Solution] ( solutions/oop/inheritance.md ) | |
87+
88+ ## Classes
89+
90+ | Name| Objective & Instructions| Solution| Comments|
91+ | --------| --------| ------| ----|
92+ | Attributes | [ Exercise] ( exercises/classes/attributes.md ) | [ Solution] ( solutions/classes/attributes.md ) | |
7993
8094## Magic Methods
8195
Original file line number Diff line number Diff line change 1+ ## Inheritance
2+
3+ Explain what is inheritance and how does it work in Python
4+
5+ ### Solution
6+
7+ By definition inheritance is the mechanism where an object acts as a base of another object, retaining all its
8+ properties.
9+
10+ So if Class B inherits from Class A, every characteristics from class A will be also available in class B.
11+ Class A would be the 'Base class' and B class would be the 'derived class'.
12+
13+ This comes handy when you have several classes that share the same functionalities.
14+
15+ The basic syntax is:
16+
17+ class Base: pass
18+
19+ class Derived(Base): pass
20+
21+ A more forged example:
22+
23+ class Animal:
24+ def __ init__ (self):
25+ print("and I'm alive!")
26+
27+ def eat(self, food):
28+ print("ñom ñom ñom", food)
29+
30+ class Human(Animal):
31+ def __ init__ (self, name):
32+ print('My name is ', name)
33+ super().__ init__ ()
34+
35+ def write_poem(self):
36+ print('Foo bar bar foo foo bar!')
37+
38+ class Dog(Animal):
39+ def __ init__ (self, name):
40+ print('My name is', name)
41+ super().__ init__ ()
42+
43+ def bark(self):
44+ print('woof woof')
45+
46+
47+ michael = Human('Michael')
48+ michael.eat('Spam')
49+ michael.write_poem()
50+
51+ bruno = Dog('Bruno')
52+ bruno.eat('bone')
53+ bruno.bark()
54+
55+ >>> My name is Michael
56+ >>> and I'm alive!
57+ >>> ñom ñom ñom Spam
58+ >>> Foo bar bar foo foo bar!
59+ >>> My name is Bruno
60+ >>> and I'm alive!
61+ >>> ñom ñom ñom bone
62+ >>> woof woof
63+
64+ Calling super() calls the Base method, thus, calling super().__ init__ () we called the Animal __ init__ .
65+
66+ There is a more advanced python feature called MetaClasses that aid the programmer to directly control class creation.
Original file line number Diff line number Diff line change 1+ ## Locations or Names
2+
3+ What do you think about the following statement:
4+
5+ In Python, variables are locations, not just names. Each
Original file line number Diff line number Diff line change 1+ ## Attributes
2+
3+ Explain class attributes vs. instance attributes
4+
5+ ### Solution
6+
7+ In the following block of code ` x ` is a class attribute while ` self.y ` is a instance attribute
8+
9+ ```
10+ class MyClass(object):
11+ x = 1
12+
13+ def __init__(self, y):
14+ self.y = y
15+ ```
Original file line number Diff line number Diff line change 1+ ## Inheritance
2+
3+ Explain what is inheritance and how does it work in Python
4+
5+ ### Solution
6+
7+ By definition inheritance is the mechanism where an object acts as a base of another object, retaining all its
8+ properties.
9+
10+ So if Class B inherits from Class A, every characteristics from class A will be also available in class B.
11+ Class A would be the 'Base class' and B class would be the 'derived class'.
12+
13+ This comes handy when you have several classes that share the same functionalities.
14+
15+ The basic syntax is:
16+
17+ class Base: pass
18+
19+ class Derived(Base): pass
20+
21+ A more forged example:
22+
23+ class Animal:
24+ def __ init__ (self):
25+ print("and I'm alive!")
26+
27+ def eat(self, food):
28+ print("ñom ñom ñom", food)
29+
30+ class Human(Animal):
31+ def __ init__ (self, name):
32+ print('My name is ', name)
33+ super().__ init__ ()
34+
35+ def write_poem(self):
36+ print('Foo bar bar foo foo bar!')
37+
38+ class Dog(Animal):
39+ def __ init__ (self, name):
40+ print('My name is', name)
41+ super().__ init__ ()
42+
43+ def bark(self):
44+ print('woof woof')
45+
46+
47+ michael = Human('Michael')
48+ michael.eat('Spam')
49+ michael.write_poem()
50+
51+ bruno = Dog('Bruno')
52+ bruno.eat('bone')
53+ bruno.bark()
54+
55+ >>> My name is Michael
56+ >>> and I'm alive!
57+ >>> ñom ñom ñom Spam
58+ >>> Foo bar bar foo foo bar!
59+ >>> My name is Bruno
60+ >>> and I'm alive!
61+ >>> ñom ñom ñom bone
62+ >>> woof woof
63+
64+ Calling super() calls the Base method, thus, calling super().__ init__ () we called the Animal __ init__ .
65+
66+ There is a more advanced python feature called MetaClasses that aid the programmer to directly control class creation.
Original file line number Diff line number Diff line change 1+ ## Locations or Names
2+
3+ ### Objective
4+
5+ What do you think about the following statement:
6+
7+ In Python, variables are locations, not just names. Each
8+
9+ ### Solution
10+
11+ The statement above is wrong. In Python a variable is merely a name, a reference to the object, not the actual object.
12+ 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.
Original file line number Diff line number Diff line change 1+ ## Types
2+
3+ 1 . How to check the type of a variable in Python?
4+ 2 . How to check if a variable points to an object of an integer type?
5+ 3 . How to check type of a literal?
6+
7+ ### Solution
8+
9+ 1 . ` type(some_var) `
10+ 2 . ` isinstance(some_var, int) `
11+ 3 . Same way: ` type(2017) ` and ` isinstance(2022, int) `
You can’t perform that action at this time.
0 commit comments