From f2a8f36c527bd6e7e002f2cc1fd30d2e1b8bb48e Mon Sep 17 00:00:00 2001 From: d_p_beladiya Date: Thu, 21 Jan 2021 00:59:42 +0530 Subject: [PATCH 01/19] Added : exercises for 16 to 26 --- .../16_class_and_objects.py | 20 +++++++++++++ .../16_class_and_onject_exercise.md | 26 ++++++++++++++++ .../17_inheritance/inheritace.py | 15 ++++++++++ .../17_inheritance/inheritance.md | 24 +++++++++++++++ .../multiple_inheritance.md | 18 +++++++++++ .../multiple_inheritance.py | 20 +++++++++++++ .../19_raise_exception_finally.md | 27 +++++++++++++++++ .../19_raise_exception_finally.py | 14 +++++++++ .../20_Iterators/20_Iterators.md | 14 +++++++++ .../20_Iterators/20_Iterators.py | 30 +++++++++++++++++++ .../21_genrators/21_genrators.md | 16 ++++++++++ .../21_genrators/21_genrators.py | 9 ++++++ .../22_list_set_dict_comprehension.md | 24 +++++++++++++++ .../22_list_set_dict_comprehension.py | 6 ++++ .../23_sets_frozensets/23_sets_frozensets.md | 20 +++++++++++++ .../23_sets_frozensets/23_sets_frozensets.py | 13 ++++++++ .../python_basics/24_argparse/24_argparse.md | 21 +++++++++++++ .../python_basics/24_argparse/24_argparse.py | 20 +++++++++++++ .../25_decorators/25_decorators.md | 22 ++++++++++++++ .../25_decorators/25_decorators.py | 19 ++++++++++++ .../26_multithreading/26_multithreading.md | 16 ++++++++++ .../26_multithreading/26_multithreading.py | 14 +++++++++ 22 files changed, 408 insertions(+) create mode 100644 Basics/python_basics/16_class_and_objects/16_class_and_objects.py create mode 100644 Basics/python_basics/16_class_and_objects/16_class_and_onject_exercise.md create mode 100644 Basics/python_basics/17_inheritance/inheritace.py create mode 100644 Basics/python_basics/17_inheritance/inheritance.md create mode 100644 Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md create mode 100644 Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py create mode 100644 Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.md create mode 100644 Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py create mode 100644 Basics/python_basics/20_Iterators/20_Iterators.md create mode 100644 Basics/python_basics/20_Iterators/20_Iterators.py create mode 100644 Basics/python_basics/21_genrators/21_genrators.md create mode 100644 Basics/python_basics/21_genrators/21_genrators.py create mode 100644 Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md create mode 100644 Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py create mode 100644 Basics/python_basics/23_sets_frozensets/23_sets_frozensets.md create mode 100644 Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py create mode 100644 Basics/python_basics/24_argparse/24_argparse.md create mode 100644 Basics/python_basics/24_argparse/24_argparse.py create mode 100644 Basics/python_basics/25_decorators/25_decorators.md create mode 100644 Basics/python_basics/25_decorators/25_decorators.py create mode 100644 Basics/python_basics/26_multithreading/26_multithreading.md create mode 100644 Basics/python_basics/26_multithreading/26_multithreading.py diff --git a/Basics/python_basics/16_class_and_objects/16_class_and_objects.py b/Basics/python_basics/16_class_and_objects/16_class_and_objects.py new file mode 100644 index 00000000..7c1120fa --- /dev/null +++ b/Basics/python_basics/16_class_and_objects/16_class_and_objects.py @@ -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 \ No newline at end of file diff --git a/Basics/python_basics/16_class_and_objects/16_class_and_onject_exercise.md b/Basics/python_basics/16_class_and_objects/16_class_and_onject_exercise.md new file mode 100644 index 00000000..52d5d054 --- /dev/null +++ b/Basics/python_basics/16_class_and_objects/16_class_and_onject_exercise.md @@ -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) + + + diff --git a/Basics/python_basics/17_inheritance/inheritace.py b/Basics/python_basics/17_inheritance/inheritace.py new file mode 100644 index 00000000..6eece8db --- /dev/null +++ b/Basics/python_basics/17_inheritance/inheritace.py @@ -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() + diff --git a/Basics/python_basics/17_inheritance/inheritance.md b/Basics/python_basics/17_inheritance/inheritance.md new file mode 100644 index 00000000..79505d97 --- /dev/null +++ b/Basics/python_basics/17_inheritance/inheritance.md @@ -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) + + + diff --git a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md new file mode 100644 index 00000000..b2db45c1 --- /dev/null +++ b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md @@ -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) + + + diff --git a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py new file mode 100644 index 00000000..5e16308c --- /dev/null +++ b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py @@ -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() \ No newline at end of file diff --git a/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.md b/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.md new file mode 100644 index 00000000..78a4908d --- /dev/null +++ b/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.md @@ -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) + + + diff --git a/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py b/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py new file mode 100644 index 00000000..f1b8507c --- /dev/null +++ b/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py @@ -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) \ No newline at end of file diff --git a/Basics/python_basics/20_Iterators/20_Iterators.md b/Basics/python_basics/20_Iterators/20_Iterators.md new file mode 100644 index 00000000..2bf2bab0 --- /dev/null +++ b/Basics/python_basics/20_Iterators/20_Iterators.md @@ -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) diff --git a/Basics/python_basics/20_Iterators/20_Iterators.py b/Basics/python_basics/20_Iterators/20_Iterators.py new file mode 100644 index 00000000..4a2f21ba --- /dev/null +++ b/Basics/python_basics/20_Iterators/20_Iterators.py @@ -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 + + diff --git a/Basics/python_basics/21_genrators/21_genrators.md b/Basics/python_basics/21_genrators/21_genrators.md new file mode 100644 index 00000000..471441eb --- /dev/null +++ b/Basics/python_basics/21_genrators/21_genrators.md @@ -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) diff --git a/Basics/python_basics/21_genrators/21_genrators.py b/Basics/python_basics/21_genrators/21_genrators.py new file mode 100644 index 00000000..3fb02201 --- /dev/null +++ b/Basics/python_basics/21_genrators/21_genrators.py @@ -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) \ No newline at end of file diff --git a/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md b/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md new file mode 100644 index 00000000..c82fd48f --- /dev/null +++ b/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md @@ -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) diff --git a/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py b/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py new file mode 100644 index 00000000..718d7e7e --- /dev/null +++ b/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py @@ -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) \ No newline at end of file diff --git a/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.md b/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.md new file mode 100644 index 00000000..b711c4d9 --- /dev/null +++ b/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.md @@ -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) diff --git a/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py b/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py new file mode 100644 index 00000000..e3dd51b1 --- /dev/null +++ b/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py @@ -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) \ No newline at end of file diff --git a/Basics/python_basics/24_argparse/24_argparse.md b/Basics/python_basics/24_argparse/24_argparse.md new file mode 100644 index 00000000..9fed9a12 --- /dev/null +++ b/Basics/python_basics/24_argparse/24_argparse.md @@ -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) diff --git a/Basics/python_basics/24_argparse/24_argparse.py b/Basics/python_basics/24_argparse/24_argparse.py new file mode 100644 index 00000000..fe0c2d92 --- /dev/null +++ b/Basics/python_basics/24_argparse/24_argparse.py @@ -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 diff --git a/Basics/python_basics/25_decorators/25_decorators.md b/Basics/python_basics/25_decorators/25_decorators.md new file mode 100644 index 00000000..37bb35fe --- /dev/null +++ b/Basics/python_basics/25_decorators/25_decorators.md @@ -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) diff --git a/Basics/python_basics/25_decorators/25_decorators.py b/Basics/python_basics/25_decorators/25_decorators.py new file mode 100644 index 00000000..f0482093 --- /dev/null +++ b/Basics/python_basics/25_decorators/25_decorators.py @@ -0,0 +1,19 @@ +def check(f): + def helper(x): + if type(x) == int and x > 0: + return f(x) + else: + raise Exception("Argument is not an integer") + return helper + +@check +def factorial(n): + if n == 1: + return 1 + else: + return n * factorial(n-1) + +for i in range(1,10): + print(i, factorial(i)) + +print(factorial(-1)) \ No newline at end of file diff --git a/Basics/python_basics/26_multithreading/26_multithreading.md b/Basics/python_basics/26_multithreading/26_multithreading.md new file mode 100644 index 00000000..0777e663 --- /dev/null +++ b/Basics/python_basics/26_multithreading/26_multithreading.md @@ -0,0 +1,16 @@ +## Exercise: Multithreading + +1. Create any multithreaded code using for loop for creating multithreads + +``` +for i in range(10): + th = Thread(target=func_name, args=(i, )) + +``` + + +2. print total active threads in multithreaded code using threading.active_count() + + + +[Solution](https://github.com/codebasics/py/blob/master/Basics/python_basics/26_multithreading/26_multithreading.py) diff --git a/Basics/python_basics/26_multithreading/26_multithreading.py b/Basics/python_basics/26_multithreading/26_multithreading.py new file mode 100644 index 00000000..601a4e3e --- /dev/null +++ b/Basics/python_basics/26_multithreading/26_multithreading.py @@ -0,0 +1,14 @@ +import time +import threading +from threading import Thread + +def sleepMe(i): + print("Thread %i will sleep." % i) + time.sleep(5) + print("Thread %i is awake" % i) + +for i in range(10): + th = Thread(target=sleepMe, args=(i, )) + th.start() + print("Current Threads: %i." % threading.active_count()) + From 75596dd0341484fd7fd329effd616514a71b380c Mon Sep 17 00:00:00 2001 From: Karandeep Singh Grover <30365139+groverkds@users.noreply.github.com> Date: Mon, 25 Jan 2021 14:18:38 +0530 Subject: [PATCH 02/19] Rename 16_class_and_onject_exercise.md to 16_class_and_object_exercise.md --- ...ass_and_onject_exercise.md => 16_class_and_object_exercise.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Basics/python_basics/16_class_and_objects/{16_class_and_onject_exercise.md => 16_class_and_object_exercise.md} (100%) diff --git a/Basics/python_basics/16_class_and_objects/16_class_and_onject_exercise.md b/Basics/python_basics/16_class_and_objects/16_class_and_object_exercise.md similarity index 100% rename from Basics/python_basics/16_class_and_objects/16_class_and_onject_exercise.md rename to Basics/python_basics/16_class_and_objects/16_class_and_object_exercise.md From fccd97ead6ee9aa53a9d6c9bec3b9f0f34ce29e6 Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 16:19:00 +0530 Subject: [PATCH 03/19] fixed issues with 16 --- .../16_class_and_object_exercise.md | 22 +++++++++++++++ .../16_class_and_objects.py | 27 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Basics/Hindi/16_class_and_objects/16_class_and_object_exercise.md create mode 100644 Basics/Hindi/16_class_and_objects/16_class_and_objects.py diff --git a/Basics/Hindi/16_class_and_objects/16_class_and_object_exercise.md b/Basics/Hindi/16_class_and_objects/16_class_and_object_exercise.md new file mode 100644 index 00000000..cf3883c4 --- /dev/null +++ b/Basics/Hindi/16_class_and_objects/16_class_and_object_exercise.md @@ -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) + + + diff --git a/Basics/Hindi/16_class_and_objects/16_class_and_objects.py b/Basics/Hindi/16_class_and_objects/16_class_and_objects.py new file mode 100644 index 00000000..4893cabc --- /dev/null +++ b/Basics/Hindi/16_class_and_objects/16_class_and_objects.py @@ -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") \ No newline at end of file From 9e593c98319b5094cfcdc02acd6f908e9b145b6b Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 16:22:22 +0530 Subject: [PATCH 04/19] changed - gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ff6d83fa..309c6a5a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ **/.idea/ .ipynb_checkpoints/ **/.ipynb_checkpoints/ -**/.cache/ \ No newline at end of file +**/.cache/ +.vscode \ No newline at end of file From 77c935591ac8d60654ee766e25135c8b0291a944 Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 16:22:42 +0530 Subject: [PATCH 05/19] moved folder --- .../17_inheritance/inheritace.py | 0 .../17_inheritance/inheritance.md | 0 .../multiple_inheritance.md | 0 .../multiple_inheritance.py | 0 .../19_raise_exception_finally.md | 0 .../19_raise_exception_finally.py | 0 .../20_Iterators/20_Iterators.md | 0 .../20_Iterators/20_Iterators.py | 0 .../21_genrators/21_genrators.md | 0 .../21_genrators/21_genrators.py | 0 .../22_list_set_dict_comprehension.md | 0 .../22_list_set_dict_comprehension.py | 0 .../23_sets_frozensets/23_sets_frozensets.md | 0 .../23_sets_frozensets/23_sets_frozensets.py | 0 .../24_argparse/24_argparse.md | 0 .../24_argparse/24_argparse.py | 0 .../25_decorators/25_decorators.md | 0 .../25_decorators/25_decorators.py | 0 .../26_multithreading/26_multithreading.md | 0 .../26_multithreading/26_multithreading.py | 0 .../16_class_and_object_exercise.md | 26 ------------------- .../16_class_and_objects.py | 20 -------------- 22 files changed, 46 deletions(-) rename Basics/{python_basics => Hindi}/17_inheritance/inheritace.py (100%) rename Basics/{python_basics => Hindi}/17_inheritance/inheritance.md (100%) rename Basics/{python_basics => Hindi}/18_multiple_inheritance/multiple_inheritance.md (100%) rename Basics/{python_basics => Hindi}/18_multiple_inheritance/multiple_inheritance.py (100%) rename Basics/{python_basics => Hindi}/19_raise_exception_finally/19_raise_exception_finally.md (100%) rename Basics/{python_basics => Hindi}/19_raise_exception_finally/19_raise_exception_finally.py (100%) rename Basics/{python_basics => Hindi}/20_Iterators/20_Iterators.md (100%) rename Basics/{python_basics => Hindi}/20_Iterators/20_Iterators.py (100%) rename Basics/{python_basics => Hindi}/21_genrators/21_genrators.md (100%) rename Basics/{python_basics => Hindi}/21_genrators/21_genrators.py (100%) rename Basics/{python_basics => Hindi}/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md (100%) rename Basics/{python_basics => Hindi}/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py (100%) rename Basics/{python_basics => Hindi}/23_sets_frozensets/23_sets_frozensets.md (100%) rename Basics/{python_basics => Hindi}/23_sets_frozensets/23_sets_frozensets.py (100%) rename Basics/{python_basics => Hindi}/24_argparse/24_argparse.md (100%) rename Basics/{python_basics => Hindi}/24_argparse/24_argparse.py (100%) rename Basics/{python_basics => Hindi}/25_decorators/25_decorators.md (100%) rename Basics/{python_basics => Hindi}/25_decorators/25_decorators.py (100%) rename Basics/{python_basics => Hindi}/26_multithreading/26_multithreading.md (100%) rename Basics/{python_basics => Hindi}/26_multithreading/26_multithreading.py (100%) delete mode 100644 Basics/python_basics/16_class_and_objects/16_class_and_object_exercise.md delete mode 100644 Basics/python_basics/16_class_and_objects/16_class_and_objects.py diff --git a/Basics/python_basics/17_inheritance/inheritace.py b/Basics/Hindi/17_inheritance/inheritace.py similarity index 100% rename from Basics/python_basics/17_inheritance/inheritace.py rename to Basics/Hindi/17_inheritance/inheritace.py diff --git a/Basics/python_basics/17_inheritance/inheritance.md b/Basics/Hindi/17_inheritance/inheritance.md similarity index 100% rename from Basics/python_basics/17_inheritance/inheritance.md rename to Basics/Hindi/17_inheritance/inheritance.md diff --git a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md b/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.md similarity index 100% rename from Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md rename to Basics/Hindi/18_multiple_inheritance/multiple_inheritance.md diff --git a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py b/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py similarity index 100% rename from Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py rename to Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py diff --git a/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.md b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.md similarity index 100% rename from Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.md rename to Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.md diff --git a/Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.py similarity index 100% rename from Basics/python_basics/19_raise_exception_finally/19_raise_exception_finally.py rename to Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.py diff --git a/Basics/python_basics/20_Iterators/20_Iterators.md b/Basics/Hindi/20_Iterators/20_Iterators.md similarity index 100% rename from Basics/python_basics/20_Iterators/20_Iterators.md rename to Basics/Hindi/20_Iterators/20_Iterators.md diff --git a/Basics/python_basics/20_Iterators/20_Iterators.py b/Basics/Hindi/20_Iterators/20_Iterators.py similarity index 100% rename from Basics/python_basics/20_Iterators/20_Iterators.py rename to Basics/Hindi/20_Iterators/20_Iterators.py diff --git a/Basics/python_basics/21_genrators/21_genrators.md b/Basics/Hindi/21_genrators/21_genrators.md similarity index 100% rename from Basics/python_basics/21_genrators/21_genrators.md rename to Basics/Hindi/21_genrators/21_genrators.md diff --git a/Basics/python_basics/21_genrators/21_genrators.py b/Basics/Hindi/21_genrators/21_genrators.py similarity index 100% rename from Basics/python_basics/21_genrators/21_genrators.py rename to Basics/Hindi/21_genrators/21_genrators.py diff --git a/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md similarity index 100% rename from Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md rename to Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md diff --git a/Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py similarity index 100% rename from Basics/python_basics/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py rename to Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py diff --git a/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.md b/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.md similarity index 100% rename from Basics/python_basics/23_sets_frozensets/23_sets_frozensets.md rename to Basics/Hindi/23_sets_frozensets/23_sets_frozensets.md diff --git a/Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py b/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py similarity index 100% rename from Basics/python_basics/23_sets_frozensets/23_sets_frozensets.py rename to Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py diff --git a/Basics/python_basics/24_argparse/24_argparse.md b/Basics/Hindi/24_argparse/24_argparse.md similarity index 100% rename from Basics/python_basics/24_argparse/24_argparse.md rename to Basics/Hindi/24_argparse/24_argparse.md diff --git a/Basics/python_basics/24_argparse/24_argparse.py b/Basics/Hindi/24_argparse/24_argparse.py similarity index 100% rename from Basics/python_basics/24_argparse/24_argparse.py rename to Basics/Hindi/24_argparse/24_argparse.py diff --git a/Basics/python_basics/25_decorators/25_decorators.md b/Basics/Hindi/25_decorators/25_decorators.md similarity index 100% rename from Basics/python_basics/25_decorators/25_decorators.md rename to Basics/Hindi/25_decorators/25_decorators.md diff --git a/Basics/python_basics/25_decorators/25_decorators.py b/Basics/Hindi/25_decorators/25_decorators.py similarity index 100% rename from Basics/python_basics/25_decorators/25_decorators.py rename to Basics/Hindi/25_decorators/25_decorators.py diff --git a/Basics/python_basics/26_multithreading/26_multithreading.md b/Basics/Hindi/26_multithreading/26_multithreading.md similarity index 100% rename from Basics/python_basics/26_multithreading/26_multithreading.md rename to Basics/Hindi/26_multithreading/26_multithreading.md diff --git a/Basics/python_basics/26_multithreading/26_multithreading.py b/Basics/Hindi/26_multithreading/26_multithreading.py similarity index 100% rename from Basics/python_basics/26_multithreading/26_multithreading.py rename to Basics/Hindi/26_multithreading/26_multithreading.py diff --git a/Basics/python_basics/16_class_and_objects/16_class_and_object_exercise.md b/Basics/python_basics/16_class_and_objects/16_class_and_object_exercise.md deleted file mode 100644 index 52d5d054..00000000 --- a/Basics/python_basics/16_class_and_objects/16_class_and_object_exercise.md +++ /dev/null @@ -1,26 +0,0 @@ -## 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) - - - diff --git a/Basics/python_basics/16_class_and_objects/16_class_and_objects.py b/Basics/python_basics/16_class_and_objects/16_class_and_objects.py deleted file mode 100644 index 7c1120fa..00000000 --- a/Basics/python_basics/16_class_and_objects/16_class_and_objects.py +++ /dev/null @@ -1,20 +0,0 @@ -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 \ No newline at end of file From 21c8ae92382a83c57cf774d56a1bc45ef3b0cb72 Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 16:51:27 +0530 Subject: [PATCH 06/19] changed - changed exercise --- .../19_raise_exception_finally.md | 9 ++-- .../19_raise_exception_finally.py | 42 +++++++++++++------ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.md b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.md index 78a4908d..da42cfea 100644 --- a/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.md +++ b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.md @@ -1,13 +1,12 @@ ## Exercise: Raise Exception And Finally -1. Create Any Custom Exception and raise it. +1. Create a custom exception AdultException. -``` - for creating custom exception just create a subClass of Exception. -``` +2. Create a class Person with attributes name and age in it. -2.now raise that custom Exception +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, diff --git a/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.py b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.py index f1b8507c..317047d2 100644 --- a/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.py +++ b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.py @@ -1,14 +1,30 @@ # 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) \ No newline at end of file +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() From 3648cf2ae8b24d27d608d4892324efedc3472229 Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 16:57:36 +0530 Subject: [PATCH 07/19] added - limit the stopping condition for iterator --- Basics/Hindi/20_Iterators/20_Iterators.md | 10 ++-------- Basics/Hindi/20_Iterators/20_Iterators.py | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/Basics/Hindi/20_Iterators/20_Iterators.md b/Basics/Hindi/20_Iterators/20_Iterators.md index 2bf2bab0..100ca74f 100644 --- a/Basics/Hindi/20_Iterators/20_Iterators.md +++ b/Basics/Hindi/20_Iterators/20_Iterators.md @@ -1,13 +1,7 @@ ## Exercise: Iterators -1. create iterator for fibonacci series. - - -``` -Explanation : - iterator must be initialized such that each of next returns next fibonacci number - -``` +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. diff --git a/Basics/Hindi/20_Iterators/20_Iterators.py b/Basics/Hindi/20_Iterators/20_Iterators.py index 4a2f21ba..5caa6a57 100644 --- a/Basics/Hindi/20_Iterators/20_Iterators.py +++ b/Basics/Hindi/20_Iterators/20_Iterators.py @@ -1,30 +1,30 @@ -class fibonacci: - def __init__(self): +class Fibonacci: + def __init__(self, limit): # default constructor self.previous = 0 self.current = 1 self.n = 1 + self.limit = limit - def __iter__(self): + def __iter__(self): return self - def __next__(self): - if self.n < 5: + 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 + raise StopIteration + # init the fib_iterator -fib_iterator = iter(fibonacci()) +fib_iterator = iter(Fibonacci(5)) while True: - # print the value of next fibonaccinacci up to 5th fibonaccinacci + # print the value of next fibonacci up to 5th fibonacci try: print(next(fib_iterator)) except StopIteration: - break - - + break From 8d2e7c988dfbc7f7c749b51ac1dc59f9767beb5b Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 17:19:19 +0530 Subject: [PATCH 08/19] added - list and set dicts --- .../22_list_set_dict_comprehension.md | 29 ++++++++++++------- .../22_list_set_dict_comprehension.py | 22 ++++++++++---- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md index c82fd48f..dbbebc37 100644 --- a/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md +++ b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md @@ -1,24 +1,33 @@ ## Exercise: Generators -1. list1=create a list of integers - list2=create a list of strings of binary values of list1 +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 : ``` - example : - num=[0,1,2,3,4] - binary=["0","1","10","11","100"] + 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 Dictionary of Binary values mapping with integers using Zip - +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. ``` - example : - dict={0:"0",1:"1",2:"10" ......} - +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) diff --git a/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py index 718d7e7e..7fcb3547 100644 --- a/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py +++ b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py @@ -1,6 +1,18 @@ -num=[0,1,2,3,4] -binary=["0","1","10","11","100"] +# Dictionary +integer = [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) \ No newline at end of file +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) From a6a67718fbfc0171d92751db90be5bcbfef3e8dc Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 17:20:21 +0530 Subject: [PATCH 09/19] renamed --- .../21_generators.md} | 0 Basics/Hindi/21_generators/21_generators.py | 11 +++++++++++ Basics/Hindi/21_genrators/21_genrators.py | 9 --------- 3 files changed, 11 insertions(+), 9 deletions(-) rename Basics/Hindi/{21_genrators/21_genrators.md => 21_generators/21_generators.md} (100%) create mode 100644 Basics/Hindi/21_generators/21_generators.py delete mode 100644 Basics/Hindi/21_genrators/21_genrators.py diff --git a/Basics/Hindi/21_genrators/21_genrators.md b/Basics/Hindi/21_generators/21_generators.md similarity index 100% rename from Basics/Hindi/21_genrators/21_genrators.md rename to Basics/Hindi/21_generators/21_generators.md diff --git a/Basics/Hindi/21_generators/21_generators.py b/Basics/Hindi/21_generators/21_generators.py new file mode 100644 index 00000000..699ec2b7 --- /dev/null +++ b/Basics/Hindi/21_generators/21_generators.py @@ -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) diff --git a/Basics/Hindi/21_genrators/21_genrators.py b/Basics/Hindi/21_genrators/21_genrators.py deleted file mode 100644 index 3fb02201..00000000 --- a/Basics/Hindi/21_genrators/21_genrators.py +++ /dev/null @@ -1,9 +0,0 @@ -def next_square(): - i=1 - while True: - yield i*i - i+=1 -for n in next_square(): - if n>25: - break - print(n) \ No newline at end of file From 65a6b504691351f7a4dac6844203fe0f2ef9547d Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 17:23:08 +0530 Subject: [PATCH 10/19] formatted --- Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py b/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py index e3dd51b1..9b7567e7 100644 --- a/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py +++ b/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py @@ -1,5 +1,5 @@ -set1 = {1,2,3,4,5} -set2 = {4,5,6,7,8} +set1 = {1, 2, 3, 4, 5} +set2 = {4, 5, 6, 7, 8} print("Original sets:") print(set1) print(set2) @@ -8,6 +8,6 @@ print("Difference of set2 and set1 using difference():") print(set2.difference(set1)) print("Difference of set1 and set2 using - operator:") -print(set1-set2) +print(set1 - set2) print("Difference of set2 and set1 using - operator:") -print(set2-set1) \ No newline at end of file +print(set2 - set1) From 87741e1292b00933d604236ad54f8b8c5a0c7502 Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 17:23:19 +0530 Subject: [PATCH 11/19] formatted --- Basics/Hindi/24_argparse/24_argparse.md | 10 +--------- Basics/Hindi/24_argparse/24_argparse.py | 9 ++++----- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Basics/Hindi/24_argparse/24_argparse.md b/Basics/Hindi/24_argparse/24_argparse.md index 9fed9a12..45b66c09 100644 --- a/Basics/Hindi/24_argparse/24_argparse.md +++ b/Basics/Hindi/24_argparse/24_argparse.md @@ -7,15 +7,7 @@ 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 - -``` - +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) diff --git a/Basics/Hindi/24_argparse/24_argparse.py b/Basics/Hindi/24_argparse/24_argparse.py index fe0c2d92..f35d7790 100644 --- a/Basics/Hindi/24_argparse/24_argparse.py +++ b/Basics/Hindi/24_argparse/24_argparse.py @@ -12,9 +12,8 @@ print(args.chemistry) print(args.maths) - + print("Result:", ( + int(args.physics) + int(args.chemistry) + int(args.maths) + ) / 3) - print("Result:",(int(args.physics)+int(args.chemistry)+int(args.maths))/3) - - - #python3 cmd.py --physics 60 --chemistry 70 --maths 90 + # python3 cmd.py --physics 60 --chemistry 70 --maths 90 From d3229fb68ccb78911140b767a438707346627520 Mon Sep 17 00:00:00 2001 From: d_p_beladiya Date: Mon, 25 Jan 2021 17:25:43 +0530 Subject: [PATCH 12/19] Edited: 17_18 --- .../17_inheritance/inheritace.py | 21 +++++++------- .../17_inheritance/inheritance.md | 10 +++---- .../multiple_inheritance.md | 14 ++++++++-- .../multiple_inheritance.py | 28 +++++++++---------- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Basics/python_basics/17_inheritance/inheritace.py b/Basics/python_basics/17_inheritance/inheritace.py index 6eece8db..a90123c8 100644 --- a/Basics/python_basics/17_inheritance/inheritace.py +++ b/Basics/python_basics/17_inheritance/inheritace.py @@ -1,15 +1,14 @@ -class Father: - def __init__(self, name, lastname): - self.name = name - self.lastname = lastname +class Animal: + def __init__(self, living_place): + self.living_place = living_place - def printname(self): - print(self.name, self.lastname) + def printplace(self): + print(self.living_place) -class Son(Father): - def __init__(self, name, lastname): - super().__init__(name, lastname) +class Dog(Animal): + def __init__(self, living_place): + super().__init__(living_place) -x = Son("Darshan", "Beladiya") -x.printname() +x = Dog("zoo") +x.printplace() diff --git a/Basics/python_basics/17_inheritance/inheritance.md b/Basics/python_basics/17_inheritance/inheritance.md index 79505d97..c8f2fd8c 100644 --- a/Basics/python_basics/17_inheritance/inheritance.md +++ b/Basics/python_basics/17_inheritance/inheritance.md @@ -1,21 +1,21 @@ ## Exercise: Inheritance -1. create inheritance using father son relation on printname. +1. create inheritance using animal dog relation. ``` for example, - father and son both has name so create a method for printname by passing firstname and lastname + animal and dog both has same living_place so create a method for living_place ``` 2. use super() constructor for calling parent constructor. ``` -class father: +class animal: #code -class son(father): - super()-it refers father class,now you can call father's methods. +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) diff --git a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md index b2db45c1..46f2c3ea 100644 --- a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md +++ b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.md @@ -1,13 +1,21 @@ ## Exercise: Multiple Inheritance Real Life Example : -1. Create multiple inheritance in mario game +1. Create multiple inheritance on teacher,student and youtuber. ``` -Q. if we have created Mario Game with only move module and now we wants to add jump as well as eat then what??? +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 -Ans : just make subclass from mario and add extra methods so that we will be having expanded class. ``` diff --git a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py index 5e16308c..5b7ac964 100644 --- a/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py +++ b/Basics/python_basics/18_multiple_inheritance/multiple_inheritance.py @@ -1,20 +1,20 @@ -class superMario(): - def move(self): - print('I am moving') +class Teacher(): + def teachers_action(self): + print('I can teach') -class jump(): - def jump_above(self): - print('I just jumped') +class Engineer(): + def Engineers_action(self): + print('I can code') -class mushroom(): - def eat_mushroom(self): - print('I have become big now') +class Youtuber(): + def youtubers_action(self): + print('I can code and teach') -class Mario(superMario, mushroom, jump): +class Person(Teacher, Engineer, Youtuber): pass -play = Mario() -play.move() -play.eat_mushroom() -play.jump_above() \ No newline at end of file +coder = Person() +coder.teachers_action() +coder.Engineers_action() +coder.youtubers_action() From fb9940b5b7978d9d74e88655a36d3408f3b0a982 Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 17:31:41 +0530 Subject: [PATCH 13/19] formatted --- Basics/Hindi/25_decorators/25_decorators.md | 16 +++++--------- Basics/Hindi/25_decorators/25_decorators.py | 23 +++++++++++++++------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Basics/Hindi/25_decorators/25_decorators.md b/Basics/Hindi/25_decorators/25_decorators.md index 37bb35fe..d11c387c 100644 --- a/Basics/Hindi/25_decorators/25_decorators.md +++ b/Basics/Hindi/25_decorators/25_decorators.md @@ -1,22 +1,16 @@ ## Exercise: Decorators -1. Create decorator function to check that the argument passed to the function factorial is a positive integer: +1. Create a decorator function to check that the argument passed to the function factorial is a non-negative integer: -``` -example: - - factorial(-1) : raise Exception or print error message +2. Create a factorial function which finds the factorial of a number. - -``` - - -2. Also check that number is integer or not +3. Use the decorator to decorate the factorial function to only allow factorial of non-negative integers. ``` example: factorial(1.354) : raise Exception or print error message + factorial(-1) : raise Exception or print error message + factorial(5) : 60 - ``` [Solution](https://github.com/codebasics/py/blob/master/Basics/python_basicsHindi/25_decorators/25_decorators.py) diff --git a/Basics/Hindi/25_decorators/25_decorators.py b/Basics/Hindi/25_decorators/25_decorators.py index f0482093..ad5dbe58 100644 --- a/Basics/Hindi/25_decorators/25_decorators.py +++ b/Basics/Hindi/25_decorators/25_decorators.py @@ -3,17 +3,28 @@ def helper(x): if type(x) == int and x > 0: return f(x) else: - raise Exception("Argument is not an integer") + raise Exception("Argument is not a non-negative integer") + return helper - + + @check def factorial(n): if n == 1: return 1 else: - return n * factorial(n-1) + return n * factorial(n - 1) + + +for i in range(1, 10): + print(i, factorial(i)) -for i in range(1,10): - print(i, factorial(i)) +try: + print(factorial(-1)) +except Exception as e: + e.print_exception() -print(factorial(-1)) \ No newline at end of file +try: + print(factorial(1.354)) +except Exception as e: + e.print_exception() \ No newline at end of file From 9b9a5897c09c85ba7b2a11935d55f805dbba5119 Mon Sep 17 00:00:00 2001 From: Karandeep Grover Date: Mon, 25 Jan 2021 17:38:13 +0530 Subject: [PATCH 14/19] formatted --- Basics/Hindi/17_inheritance/inheritace.py | 24 ++++++++++++------- Basics/Hindi/17_inheritance/inheritance.md | 10 ++++---- .../multiple_inheritance.py | 23 ++++++++++-------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/Basics/Hindi/17_inheritance/inheritace.py b/Basics/Hindi/17_inheritance/inheritace.py index a90123c8..d64efa1e 100644 --- a/Basics/Hindi/17_inheritance/inheritace.py +++ b/Basics/Hindi/17_inheritance/inheritace.py @@ -1,14 +1,22 @@ class Animal: - def __init__(self, living_place): - self.living_place = living_place + def __init__(self, habitat): + self.habitat = habitat + + def print_habitat(self): + print(self.habitat) + + def sound(self): + print("Some Animal Sound") - def printplace(self): - print(self.living_place) class Dog(Animal): - def __init__(self, living_place): - super().__init__(living_place) + def __init__(self): + super().__init__("Kennel") + + def sound(self): + print("Woof woof!") -x = Dog("zoo") -x.printplace() +x = Dog() +x.print_habitat() +x.sound() \ No newline at end of file diff --git a/Basics/Hindi/17_inheritance/inheritance.md b/Basics/Hindi/17_inheritance/inheritance.md index c8f2fd8c..925e0945 100644 --- a/Basics/Hindi/17_inheritance/inheritance.md +++ b/Basics/Hindi/17_inheritance/inheritance.md @@ -1,21 +1,21 @@ ## Exercise: Inheritance -1. create inheritance using animal dog relation. +1. create inheritance using animal Dog relation. ``` for example, - animal and dog both has same living_place so create a method for living_place + Animal and Dog both has same habitat so create a method for habitat ``` 2. use super() constructor for calling parent constructor. ``` -class animal: +class Animal: #code -class dog(animal): - super()-it refers animal class,now you can call animal's methods. +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) diff --git a/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py b/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py index 5b7ac964..57be8094 100644 --- a/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py +++ b/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py @@ -1,20 +1,23 @@ -class Teacher(): - def teachers_action(self): - print('I can teach') +class Teacher: + def teachers_action(self): + print("I can teach") -class Engineer(): +class Engineer: def Engineers_action(self): - print('I can code') + print("I can code") -class Youtuber(): + +class Youtuber: def youtubers_action(self): - print('I can code and teach') + print("I can code and teach") + + +class Person(Teacher, Engineer, Youtuber): + pass -class Person(Teacher, Engineer, Youtuber): - pass -coder = Person() +coder = Person() coder.teachers_action() coder.Engineers_action() coder.youtubers_action() From 2367fcbd76140895285a35f79fe4255e479d7eb9 Mon Sep 17 00:00:00 2001 From: Karandeep Singh Grover <30365139+groverkds@users.noreply.github.com> Date: Tue, 26 Jan 2021 09:58:28 +0530 Subject: [PATCH 15/19] Rename inheritace.py to inheritance.py --- Basics/Hindi/17_inheritance/{inheritace.py => inheritance.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Basics/Hindi/17_inheritance/{inheritace.py => inheritance.py} (97%) diff --git a/Basics/Hindi/17_inheritance/inheritace.py b/Basics/Hindi/17_inheritance/inheritance.py similarity index 97% rename from Basics/Hindi/17_inheritance/inheritace.py rename to Basics/Hindi/17_inheritance/inheritance.py index d64efa1e..b9c860db 100644 --- a/Basics/Hindi/17_inheritance/inheritace.py +++ b/Basics/Hindi/17_inheritance/inheritance.py @@ -19,4 +19,4 @@ def sound(self): x = Dog() x.print_habitat() -x.sound() \ No newline at end of file +x.sound() From 73e55beedfc871dfed4bdef3623692efdda20848 Mon Sep 17 00:00:00 2001 From: Karandeep Singh Grover <30365139+groverkds@users.noreply.github.com> Date: Tue, 26 Jan 2021 09:58:52 +0530 Subject: [PATCH 16/19] Rename inheritance.py to 17_inheritance.py --- Basics/Hindi/17_inheritance/{inheritance.py => 17_inheritance.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Basics/Hindi/17_inheritance/{inheritance.py => 17_inheritance.py} (100%) diff --git a/Basics/Hindi/17_inheritance/inheritance.py b/Basics/Hindi/17_inheritance/17_inheritance.py similarity index 100% rename from Basics/Hindi/17_inheritance/inheritance.py rename to Basics/Hindi/17_inheritance/17_inheritance.py From 4340f72dcfe6290024d959144bc955091ccd2871 Mon Sep 17 00:00:00 2001 From: Karandeep Singh Grover <30365139+groverkds@users.noreply.github.com> Date: Tue, 26 Jan 2021 09:59:06 +0530 Subject: [PATCH 17/19] Rename multiple_inheritance.md to 18_multiple_inheritance.md --- .../{multiple_inheritance.md => 18_multiple_inheritance.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Basics/Hindi/18_multiple_inheritance/{multiple_inheritance.md => 18_multiple_inheritance.md} (100%) diff --git a/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.md b/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.md similarity index 100% rename from Basics/Hindi/18_multiple_inheritance/multiple_inheritance.md rename to Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.md From 3eca98bd01aa0fce2b6611f1e14eac2c9a8c1b01 Mon Sep 17 00:00:00 2001 From: Karandeep Singh Grover <30365139+groverkds@users.noreply.github.com> Date: Tue, 26 Jan 2021 09:59:53 +0530 Subject: [PATCH 18/19] Rename multiple_inheritance.py to 18_multiple_inheritance.py --- .../{multiple_inheritance.py => 18_multiple_inheritance.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Basics/Hindi/18_multiple_inheritance/{multiple_inheritance.py => 18_multiple_inheritance.py} (100%) diff --git a/Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py b/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.py similarity index 100% rename from Basics/Hindi/18_multiple_inheritance/multiple_inheritance.py rename to Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.py From a841369c534f3c852c408393c0dc4e4c5d266d7a Mon Sep 17 00:00:00 2001 From: Karandeep Singh Grover <30365139+groverkds@users.noreply.github.com> Date: Tue, 26 Jan 2021 10:00:26 +0530 Subject: [PATCH 19/19] Rename inheritance.md to 17_inheritance.md --- Basics/Hindi/17_inheritance/{inheritance.md => 17_inheritance.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Basics/Hindi/17_inheritance/{inheritance.md => 17_inheritance.md} (100%) diff --git a/Basics/Hindi/17_inheritance/inheritance.md b/Basics/Hindi/17_inheritance/17_inheritance.md similarity index 100% rename from Basics/Hindi/17_inheritance/inheritance.md rename to Basics/Hindi/17_inheritance/17_inheritance.md