diff --git a/homeworks/10_functions/find_factors.py b/homeworks/10_functions/find_factors.py new file mode 100644 index 0000000..b09533f --- /dev/null +++ b/homeworks/10_functions/find_factors.py @@ -0,0 +1,15 @@ +def factors(num): + result = [] + if num < 1: + return result + else: + for i in range(1, num+1): + # print (i) + # print(num%i) + if (num%i == 0): + # print(num/i) + result.append(i) + return result + +num = int(input("enter the num:")) +print (factors(num)) \ No newline at end of file diff --git a/homeworks/10_functions/star_rectangle.py b/homeworks/10_functions/star_rectangle.py new file mode 100644 index 0000000..38568a6 --- /dev/null +++ b/homeworks/10_functions/star_rectangle.py @@ -0,0 +1,22 @@ +""" +To create a Rectangle like the below: + +******* +* * +* * +* * +* * +******* + +""" + +def star_rectangle(width, length): + for i in range(0, length): + if i == 0 or i == length -1: + print("*" * width) + else: + print("*", ' ' * (width - 4), "*") + + + +star_rectangle(7, 6) diff --git a/homeworks/10_functions/sum_of_digits.py b/homeworks/10_functions/sum_of_digits.py new file mode 100644 index 0000000..6f6bdf6 --- /dev/null +++ b/homeworks/10_functions/sum_of_digits.py @@ -0,0 +1,9 @@ +def sum_digit(num): + result = 0 + for i in num: + if i.isdigit(): + result += int(i) + return result + +num = input("enter a number:") +print(sum_digit(num)) \ No newline at end of file diff --git a/homeworks/11_exceptions/safe_division.py b/homeworks/11_exceptions/safe_division.py new file mode 100644 index 0000000..ea5d678 --- /dev/null +++ b/homeworks/11_exceptions/safe_division.py @@ -0,0 +1,13 @@ +def safe_divide(num, den): + try: + print(f"The result is {num / den}") + + except ZeroDivisionError: + print("ERROR: Cannot be divided by zero") + finally: + print("Division is done") + + +num = int(input("Enter the numerator:")) +den = int(input("Enter the denominator:")) +safe_divide(num, den) diff --git a/homeworks/11_exceptions/voting.py b/homeworks/11_exceptions/voting.py new file mode 100644 index 0000000..a25f8bc --- /dev/null +++ b/homeworks/11_exceptions/voting.py @@ -0,0 +1,9 @@ +def check_voter_age(age): + if age < 18: + raise ValueError("Not eligible to vote") + print("Eligible to vote") +try: + age = int(input("Enter the age:")) + check_voter_age(age) +except ValueError as e: + print ("Error:", e) \ No newline at end of file diff --git a/homeworks/12_modules/cli_task_manager.py b/homeworks/12_modules/cli_task_manager.py new file mode 100644 index 0000000..51b8726 --- /dev/null +++ b/homeworks/12_modules/cli_task_manager.py @@ -0,0 +1,35 @@ +import argparse + +def list_TASKS(TASKS): + if len(TASKS) != 0: + for item in TASKS: + print (item) + else: + print("No Tasks Available") + +def update_TASKS (operation): + if operation == "add": + task = input("Enter the task to add:") + TASKS.append(task) + print("Task Updated") + elif operation == "delete": + pass + +TASKS = [] +parser = argparse.ArgumentParser() +# parser.add_argument("add", help = "Add new task") +# parser.add_argument("delete", help = "Delete existing task") +parser.add_argument("--operation", help = "Add or Remove a task from manager", choices=["add", "delete"]) +parser.add_argument("--list", help = "List all tasks", action="store_true") +args = parser.parse_args() + +if args.operation == "add": + update_TASKS("add") +elif args.operation == "delete": + update_TASKS("delete") + +if args.list: + list_TASKS(TASKS) + + + diff --git a/homeworks/13_file_handling/file_content_reverser.py b/homeworks/13_file_handling/file_content_reverser.py new file mode 100644 index 0000000..6662b51 --- /dev/null +++ b/homeworks/13_file_handling/file_content_reverser.py @@ -0,0 +1,16 @@ +def reverser(): + try: + lines = [] + with open ("test.txt", "r") as f: + lines = f.readlines() + # print (lines) + f.close() + with open ("test.txt","w") as f: + for line in lines[::-1]: + f.writelines(line.rstrip("\n") + "\n") + # f.write() + print("File reversed") + except FileNotFoundError: + print("Error: File Not Found") + +reverser() \ No newline at end of file diff --git a/homeworks/13_file_handling/safe_file_reader.py b/homeworks/13_file_handling/safe_file_reader.py new file mode 100644 index 0000000..6c5d855 --- /dev/null +++ b/homeworks/13_file_handling/safe_file_reader.py @@ -0,0 +1,12 @@ +def safe_file_reader(): + try: + with open("test1.txt", "r") as f: + print("Test file found successfully...\n") + lines = f.readlines() + for line in lines: + print(line) + except FileNotFoundError: + print("Error: Test file is not available in current directory") + return None + +safe_file_reader() \ No newline at end of file diff --git a/homeworks/13_file_handling/test.txt b/homeworks/13_file_handling/test.txt new file mode 100644 index 0000000..87202e0 --- /dev/null +++ b/homeworks/13_file_handling/test.txt @@ -0,0 +1,2 @@ +Hi Logi +How are you? \ No newline at end of file diff --git a/homeworks/15_oops/multi-level_inheritance.py b/homeworks/15_oops/multi-level_inheritance.py new file mode 100644 index 0000000..fe04764 --- /dev/null +++ b/homeworks/15_oops/multi-level_inheritance.py @@ -0,0 +1,40 @@ +class Animal: + def __init__(self, name): + self.name = name + def eat(self): + return f"{self.name} is eating" + +class Mammal(Animal): + def __init__(self,name,has_fur): + super().__init__(name) + self.has_fur = has_fur + def walk(self): + return f"{self.name} is walking" + +class Dog(Mammal): + def __init__(self,name,has_fur,breed): + super().__init__(name,has_fur) + self.breed = breed + def bark(self): + return f"{self.name} can Bark" + def fur_info(self): + if self.has_fur == True: + return f"{self.name} is having fur" + else: + return f"{self.name} does not have fur" + +class Cat(Mammal): + def __init__(self, name, has_fur, breed): + Mammal.__init__(self,name,has_fur) + self.breed = breed + def meow(self): + return f"{self.name} can meow" + + +if __name__ == '__main__': + a = Dog(name="Leo", has_fur=True, breed="Labrador") + print(a.walk(), '\n', a.bark()) + print (a.fur_info()) + + b = Cat(name="Min", has_fur=False, breed="Persian") + print(b.walk(), '\n', b.meow()) diff --git a/homeworks/15_oops/multiple_inheritance.py b/homeworks/15_oops/multiple_inheritance.py new file mode 100644 index 0000000..744b83d --- /dev/null +++ b/homeworks/15_oops/multiple_inheritance.py @@ -0,0 +1,24 @@ +class Phone: + def __init__(self, brand, number): + self.brand = brand + self.number = number + + def call(self): + return f"Calling from {self.number} with {self.brand}" + +class Camera: + def __init__(self, mps): + self.mps = mps + + def take_photo(self): + return f"Taking photo with {self.mps} megapixels camera" + +class Smartphone(Phone, Camera): + def __init__(self,brand,number,mps): + Phone.__init__(self,brand,number) + Camera.__init__(self,mps) + +sp = Smartphone(brand= 'Iphone', number=123456789, mps=45) + +print(sp.call()) +print(sp.take_photo()) \ No newline at end of file diff --git a/homeworks/15_oops/pet_simulator.py b/homeworks/15_oops/pet_simulator.py new file mode 100644 index 0000000..9cf3a8f --- /dev/null +++ b/homeworks/15_oops/pet_simulator.py @@ -0,0 +1,21 @@ +class Pet: + def __init__(self, name, hunger, happiness, health): + self.name = name + self.hunger = hunger + self.happiness = happiness + self.health = health + + def get_status(self): + return f"Pet name is {self.name}, it is {self.hunger} & {self.happiness}. It's health is {self.health}" + def feed(self): + return f"{self.name} is eating" + + def play(self): + self.happiness = "Happy" + return f"{self.name} is {self.happiness}" + + +pet = Pet("Leo", "Hungry", "Sad", "Average") +print(pet.get_status()) +print(pet.feed()) +print(pet.play()) \ No newline at end of file diff --git a/homeworks/15_oops/single_inheritance.py b/homeworks/15_oops/single_inheritance.py new file mode 100644 index 0000000..721fe16 --- /dev/null +++ b/homeworks/15_oops/single_inheritance.py @@ -0,0 +1,22 @@ +class Employee: + def __init__(self, name, salary): + self.name = name + self.salary = salary + + def get_employee_info(self): + return (f"Employee Name: {self.name}, Employee Salary: {self.salary}") + +class Manager(Employee): + def __init__(self, name, salary, dept): + super().__init__(name,salary) + self.dept = dept + + def get_manager_info(self): + return (f"Manager Info:{self.name}, Manager Salary: {self.salary}, Manager Department: {self.dept}") + +if __name__ == '__main__': + emp = Employee("Ajay", 20000) + mgr = Manager("Logi", 200000, "SW & ADAS") + print(emp.get_employee_info()) + print(mgr.get_employee_info()) + print(mgr.get_manager_info()) \ No newline at end of file diff --git a/homeworks/15_oops/test.py b/homeworks/15_oops/test.py new file mode 100644 index 0000000..5202555 --- /dev/null +++ b/homeworks/15_oops/test.py @@ -0,0 +1,32 @@ +# class Vehicle: +# def __init__(self, wheels, color): +# self.wheels = wheels +# self.color = color +# class Car(Vehicle): +# def __init__(self, wheels, color, gears): +# super().__init__(wheels, color) +# self.gears = gears +# def gear_info(self): +# return (f"{self.color} Car has {self.gears} gears") +# +# honda = Vehicle(wheels=2, color="White") +# honda_car = Car(wheels=4, color="Black", gears= 6) +# print(honda.color, honda.wheels) +# print(honda_car.color, honda_car.wheels) +# print (honda_car.gear_info()) + + +class Vehicle: + def __init__(self): + self.wheels = 2 + self.color = "Black" +class Car(Vehicle): + def __init__(self): + super().__init__() + self.wheels = 4 + +bike = Vehicle() +car = Car() + +print (bike.color , bike.wheels) +print (car.color, car.wheels) \ No newline at end of file diff --git a/homeworks/ch1-hw1.py b/homeworks/ch1-hw1.py new file mode 100644 index 0000000..4539546 --- /dev/null +++ b/homeworks/ch1-hw1.py @@ -0,0 +1,3 @@ +user_name= input("Enter your name:") +fav_color= input("Enter favourite color:") +print (f"Hi,{user_name}. {fav_color} is your favourite color") \ No newline at end of file diff --git a/homeworks/ch1-hw2.py b/homeworks/ch1-hw2.py new file mode 100644 index 0000000..23008a7 --- /dev/null +++ b/homeworks/ch1-hw2.py @@ -0,0 +1,4 @@ +user_addr= input("Enter user address:") +item_name= input("Enter Item name:") +item_cost= input("Enter item cost:") +print(f"Address:{user_addr} \n Item Purchased:{item_name} \n Item Cost:${item_cost}") \ No newline at end of file diff --git a/homeworks/ch1-hw3.py b/homeworks/ch1-hw3.py new file mode 100644 index 0000000..5f84197 --- /dev/null +++ b/homeworks/ch1-hw3.py @@ -0,0 +1,2 @@ +prompt = input("Hi, Tell me what you have learnt today?\n") +print(f"Amazing to hear!! You have learnt {prompt}") \ No newline at end of file diff --git a/homeworks/ch2-hw1.py b/homeworks/ch2-hw1.py new file mode 100644 index 0000000..f7b76dd --- /dev/null +++ b/homeworks/ch2-hw1.py @@ -0,0 +1,3 @@ +inch = float(input("Enter the inches:")) +cm = inch * 2.54 +print (f"{inch} inches is equal to {cm}cm") \ No newline at end of file diff --git a/homeworks/ch2-hw2.py b/homeworks/ch2-hw2.py new file mode 100644 index 0000000..c6ebffc --- /dev/null +++ b/homeworks/ch2-hw2.py @@ -0,0 +1,3 @@ +fah = float(input("Enter the Fahrenheit:")) +Cel = float ((fah - 32) * (5/9)) +print (f"{fah} Fahrenheit is {round(Cel,2)} Celsius") \ No newline at end of file diff --git a/homeworks/ch2-hw3.py b/homeworks/ch2-hw3.py new file mode 100644 index 0000000..f82aa04 --- /dev/null +++ b/homeworks/ch2-hw3.py @@ -0,0 +1,5 @@ +principal = int(input("Enter the principal amount:")) +rate = int(input("enter the rate of interest:")) +time = int(input("enter the time period:")) +SI = round((float((principal * rate * time)/100)),1) +print ("The total simple interest is",SI) \ No newline at end of file diff --git a/homeworks/ch3-hw1.py b/homeworks/ch3-hw1.py new file mode 100644 index 0000000..d0baf4b --- /dev/null +++ b/homeworks/ch3-hw1.py @@ -0,0 +1,5 @@ +a= int(input("enter the no:")) +if ( a >= -3 and a <= 7): + print(f"the number {a} belongs to the interval") +else: + print(f"the number {a} doesn't belong to the interval") \ No newline at end of file diff --git a/homeworks/ch3-hw2.py b/homeworks/ch3-hw2.py new file mode 100644 index 0000000..2f1acb8 --- /dev/null +++ b/homeworks/ch3-hw2.py @@ -0,0 +1,6 @@ +a = int(input("enter the no:")) +if ((a>= -30 and a <= -25) or (a >= 7 and a <= 25)): + print (f"the number {a} belong to the interval") +else: + print(f"the number {a} does not belong to the interval") + diff --git a/homeworks/ch3-hw3.py b/homeworks/ch3-hw3.py new file mode 100644 index 0000000..9e685f8 --- /dev/null +++ b/homeworks/ch3-hw3.py @@ -0,0 +1,9 @@ +weight = int(input("Enter the bodyweight:")) +if (weight > 64 and weight <=69): + print("Middleweight") +elif (weight> 60 and weight <=64): + print ("First Middleweight") +elif (weight<= 60): + print ("LightWeight") +else: + print("Not a valid weight category") \ No newline at end of file diff --git a/homeworks/ch3-hw4.py b/homeworks/ch3-hw4.py new file mode 100644 index 0000000..4da0d8b --- /dev/null +++ b/homeworks/ch3-hw4.py @@ -0,0 +1,7 @@ +a = int(input("Enter first value for triangle:")) +b = int(input("Enter second value for triangle:")) +c = int(input("Enter third value for triangle:")) +if ((a+b) len(city2) and len(city1)>len(city3)): + print (city1) +elif (len(city2)>len(city1) and len(city2)>len(city3)): + print(city2) +elif (len(city3)>len(city1) and len(city3)>len(city2)): + print(city3) \ No newline at end of file diff --git a/homeworks/reverse-sentence.py b/homeworks/reverse-sentence.py new file mode 100644 index 0000000..7b4806c --- /dev/null +++ b/homeworks/reverse-sentence.py @@ -0,0 +1,8 @@ +# ip = [] +# print ("Enter 3 lines:\n") +# for i in range(4): +# line= input() +# line = ip.append() +# print(line[0,]) + +print (10//3) \ No newline at end of file diff --git a/homeworks/test.py b/homeworks/test.py new file mode 100644 index 0000000..11cc767 --- /dev/null +++ b/homeworks/test.py @@ -0,0 +1,6 @@ +a= int(input("enter the no:")) +print(f"raw_input of{a}") +if ( a >= -3 and a <= 7): + print(f"the number {a} belongs to the interval") +else: + print(f"the number {a} doesn't belong to the interval") \ No newline at end of file diff --git a/homeworks/test1.py b/homeworks/test1.py new file mode 100644 index 0000000..2f1acb8 --- /dev/null +++ b/homeworks/test1.py @@ -0,0 +1,6 @@ +a = int(input("enter the no:")) +if ((a>= -30 and a <= -25) or (a >= 7 and a <= 25)): + print (f"the number {a} belong to the interval") +else: + print(f"the number {a} does not belong to the interval") +