Skip to content

Commit 241ca3b

Browse files
committed
classes
1 parent 13e0203 commit 241ca3b

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

21_Day_Classes_and_objects/21_classes_and_objects.md

+28-22
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
<sub>Author:
1111
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
12-
<small> First Edition: Nov 22 - Dec 22, 2019</small>
12+
<small>Second Edition: July, 2021</small>
1313
</sub>
1414

15-
</div>
1615
</div>
1716

1817
[<< Day 20](../20_Day_Python_package_manager/20_python_package_manager.md) | [Day 22 >>](../22_Day_Web_scraping/22_web_scraping.md)
@@ -30,22 +29,22 @@
3029
- [Inheritance](#inheritance)
3130
- [Overriding parent method](#overriding-parent-method)
3231
- [💻 Exercises: Day 21](#-exercises-day-21)
32+
- [Exercises: Level 1](#exercises-level-1)
33+
- [Exercises: Level 2](#exercises-level-2)
34+
- [Exercises: Level 3](#exercises-level-3)
3335

3436
# 📘 Day 21
3537

3638
## Classes and Objects
3739

38-
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.
3941

4042
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:
4244

4345
```py
44-
Last login: Tue Dec 10 09:35:28 on console
45-
asabeneh@Asabeneh:~$ pyhton
46-
-bash: pyhton: command not found
4746
asabeneh@Asabeneh:~$ python
48-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
47+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
4948
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
5049
Type "help", "copyright", "credits" or "license" for more information.
5150
>>> num = 10
@@ -86,6 +85,7 @@ class ClassName:
8685
```py
8786
class Person:
8887
pass
88+
print(Person)
8989
```
9090

9191
```sh
@@ -103,12 +103,13 @@ print(p)
103103

104104
### Class Constructor
105105

106-
In the examples above, we have created an object from the Person class. However, Class without a constructor is not 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 object from the Person class. However, a class without a constructor is not 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
107107
**Examples:**
108108

109109
```py
110110
class Person:
111111
def __init__ (self, name):
112+
# self allows to attach parameter to the class
112113
self.name =name
113114

114115
p = Person('Asabeneh')
@@ -122,7 +123,7 @@ Asabeneh
122123
<__main__.Person object at 0x2abf46907e80>
123124
```
124125

125-
Let's add more parameters to the constructor function.
126+
Let us add more parameters to the constructor function.
126127

127128
```py
128129
class Person:
@@ -165,11 +166,9 @@ class Person:
165166
self.age = age
166167
self.country = country
167168
self.city = city
168-
169169
def person_info(self):
170170
return f'{self.firstname} {self.lastname} is {self.age} years old. He lives in {self.city}, {self.country}'
171171

172-
173172
p = Person('Asabeneh', 'Yetayeh', 250, 'Finland', 'Helsinki')
174173
print(p.person_info())
175174
```
@@ -181,7 +180,7 @@ Asabeneh Yetayeh is 250 years old. He lives in Helsinki, Finland
181180

182181
### Object Default Methods
183182

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:
185184

186185
**Example:**
187186

@@ -211,7 +210,7 @@ John Doe is 30 years old. He lives in Noman city, Nomanland.
211210

212211
### Method to Modify Class Default Values
213212

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.
215214

216215
```py
217216
class Person:
@@ -249,8 +248,8 @@ John Doe is 30 years old. He lives in Noman city, Nomanland.
249248

250249
### Inheritance
251250

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.
254253

255254
```py
256255
class Student(Person):
@@ -281,8 +280,8 @@ Lidiya Teklemariam is 28 years old. He lives in Espoo, Finland.
281280
['Organizing', 'Marketing', 'Digital Marketing']
282281
```
283282

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.
286285

287286
### Overriding parent method
288287

@@ -317,13 +316,15 @@ Lidiya Teklemariam is 28 years old. She lives in Espoo, Finland.
317316
['Organizing', 'Marketing', 'Digital Marketing']
318317
```
319318

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).
321320

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.
323322

324323
## 💻 Exercises: Day 21
325324

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.
327328

328329
```py
329330
ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27, 27, 24, 32, 33, 27, 25, 26, 38, 37, 31, 34, 24, 33, 29, 26]
@@ -357,7 +358,12 @@ Standard Deviation: 4.2
357358
Frequency Distribution: [(20.0, 26), (16.0, 27), (12.0, 32), (8.0, 37), (8.0, 34), (8.0, 33), (8.0, 31), (8.0, 24), (4.0, 38), (4.0, 29), (4.0, 25)]
358359
```
359360

360-
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.
364+
365+
### Exercises: Level 3
366+
361367

362368
🎉 CONGRATULATIONS ! 🎉
363369

0 commit comments

Comments
 (0)