Skip to content

Commit f2a8f36

Browse files
author
d_p_beladiya
committed
Added : exercises for 16 to 26
1 parent 2123111 commit f2a8f36

File tree

22 files changed

+408
-0
lines changed

22 files changed

+408
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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("ID: %d \nName: %s" % (self.id, self.name))
9+
10+
# Creating a emp instance of Employee class
11+
emp = Employee(1,"coder")
12+
13+
# Deleting the property of object
14+
# del emp.id
15+
# Deleting the object itself
16+
emp.display()
17+
18+
19+
#del emp
20+
#emp.display() #it will gives error after del emp
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Exercise: Class and Objects
2+
3+
1. Create a sample class named Employee with two attributes id and name
4+
5+
6+
```
7+
employee :
8+
id
9+
name
10+
11+
-> such that object initializes id and name dynamically for every employees
12+
```
13+
14+
2. Use del property on attributes as well as class objects
15+
16+
```
17+
emp = Employee(1,"coder")
18+
19+
use : del emp.id
20+
use : del emp
21+
```
22+
23+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/16_class_and_objects/16_class_and_objects.py)
24+
25+
26+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Father:
2+
def __init__(self, name, lastname):
3+
self.name = name
4+
self.lastname = lastname
5+
6+
def printname(self):
7+
print(self.name, self.lastname)
8+
9+
class Son(Father):
10+
def __init__(self, name, lastname):
11+
super().__init__(name, lastname)
12+
13+
x = Son("Darshan", "Beladiya")
14+
x.printname()
15+
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 father son relation on printname.
4+
5+
6+
```
7+
for example,
8+
father and son both has name so create a method for printname by passing firstname and lastname
9+
```
10+
11+
2. use super() constructor for calling parent constructor.
12+
13+
```
14+
class father:
15+
#code
16+
17+
class son(father):
18+
super()-it refers father class,now you can call father'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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Exercise: Multiple Inheritance
2+
3+
Real Life Example :
4+
1. Create multiple inheritance in mario game
5+
6+
7+
```
8+
Q. if we have created Mario Game with only move module and now we wants to add jump as well as eat then what???
9+
10+
Ans : just make subclass from mario and add extra methods so that we will be having expanded class.
11+
```
12+
13+
14+
15+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/18_multiple_inheritance/18_multiple_inheritance.py)
16+
17+
18+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class superMario():
2+
def move(self):
3+
print('I am moving')
4+
5+
6+
class jump():
7+
def jump_above(self):
8+
print('I just jumped')
9+
10+
class mushroom():
11+
def eat_mushroom(self):
12+
print('I have become big now')
13+
14+
15+
class Mario(superMario, mushroom, jump):
16+
pass
17+
play = Mario()
18+
play.move()
19+
play.eat_mushroom()
20+
play.jump_above()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Exercise: Raise Exception And Finally
2+
3+
1. Create Any Custom Exception and raise it.
4+
5+
```
6+
for creating custom exception just create a subClass of Exception.
7+
```
8+
9+
2.now raise that custom Exception
10+
11+
```
12+
let us say,
13+
14+
if age>18
15+
he is major
16+
else
17+
raise exception
18+
19+
create cusomException named ismajor and raise it if age<18.
20+
```
21+
22+
23+
24+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py)
25+
26+
27+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# for making exception just make subclass of Exception
2+
class isMajor(Exception):
3+
pass
4+
5+
def check(age):
6+
if int(age) < 18:
7+
raise isMajor
8+
else:
9+
print('Age: '+str(age))
10+
11+
#don't raise
12+
check(23)
13+
#raises an Exception
14+
check(17)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Exercise: Iterators
2+
3+
1. create iterator for fibonacci series.
4+
5+
6+
```
7+
Explanation :
8+
iterator must be initialized such that each of next returns next fibonacci number
9+
10+
```
11+
12+
13+
14+
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/20_Iterators/20_Iterators.py)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class fibonacci:
2+
def __init__(self):
3+
# default constructor
4+
self.previous = 0
5+
self.current = 1
6+
self.n = 1
7+
8+
def __iter__(self):
9+
return self
10+
11+
def __next__(self):
12+
if self.n < 5:
13+
result = self.previous + self.current
14+
self.previous = self.current
15+
self.current = result
16+
self.n += 1
17+
return result
18+
else:
19+
raise StopIteration
20+
21+
# init the fib_iterator
22+
fib_iterator = iter(fibonacci())
23+
while True:
24+
# print the value of next fibonaccinacci up to 5th fibonaccinacci
25+
try:
26+
print(next(fib_iterator))
27+
except StopIteration:
28+
break
29+
30+

0 commit comments

Comments
 (0)