Skip to content

Commit

Permalink
Added : exercises for 16 to 26
Browse files Browse the repository at this point in the history
  • Loading branch information
d_p_beladiya committed Jan 20, 2021
1 parent 2123111 commit f2a8f36
Show file tree
Hide file tree
Showing 22 changed files with 408 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Basics/python_basics/16_class_and_objects/16_class_and_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Employee:

def __init__(self,id,name):
self.id=id
self.name=name

def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))

# Creating a emp instance of Employee class
emp = Employee(1,"coder")

# Deleting the property of object
# del emp.id
# Deleting the object itself
emp.display()


#del emp
#emp.display() #it will gives error after del emp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Exercise: Class and Objects

1. Create a sample class named Employee with two attributes id and name


```
employee :
id
name
-> such that object initializes id and name dynamically for every employees
```

2. Use del property on attributes as well as class objects

```
emp = Employee(1,"coder")
use : del emp.id
use : del emp
```

[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/16_class_and_objects/16_class_and_objects.py)



15 changes: 15 additions & 0 deletions Basics/python_basics/17_inheritance/inheritace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Father:
def __init__(self, name, lastname):
self.name = name
self.lastname = lastname

def printname(self):
print(self.name, self.lastname)

class Son(Father):
def __init__(self, name, lastname):
super().__init__(name, lastname)

x = Son("Darshan", "Beladiya")
x.printname()

24 changes: 24 additions & 0 deletions Basics/python_basics/17_inheritance/inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Exercise: Inheritance

1. create inheritance using father son relation on printname.


```
for example,
father and son both has name so create a method for printname by passing firstname and lastname
```

2. use super() constructor for calling parent constructor.

```
class father:
#code
class son(father):
super()-it refers father class,now you can call father's methods.
```

[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/17_inheritance/17_inheritance.py)



Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Exercise: Multiple Inheritance

Real Life Example :
1. Create multiple inheritance in mario game


```
Q. if we have created Mario Game with only move module and now we wants to add jump as well as eat then what???
Ans : just make subclass from mario and add extra methods so that we will be having expanded class.
```



[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/18_multiple_inheritance/18_multiple_inheritance.py)



Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class superMario():
def move(self):
print('I am moving')


class jump():
def jump_above(self):
print('I just jumped')

class mushroom():
def eat_mushroom(self):
print('I have become big now')


class Mario(superMario, mushroom, jump):
pass
play = Mario()
play.move()
play.eat_mushroom()
play.jump_above()
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Exercise: Raise Exception And Finally

1. Create Any Custom Exception and raise it.

```
for creating custom exception just create a subClass of Exception.
```

2.now raise that custom Exception

```
let us say,
if age>18
he is major
else
raise exception
create cusomException named ismajor and raise it if age<18.
```



[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py)



Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# for making exception just make subclass of Exception
class isMajor(Exception):
pass

def check(age):
if int(age) < 18:
raise isMajor
else:
print('Age: '+str(age))

#don't raise
check(23)
#raises an Exception
check(17)
14 changes: 14 additions & 0 deletions Basics/python_basics/20_Iterators/20_Iterators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Exercise: Iterators

1. create iterator for fibonacci series.


```
Explanation :
iterator must be initialized such that each of next returns next fibonacci number
```



[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/20_Iterators/20_Iterators.py)
30 changes: 30 additions & 0 deletions Basics/python_basics/20_Iterators/20_Iterators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class fibonacci:
def __init__(self):
# default constructor
self.previous = 0
self.current = 1
self.n = 1

def __iter__(self):
return self

def __next__(self):
if self.n < 5:
result = self.previous + self.current
self.previous = self.current
self.current = result
self.n += 1
return result
else:
raise StopIteration

# init the fib_iterator
fib_iterator = iter(fibonacci())
while True:
# print the value of next fibonaccinacci up to 5th fibonaccinacci
try:
print(next(fib_iterator))
except StopIteration:
break


16 changes: 16 additions & 0 deletions Basics/python_basics/21_genrators/21_genrators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Exercise: Generators

1. Print Square Sequence using yield


```
Create Generator method such that every time it will returns a next square number
for exmaple : 1 4 9 16 ..
```



[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/21_genrators/21_genrators.py)
9 changes: 9 additions & 0 deletions Basics/python_basics/21_genrators/21_genrators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def next_square():
i=1
while True:
yield i*i
i+=1
for n in next_square():
if n>25:
break
print(n)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Exercise: Generators


1. list1=create a list of integers
list2=create a list of strings of binary values of list1

```
example :
num=[0,1,2,3,4]
binary=["0","1","10","11","100"]
```

2. Create a Dictionary of Binary values mapping with integers using Zip


```
example :
dict={0:"0",1:"1",2:"10" ......}
```



[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
num=[0,1,2,3,4]
binary=["0","1","10","11","100"]

z=zip(num,binary)
ans={num:binary for num,binary in z}
print(ans)
20 changes: 20 additions & 0 deletions Basics/python_basics/23_sets_frozensets/23_sets_frozensets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Exercise: Sets and Frozen Sets


1. create any set anf try to use frozenset(setname)


2. Find the elements in a given set that are not in another set


```
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
diffrence between set1 and set2 is {1,2,3}
```



[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py)
13 changes: 13 additions & 0 deletions Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print("Original sets:")
print(set1)
print(set2)
print("Difference of set1 and set2 using difference():")
print(set1.difference(set2))
print("Difference of set2 and set1 using difference():")
print(set2.difference(set1))
print("Difference of set1 and set2 using - operator:")
print(set1-set2)
print("Difference of set2 and set1 using - operator:")
print(set2-set1)
21 changes: 21 additions & 0 deletions Basics/python_basics/24_argparse/24_argparse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Exercise: Commandline Argument Processing using argparse

1. Take subject marks as command line arguments

```
example:
python3 cmd.py --physics 60 --chemistry 70 --maths 90
```

2. find PCM(physics chemistry maths) Merit(Average) using command line input of marks

```
-take input
-do sum of marks
-take average
```



[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/24_argparse/24_argparse.py)
20 changes: 20 additions & 0 deletions Basics/python_basics/24_argparse/24_argparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import argparse

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--physics", help="physics marks")
parser.add_argument("--chemistry", help="chemistry marks")
parser.add_argument("--maths", help="maths marks")

args = parser.parse_args()

print(args.physics)
print(args.chemistry)
print(args.maths)



print("Result:",(int(args.physics)+int(args.chemistry)+int(args.maths))/3)


#python3 cmd.py --physics 60 --chemistry 70 --maths 90
22 changes: 22 additions & 0 deletions Basics/python_basics/25_decorators/25_decorators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Exercise: Decorators

1. Create decorator function to check that the argument passed to the function factorial is a positive integer:

```
example:
factorial(-1) : raise Exception or print error message
```


2. Also check that number is integer or not
```
example:
factorial(1.354) : raise Exception or print error message
```
[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basicsHindi/25_decorators/25_decorators.py)
Loading

0 comments on commit f2a8f36

Please sign in to comment.