Skip to content

Commit

Permalink
Merge pull request codebasics#92 from beladiyadarshan/exercise_16_26
Browse files Browse the repository at this point in the history
Added : exercises for 16 to 26
  • Loading branch information
groverkds authored Feb 4, 2021
2 parents e26c3fe + a841369 commit bb15f2f
Show file tree
Hide file tree
Showing 23 changed files with 459 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
**/.idea/
.ipynb_checkpoints/
**/.ipynb_checkpoints/
**/.cache/
**/.cache/
.vscode
22 changes: 22 additions & 0 deletions Basics/Hindi/16_class_and_objects/16_class_and_object_exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Exercise: Class and Objects

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

```
employee :
id
name
```
object initializes id and name dynamically for every Employee object created.

```
emp = Employee(1, "coder")
```

2. Use del property to first delete id attribute and then the entire object


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



27 changes: 27 additions & 0 deletions Basics/Hindi/16_class_and_objects/16_class_and_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Employee:

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

def display(self):
print(f"ID: {self.id} \nName: {self.name}")


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

emp.display()
# Deleting the property of object
del emp.id
# Deleting the object itself
try:
print(emp.id)
except NameError:
print("emp.id is not defined")

del emp
try:
emp.display() # it will gives error after deleting emp
except NameError:
print("emp is not defined")
24 changes: 24 additions & 0 deletions Basics/Hindi/17_inheritance/17_inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Exercise: Inheritance

1. create inheritance using animal Dog relation.


```
for example,
Animal and Dog both has same habitat so create a method for habitat
```

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

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

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



22 changes: 22 additions & 0 deletions Basics/Hindi/17_inheritance/17_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Animal:
def __init__(self, habitat):
self.habitat = habitat

def print_habitat(self):
print(self.habitat)

def sound(self):
print("Some Animal Sound")


class Dog(Animal):
def __init__(self):
super().__init__("Kennel")

def sound(self):
print("Woof woof!")


x = Dog()
x.print_habitat()
x.sound()
26 changes: 26 additions & 0 deletions Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Exercise: Multiple Inheritance

Real Life Example :
1. Create multiple inheritance on teacher,student and youtuber.


```
Q. if we have created teacher and now one student joins master degree with becoming teacher then what??
Ans : just make subclass from teacher so that student will become teacher
```

2. Now student is teacher as well as youtuber then what???


```
-just use multiple inheritance for these three relations
```



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



23 changes: 23 additions & 0 deletions Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Teacher:
def teachers_action(self):
print("I can teach")


class Engineer:
def Engineers_action(self):
print("I can code")


class Youtuber:
def youtubers_action(self):
print("I can code and teach")


class Person(Teacher, Engineer, Youtuber):
pass


coder = Person()
coder.teachers_action()
coder.Engineers_action()
coder.youtubers_action()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Exercise: Raise Exception And Finally

1. Create a custom exception AdultException.

2. Create a class Person with attributes name and age in it.

3. Create a function get_minor_age() in the class. It throws an exception if the person is adult otherwise returns age.

4. Create a function display_person() which prints the age and name of a person.
```
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,30 @@
# for making exception just make subclass of Exception
class AdultException(Exception):
pass


class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def get_minor_age(self):
if int(self.age) >= 18:
raise AdultException
else:
return self.age

def display(self):
try:
print(f"age -> {self.get_minor_age()}")
except AdultException:
print("Person is an adult")
finally:
print(f"name -> {self.name}")


# No exception
Person("Bhavin", 17).display()

# AdultException is raised
Person("Dhaval", 23).display()
8 changes: 8 additions & 0 deletions Basics/Hindi/20_Iterators/20_Iterators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Exercise: Iterators

1. Create an iterator for fibonacci series in such a way that each next returns the next element from fibonacci series.
2. The iterator should stop when it reaches a `limit` defined in the constructor.



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

def __iter__(self):
return self

def __next__(self):
if self.n < self.limit:
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(5))
while True:
# print the value of next fibonacci up to 5th fibonacci
try:
print(next(fib_iterator))
except StopIteration:
break
16 changes: 16 additions & 0 deletions Basics/Hindi/21_generators/21_generators.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)
11 changes: 11 additions & 0 deletions Basics/Hindi/21_generators/21_generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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,33 @@
## Exercise: Generators


1. Create a Dictionary which contains the Binary values mapping with numbers found in the below integer and binary and save it in binary_dict.

Example :
```
integer = [0, 1, 2, 3, 4]
binary = ["0", "1", "10", "11", "100"]
binary_dict = {0:"0", 1:"1", 2:"10", 3: "11", 4:"100"}
```

2. Create a List which contains additive inverse of a given integer list.
An additive inverse `a` for an integer `i` is a number such that:
```
a + i = 0
```
Example:
```
integer = [1, -1, 2, 3, 5, 0, -7]
additive_inverse = [-1, 1, -2, -3, -5, 0, 7]
```

3. Create a set which only contains unique sqaures from a given a integer list.
```
integer = [1, -1, 2, -2, 3, -3]
sq_set = {1, 4, 9}
```




[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,18 @@
# Dictionary
integer = [0, 1, 2, 3, 4]
binary = ["0", "1", "10", "11", "100"]

z = zip(integer, binary)
binary_dict = {integer: binary for integer, binary in z}

print(binary_dict)

# List
integer = [1, -1, 2, 3, 5, 0, -7]
additive_inverse = [-1*i for i in integer]
print(additive_inverse)

# Set
integer = [1, -1, 2, -2, 3, -3]
sq_set = {i*i for i in integer}
print(sq_set)
20 changes: 20 additions & 0 deletions Basics/Hindi/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/Hindi/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)
13 changes: 13 additions & 0 deletions Basics/Hindi/24_argparse/24_argparse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## 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 average marks for the three subjects using command line input of marks.


[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/24_argparse/24_argparse.py)
Loading

0 comments on commit bb15f2f

Please sign in to comment.