You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python is an object oriented programming language. Everything in Python is an object, with its properties and methods. A number, string, list, dictionary, tuple, set etc. used in a program is an object of a corresponding built-in class. We create class to create an object. A Class is like an object constructor, or a "blueprint" for creating objects. We instantiate a class to create an object. The class defines attributes and the behavior of the object, while the object, on the other hand, represents the class.
40
+
Python is an object oriented programming language. Everything in Python is an object, with its properties and methods. A number, string, list, dictionary, tuple, set etc. used in a program is an object of a corresponding built-in class. We create class to create an object. A class is like an object constructor, or a "blueprint" for creating objects. We instantiate a class to create an object. The class defines attributes and the behavior of the object, while the object, on the other hand, represents the class.
39
41
40
42
We have been working with classes and objects right from the beginning of this challenge unknowingly. Every element in a Python program is an object of a class.
41
-
Let's check if everything in python is a class:
43
+
Let us check if everything in python is a class:
42
44
43
45
```py
44
-
Last login: Tue Dec 1009:35:28 on console
45
-
asabeneh@Asabeneh:~$ pyhton
46
-
-bash: pyhton: command not found
47
46
asabeneh@Asabeneh:~$ python
48
-
Python 3.7.5 (default, Nov 12019, 02:16:32)
47
+
Python 3.9.6 (default, Jun 282021, 15:26:21)
49
48
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
50
49
Type "help", "copyright", "credits"or"license"for more information.
51
50
>>> num = 10
@@ -86,6 +85,7 @@ class ClassName:
86
85
```py
87
86
class Person:
88
87
pass
88
+
print(Person)
89
89
```
90
90
91
91
```sh
@@ -103,12 +103,13 @@ print(p)
103
103
104
104
### Class Constructor
105
105
106
-
In the examples above, we have created an objectfrom the Person class. However, Class without a constructor isnot really useful in real applications. Let's use constructor function to make our class more useful. Like the constructor function in Java or JavaScript, python has also a built-in _**init**()_ constructor function. The _**init**_ constructor function has self parameter which is a reference to the current instance of the class
106
+
In the examples above, we have created an objectfrom the Person class. However, a classwithout a constructor isnot really useful in real applications. Let us use constructor function to make our class more useful. Like the constructor function in Java or JavaScript, Python has also a built-in**__init__**() constructor function. The **__init__** constructor function has self parameter which is a reference to the current instance of the class
107
107
**Examples:**
108
108
109
109
```py
110
110
classPerson:
111
111
def__init__ (self, name):
112
+
# self allows to attach parameter to the class
112
113
self.name =name
113
114
114
115
p = Person('Asabeneh')
@@ -122,7 +123,7 @@ Asabeneh
122
123
<__main__.Person object at 0x2abf46907e80>
123
124
```
124
125
125
-
Let's add more parameters to the constructor function.
126
+
Let us add more parameters to the constructor function.
126
127
127
128
```py
128
129
classPerson:
@@ -165,11 +166,9 @@ class Person:
165
166
self.age = age
166
167
self.country = country
167
168
self.city = city
168
-
169
169
defperson_info(self):
170
170
returnf'{self.firstname}{self.lastname} is {self.age} years old. He lives in {self.city}, {self.country}'
171
171
172
-
173
172
p = Person('Asabeneh', 'Yetayeh', 250, 'Finland', 'Helsinki')
174
173
print(p.person_info())
175
174
```
@@ -181,7 +180,7 @@ Asabeneh Yetayeh is 250 years old. He lives in Helsinki, Finland
181
180
182
181
### Object Default Methods
183
182
184
-
Sometimes, you may want to have a default values for you object methods. If we give default values for the parameters in the constructor, we can avoid errors when we call or instantiate our class without parameters. Let's see how it looks:
183
+
Sometimes, you may want to have a default values for your object methods. If we give default values for the parameters in the constructor, we can avoid errors when we call or instantiate our class without parameters. Let's see how it looks:
185
184
186
185
**Example:**
187
186
@@ -211,7 +210,7 @@ John Doe is 30 years old. He lives in Noman city, Nomanland.
211
210
212
211
### Method to Modify Class Default Values
213
212
214
-
In the example below, the person class, all the constructor parameters have default values. In addition to that, we have skills parameter, which we can access using a method. Let's create add_skill method to add skills to the skills list.
213
+
In the example below, the person class, all the constructor parameters have default values. In addition to that, we have skills parameter, which we can access using a method. Let us create add_skill method to add skills to the skills list.
215
214
216
215
```py
217
216
classPerson:
@@ -249,8 +248,8 @@ John Doe is 30 years old. He lives in Noman city, Nomanland.
249
248
250
249
### Inheritance
251
250
252
-
Using inheritance we can reuse parent class code. Inheritance allows us to define a class that inherits all the methods and properties from another class. The parent class or super or base class is the class which gives all the methods and properties. Child class is the class that inherits from another class.
253
-
Let's create a student class by inheriting from person class.
251
+
Using inheritance we can reuse parent class code. Inheritance allows us to define a class that inherits all the methods and properties from parent class. The parent class or super or base class is the class which gives all the methods and properties. Child class is the class that inherits from another or parent class.
252
+
Let us create a student class by inheriting from person class.
254
253
255
254
```py
256
255
classStudent(Person):
@@ -281,8 +280,8 @@ Lidiya Teklemariam is 28 years old. He lives in Espoo, Finland.
281
280
['Organizing', 'Marketing', 'Digital Marketing']
282
281
```
283
282
284
-
We didn't call the _**init**()_ constructor in the child class. If we didn't call it then we can still access all the properties from the parent. But if we do call the constructor we can access the parent properties by calling _super_.
285
-
We can add a new method to the child or we can overwrite the parent class by creating the same method name in the child class. When we add the **init**() function, the child class will no longer inherit the parent's **init**() function.
283
+
We did not call the **__init__**() constructor in the child class. If we didn't call it then we can still access all the properties from the parent. But if we do call the constructor we can access the parent properties by calling _super_.
284
+
We can add a new method to the child or we can override the parent class methods by creating the same method name in the child class. When we add the **__init__**() function, the child class will no longer inherit the parent's **__init__**() function.
286
285
287
286
### Overriding parent method
288
287
@@ -317,13 +316,15 @@ Lidiya Teklemariam is 28 years old. She lives in Espoo, Finland.
317
316
['Organizing', 'Marketing', 'Digital Marketing']
318
317
```
319
318
320
-
We can use super() function or the parent name Person to automatically inherit the methods and properties from its parent. In the example above we override the parant method. The child method has a different feature, it can identify, if the gender is male or female and assign the proper pronoun(He/She).
319
+
We can use super() built-in function or the parent name Person to automatically inherit the methods and properties from its parent. In the example above we override the parent method. The child method has a different feature, it can identify, if the gender is male or female and assign the proper pronoun(He/She).
321
320
322
-
🌕 Now, you are fully charged with a super power of programming. Now do some exercises for your brain and for your muscle.
321
+
🌕 Now, you are fully charged with a super power of programming. Now do some exercises for your brain and muscles.
323
322
324
323
## 💻 Exercises: Day 21
325
324
326
-
1. Python has the module called _statistics_ and we can use this module to do all the statistical caluculations. However to challlenge ourselves, let's try to develop a program, which calculates the measure of central tendency of a sample (mean, median, mode) and measure of variability (range, variance, standard deviation). In addition to those measures, find the min, max, count, percentile, and frequency distribution of the sample. You can create a class called Statistics and create all the functions that do statistical calculations as methods for the Statistics class. Check the output below.
325
+
### Exercises: Level 1
326
+
327
+
1. Python has the module called _statistics_ and we can use this module to do all the statistical calculations. However, to learn how to make function and reuse function let us try to develop a program, which calculates the measure of central tendency of a sample (mean, median, mode) and measure of variability (range, variance, standard deviation). In addition to those measures, find the min, max, count, percentile, and frequency distribution of the sample. You can create a class called Statistics and create all the functions that do statistical calculations as methods for the Statistics class. Check the output below.
2. Create a class called PersonAccount. It has firstname, lastname, incomes, expenses properties and it has total_income, total_expense, account_info, add_income, add_expense and account_balance methods. Incomes is a set of incomes and its description. The same goes for expenses.
361
+
### Exercises: Level 2
362
+
363
+
1. Create a class called PersonAccount. It has firstname, lastname, incomes, expenses properties and it has total_income, total_expense, account_info, add_income, add_expense and account_balance methods. Incomes is a set of incomes and its description. The same goes for expenses.
0 commit comments