Skip to content

Commit bb15f2f

Browse files
authored
Merge pull request #92 from beladiyadarshan/exercise_16_26
Added : exercises for 16 to 26
2 parents e26c3fe + a841369 commit bb15f2f

23 files changed

+459
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
**/.idea/
44
.ipynb_checkpoints/
55
**/.ipynb_checkpoints/
6-
**/.cache/
6+
**/.cache/
7+
.vscode
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Exercise: Class and Objects
2+
3+
1. Create a sample class named Employee with two attributes id and name
4+
5+
```
6+
employee :
7+
id
8+
name
9+
```
10+
object initializes id and name dynamically for every Employee object created.
11+
12+
```
13+
emp = Employee(1, "coder")
14+
```
15+
16+
2. Use del property to first delete id attribute and then the entire object
17+
18+
19+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/16_class_and_objects/16_class_and_objects.py)
20+
21+
22+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Employee:
2+
3+
def __init__(self, id, name):
4+
self.id = id
5+
self.name = name
6+
7+
def display(self):
8+
print(f"ID: {self.id} \nName: {self.name}")
9+
10+
11+
# Creating a emp instance of Employee class
12+
emp = Employee(1, "coder")
13+
14+
emp.display()
15+
# Deleting the property of object
16+
del emp.id
17+
# Deleting the object itself
18+
try:
19+
print(emp.id)
20+
except NameError:
21+
print("emp.id is not defined")
22+
23+
del emp
24+
try:
25+
emp.display() # it will gives error after deleting emp
26+
except NameError:
27+
print("emp is not defined")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Exercise: Inheritance
2+
3+
1. create inheritance using animal Dog relation.
4+
5+
6+
```
7+
for example,
8+
Animal and Dog both has same habitat so create a method for habitat
9+
```
10+
11+
2. use super() constructor for calling parent constructor.
12+
13+
```
14+
class Animal:
15+
#code
16+
17+
class Dog(Animal):
18+
super()-it refers Animal class,now you can call Animal's methods.
19+
```
20+
21+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/17_inheritance/17_inheritance.py)
22+
23+
24+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Animal:
2+
def __init__(self, habitat):
3+
self.habitat = habitat
4+
5+
def print_habitat(self):
6+
print(self.habitat)
7+
8+
def sound(self):
9+
print("Some Animal Sound")
10+
11+
12+
class Dog(Animal):
13+
def __init__(self):
14+
super().__init__("Kennel")
15+
16+
def sound(self):
17+
print("Woof woof!")
18+
19+
20+
x = Dog()
21+
x.print_habitat()
22+
x.sound()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Exercise: Multiple Inheritance
2+
3+
Real Life Example :
4+
1. Create multiple inheritance on teacher,student and youtuber.
5+
6+
7+
```
8+
Q. if we have created teacher and now one student joins master degree with becoming teacher then what??
9+
10+
Ans : just make subclass from teacher so that student will become teacher
11+
```
12+
13+
2. Now student is teacher as well as youtuber then what???
14+
15+
16+
```
17+
-just use multiple inheritance for these three relations
18+
19+
```
20+
21+
22+
23+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/18_multiple_inheritance/18_multiple_inheritance.py)
24+
25+
26+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Teacher:
2+
def teachers_action(self):
3+
print("I can teach")
4+
5+
6+
class Engineer:
7+
def Engineers_action(self):
8+
print("I can code")
9+
10+
11+
class Youtuber:
12+
def youtubers_action(self):
13+
print("I can code and teach")
14+
15+
16+
class Person(Teacher, Engineer, Youtuber):
17+
pass
18+
19+
20+
coder = Person()
21+
coder.teachers_action()
22+
coder.Engineers_action()
23+
coder.youtubers_action()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Exercise: Raise Exception And Finally
2+
3+
1. Create a custom exception AdultException.
4+
5+
2. Create a class Person with attributes name and age in it.
6+
7+
3. Create a function get_minor_age() in the class. It throws an exception if the person is adult otherwise returns age.
8+
9+
4. Create a function display_person() which prints the age and name of a person.
10+
```
11+
let us say,
12+
13+
if age>18
14+
he is major
15+
else
16+
raise exception
17+
18+
create cusomException named ismajor and raise it if age<18.
19+
```
20+
21+
22+
23+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py)
24+
25+
26+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# for making exception just make subclass of Exception
2+
class AdultException(Exception):
3+
pass
4+
5+
6+
class Person:
7+
def __init__(self, name, age):
8+
self.name = name
9+
self.age = age
10+
11+
def get_minor_age(self):
12+
if int(self.age) >= 18:
13+
raise AdultException
14+
else:
15+
return self.age
16+
17+
def display(self):
18+
try:
19+
print(f"age -> {self.get_minor_age()}")
20+
except AdultException:
21+
print("Person is an adult")
22+
finally:
23+
print(f"name -> {self.name}")
24+
25+
26+
# No exception
27+
Person("Bhavin", 17).display()
28+
29+
# AdultException is raised
30+
Person("Dhaval", 23).display()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Exercise: Iterators
2+
3+
1. Create an iterator for fibonacci series in such a way that each next returns the next element from fibonacci series.
4+
2. The iterator should stop when it reaches a `limit` defined in the constructor.
5+
6+
7+
8+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/20_Iterators/20_Iterators.py)

0 commit comments

Comments
 (0)