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 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 diff --git a/Basics/Hindi/17_inheritance/17_inheritance.md b/Basics/Hindi/17_inheritance/17_inheritance.md new file mode 100644 index 00000000..925e0945 --- /dev/null +++ b/Basics/Hindi/17_inheritance/17_inheritance.md @@ -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) + + + diff --git a/Basics/Hindi/17_inheritance/17_inheritance.py b/Basics/Hindi/17_inheritance/17_inheritance.py new file mode 100644 index 00000000..b9c860db --- /dev/null +++ b/Basics/Hindi/17_inheritance/17_inheritance.py @@ -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() diff --git a/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.md b/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.md new file mode 100644 index 00000000..46f2c3ea --- /dev/null +++ b/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.md @@ -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) + + + diff --git a/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.py b/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.py new file mode 100644 index 00000000..57be8094 --- /dev/null +++ b/Basics/Hindi/18_multiple_inheritance/18_multiple_inheritance.py @@ -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() 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 new file mode 100644 index 00000000..da42cfea --- /dev/null +++ b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.md @@ -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) + + + 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 new file mode 100644 index 00000000..317047d2 --- /dev/null +++ b/Basics/Hindi/19_raise_exception_finally/19_raise_exception_finally.py @@ -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() diff --git a/Basics/Hindi/20_Iterators/20_Iterators.md b/Basics/Hindi/20_Iterators/20_Iterators.md new file mode 100644 index 00000000..100ca74f --- /dev/null +++ b/Basics/Hindi/20_Iterators/20_Iterators.md @@ -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) diff --git a/Basics/Hindi/20_Iterators/20_Iterators.py b/Basics/Hindi/20_Iterators/20_Iterators.py new file mode 100644 index 00000000..5caa6a57 --- /dev/null +++ b/Basics/Hindi/20_Iterators/20_Iterators.py @@ -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 diff --git a/Basics/Hindi/21_generators/21_generators.md b/Basics/Hindi/21_generators/21_generators.md new file mode 100644 index 00000000..471441eb --- /dev/null +++ b/Basics/Hindi/21_generators/21_generators.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/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/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 new file mode 100644 index 00000000..dbbebc37 --- /dev/null +++ b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.md @@ -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) 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 new file mode 100644 index 00000000..7fcb3547 --- /dev/null +++ b/Basics/Hindi/22_list_set_dict_comprehension/22_list_set_dict_comprehension.py @@ -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) diff --git a/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.md b/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.md new file mode 100644 index 00000000..b711c4d9 --- /dev/null +++ b/Basics/Hindi/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/Hindi/23_sets_frozensets/23_sets_frozensets.py b/Basics/Hindi/23_sets_frozensets/23_sets_frozensets.py new file mode 100644 index 00000000..9b7567e7 --- /dev/null +++ b/Basics/Hindi/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) diff --git a/Basics/Hindi/24_argparse/24_argparse.md b/Basics/Hindi/24_argparse/24_argparse.md new file mode 100644 index 00000000..45b66c09 --- /dev/null +++ b/Basics/Hindi/24_argparse/24_argparse.md @@ -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) diff --git a/Basics/Hindi/24_argparse/24_argparse.py b/Basics/Hindi/24_argparse/24_argparse.py new file mode 100644 index 00000000..f35d7790 --- /dev/null +++ b/Basics/Hindi/24_argparse/24_argparse.py @@ -0,0 +1,19 @@ +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/Hindi/25_decorators/25_decorators.md b/Basics/Hindi/25_decorators/25_decorators.md new file mode 100644 index 00000000..d11c387c --- /dev/null +++ b/Basics/Hindi/25_decorators/25_decorators.md @@ -0,0 +1,16 @@ +## Exercise: Decorators + +1. Create a decorator function to check that the argument passed to the function factorial is a non-negative integer: + +2. Create a factorial function which finds the factorial of a number. + +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 new file mode 100644 index 00000000..ad5dbe58 --- /dev/null +++ b/Basics/Hindi/25_decorators/25_decorators.py @@ -0,0 +1,30 @@ +def check(f): + def helper(x): + if type(x) == int and x > 0: + return f(x) + else: + 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) + + +for i in range(1, 10): + print(i, factorial(i)) + +try: + print(factorial(-1)) +except Exception as e: + e.print_exception() + +try: + print(factorial(1.354)) +except Exception as e: + e.print_exception() \ No newline at end of file diff --git a/Basics/Hindi/26_multithreading/26_multithreading.md b/Basics/Hindi/26_multithreading/26_multithreading.md new file mode 100644 index 00000000..0777e663 --- /dev/null +++ b/Basics/Hindi/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/Hindi/26_multithreading/26_multithreading.py b/Basics/Hindi/26_multithreading/26_multithreading.py new file mode 100644 index 00000000..601a4e3e --- /dev/null +++ b/Basics/Hindi/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()) +