├── .gitattributes ├── Chapter 1 - PS ├── Problem1.py ├── Problem2.py ├── Problem3.py └── Problem4.py ├── Chapter 1 ├── first.py └── module.py ├── Chapter 10 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py └── 06_problem6.py ├── Chapter 10 ├── 01_class.py ├── 02_instance_vs_class_attr.py ├── 03_self.py └── 04_constructor.py ├── Chapter 11 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py ├── 06_problem6.py └── 07_problem7.py ├── Chapter 11 ├── 01_inheritance.py ├── 02_multiple_inheritance.py ├── 03_multilevel_inheritance.py ├── 04_super.py ├── 05_class_methods.py ├── 06_property_decorators.py └── 07_operator_overloading.py ├── Chapter 12 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05problem5.py └── tables.txt ├── Chapter 12 ├── 01_walrus.py ├── 02_types.py ├── 03_match_case.py ├── 04_exception.py ├── 05_raising_exceptions.py ├── 06_try_else.py ├── 07_try_finally.py ├── 08_main.py ├── 09_global.py ├── 10_enumerate.py ├── 11_list_comprehensions.py └── module.py ├── Chapter 13 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py ├── 06_problem6.py ├── 07_problem7.py ├── env1 │ └── .gitkeep ├── env2 │ └── .gitkeep ├── harryenv │ └── .gitkeep └── requirements.txt ├── Chapter 13 ├── 01_venv.py ├── 02_lambda.py ├── 03_join.py ├── 04_format.py ├── 05_map_filter_reduce.py └── requirements.txt ├── Chapter 2 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py └── 06_problem6.py ├── Chapter 2 ├── 01_variables.py ├── 02_datatypes.py ├── 03_rules_variables.py ├── 04_operators.py ├── 05_type.py └── 06_input.py ├── Chapter 3 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py └── 05_problem5.py ├── Chapter 3 ├── 01_intro_to_strings.py ├── 02_negative_slicing.py ├── 03_str_functions.py └── 04_escape_seq.py ├── Chapter 4 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py └── 05_problem5.py ├── Chapter 4 ├── 01_list.py ├── 02_list_methods.py ├── 03_tuple.py └── 04_tuple_methods.py ├── Chapter 5 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py ├── 06_problem6.py ├── 07_problem7.py ├── 08_problem8.py └── 09_problem9.py ├── Chapter 5 ├── 01_dict.py ├── 02_dict_methods.py ├── 03_sets.py ├── 04_set_methods.py └── 05_set_union_intersection.py ├── Chapter 6 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py ├── 06_problem6.py └── 07_problem7.py ├── Chapter 6 ├── 01_conditionals.py ├── 02_if_elif_else_ladder.py └── 03_multiple_if_statements.py ├── Chapter 7 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py ├── 06_problem6.py ├── 07_problem7.py ├── 08_problem8.py ├── 09_problem9.py └── 10_problem10.py ├── Chapter 7 ├── 01_loops.py ├── 02_while_loops.py ├── 03_list_using_while.py ├── 04_for_loops.py ├── 05_for_loop_iterate.py ├── 06_for_with_else.py ├── 07_break_and_continue.py └── 08_pass.py ├── Chapter 8 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py ├── 06_problem6.py ├── 07_problem7.py └── 08_problem8.py ├── Chapter 8 ├── 01_functions.py ├── 02_quick_quiz.py ├── 03_function_with_arguments.py ├── 04_default_argument.py └── 05_recursion.py ├── Chapter 9 - PS ├── 01_problem1.py ├── 02_problem2.py ├── 03_problem3.py ├── 04_problem4.py ├── 05_problem5.py ├── 06_problem.py ├── 07_problem7.py ├── 08_problem8.py ├── 09_problem9.py ├── 10_problem10.py ├── 11_problem.py ├── file.txt ├── hiscore.txt ├── log.txt ├── old.txt ├── poem.txt ├── renamed_by_python.txt ├── tables │ ├── table_10.txt │ ├── table_11.txt │ ├── table_12.txt │ ├── table_13.txt │ ├── table_14.txt │ ├── table_15.txt │ ├── table_16.txt │ ├── table_17.txt │ ├── table_18.txt │ ├── table_19.txt │ ├── table_2.txt │ ├── table_20.txt │ ├── table_3.txt │ ├── table_4.txt │ ├── table_5.txt │ ├── table_6.txt │ ├── table_7.txt │ ├── table_8.txt │ └── table_9.txt ├── this.txt └── this_copy.txt ├── Chapter 9 ├── 01_file.py ├── 02_file_write.py ├── 03_more_file_functions.py ├── 04_append.py ├── 05_with.py ├── file.txt └── myfile.txt ├── HandWritten_Python_Complete_Notes.pdf ├── Mega Project 1 - Jarvis ├── client.py ├── main.py ├── musicLibrary.py └── temp.mp3 ├── Mega Project 2 - AI AutoReply Bot ├── 01_get_cursor.py ├── 02_openai.py └── 03_bot.py ├── Project 1 ├── main-shortened.py └── main.py ├── Project 2 └── main.py ├── README.md └── The Ultimate Python Handbook.pdf /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Chapter 1 - PS/Problem1.py: -------------------------------------------------------------------------------- 1 | print(''' Twinkle, twinkle, little star, 2 | How I wonder what you are! 3 | Up above the world so high, 4 | Like a diamond in the sky. 5 | 6 | When the blazing sun is gone, 7 | When he nothing shines upon, 8 | Then you show your little light, 9 | Twinkle, twinkle, all the night. 10 | 11 | Then the trav'ller in the dark, 12 | Thanks you for your tiny spark, 13 | He could not see which way to go, 14 | If you did not twinkle so. 15 | 16 | In the dark blue sky you keep, 17 | And often thro' my curtains peep, 18 | For you never shut your eye, 19 | Till the sun is in the sky. 20 | 21 | 'Tis your bright and tiny spark, 22 | Lights the trav'ller in the dark: 23 | Tho' I know not what you are, 24 | Twinkle, twinkle, little star.''') -------------------------------------------------------------------------------- /Chapter 1 - PS/Problem2.py: -------------------------------------------------------------------------------- 1 | # Done using REPL -------------------------------------------------------------------------------- /Chapter 1 - PS/Problem3.py: -------------------------------------------------------------------------------- 1 | import pyttsx3 2 | engine = pyttsx3.init() 3 | engine.say("Hey I am good") 4 | engine.runAndWait() -------------------------------------------------------------------------------- /Chapter 1 - PS/Problem4.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | # Select the directory whose content you want to list 4 | directory_path = '/' 5 | 6 | # Use the os module to list the directory content 7 | contents = os.listdir(directory_path) 8 | 9 | # Print the contents of the directory 10 | print(contents) 11 | -------------------------------------------------------------------------------- /Chapter 1/first.py: -------------------------------------------------------------------------------- 1 | print("Hello World") -------------------------------------------------------------------------------- /Chapter 1/module.py: -------------------------------------------------------------------------------- 1 | import pyjokes 2 | 3 | # print("Printing Jokes...") 4 | 5 | # This prints a random joke 6 | joke = pyjokes.get_joke() 7 | print(joke) 8 | 9 | 10 | # so thanks 11 | # that was my program 12 | # another line 13 | # Yet another line -------------------------------------------------------------------------------- /Chapter 10 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | class Programmer: 2 | company = "Microsoft" 3 | def __init__(self, name, salary, pin): 4 | self.name = name 5 | self.salary = salary 6 | self.pin = pin 7 | 8 | 9 | p = Programmer("Harry", 1200000, 245001) 10 | print(p.name, p.salary, p.pin, p.company) 11 | r = Programmer("Rohan", 1200000, 245001) 12 | print(r.name, r.salary, r.pin, r.company) -------------------------------------------------------------------------------- /Chapter 10 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | class Calculator: 2 | def __init__(self, n): 3 | self.n = n 4 | 5 | def square(self): 6 | print(f"The square is {self.n*self.n}") 7 | 8 | def cube(self): 9 | print(f"The cube is {self.n*self.n*self.n}") 10 | 11 | def squareroot(self): 12 | print(f"The squareroot is {self.n**1/2}") 13 | 14 | a = Calculator(4) 15 | a.square() 16 | a.cube() 17 | a.squareroot() -------------------------------------------------------------------------------- /Chapter 10 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | class Demo: 2 | a = 4 3 | 4 | o = Demo() 5 | print(o.a) # Prints the class attribute because instance attribute is not present 6 | o.a = 0 # Instance attribute is set 7 | print(o.a) # Prints the instance attribute because instance attribute is present 8 | print(Demo.a) # Prints the class attribute -------------------------------------------------------------------------------- /Chapter 10 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | class Calculator: 2 | def __init__(self, n): 3 | self.n = n 4 | 5 | def square(self): 6 | print(f"The square is {self.n*self.n}") 7 | 8 | def cube(self): 9 | print(f"The cube is {self.n*self.n*self.n}") 10 | 11 | def squareroot(self): 12 | print(f"The squareroot is {self.n**1/2}") 13 | 14 | @staticmethod 15 | def hello(): 16 | print("Hello there!") 17 | 18 | a = Calculator(4) 19 | a.hello() 20 | a.square() 21 | a.cube() 22 | a.squareroot() -------------------------------------------------------------------------------- /Chapter 10 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | class Train: 4 | 5 | def __init__(self, trainNo): 6 | self.trainNo = trainNo 7 | 8 | def book(self, fro, to): 9 | print(f"Ticket is booked in train no: {self.trainNo} from {fro} to {to}") 10 | 11 | def getStatus(self): 12 | print(f"Train no: {self.trainNo} is running on time") 13 | 14 | def getFare(self, fro, to): 15 | print(f"Ticket fare in train no: {self.trainNo} from {fro} to {to} is {randint(222, 5555)}") 16 | 17 | 18 | t = Train(12399) 19 | t.book("Rampur", "Delhi") 20 | t.getStatus() 21 | t.getFare("Rampur", "Delhi") 22 | -------------------------------------------------------------------------------- /Chapter 10 - PS/06_problem6.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | class Train: 4 | 5 | def __init__(slf, trainNo): 6 | slf.trainNo = trainNo 7 | 8 | def book(harry, fro, to): 9 | print(f"Ticket is booked in train no: {harry.trainNo} from {fro} to {to}") 10 | 11 | def getStatus(self): 12 | print(f"Train no: {self.trainNo} is running on time") 13 | 14 | def getFare(self, fro, to): 15 | print(f"Ticket fare in train no: {self.trainNo} from {fro} to {to} is {randint(222, 5555)}") 16 | 17 | 18 | t = Train(12399) 19 | t.book("Rampur", "Delhi") 20 | t.getStatus() 21 | t.getFare("Rampur", "Delhi") 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter 10/01_class.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | language = "Py" # This is a class attribute 3 | salary = 1200000 4 | 5 | 6 | harry = Employee() 7 | harry.name = "Harry" # This is an instance attribute 8 | print(harry.name, harry.language, harry.salary) 9 | 10 | rohan = Employee() 11 | rohan.name = "Rohan Roro Robinson" 12 | print(rohan.name, rohan.salary, rohan.language) 13 | 14 | # Here name is instance attribute and salary and language are class attributes as they directly belong to the class -------------------------------------------------------------------------------- /Chapter 10/02_instance_vs_class_attr.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | language = "Python" # This is a class attribute 3 | salary = 1200000 4 | 5 | 6 | harry = Employee() 7 | harry.language = "JavaScript" # This is an instance attribute 8 | print(harry.language, harry.salary) 9 | -------------------------------------------------------------------------------- /Chapter 10/03_self.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | language = "Python" # This is a class attribute 3 | salary = 1200000 4 | 5 | def getInfo(self): 6 | print(f"The language is {self.language}. The salary is {self.salary}") 7 | 8 | @staticmethod 9 | def greet(): 10 | print("Good morning") 11 | 12 | 13 | harry = Employee() 14 | # harry.language = "JavaScript" # This is an instance attribute 15 | harry.greet() 16 | harry.getInfo() 17 | # Employee.getInfo(harry) 18 | -------------------------------------------------------------------------------- /Chapter 10/04_constructor.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | language = "Python" # This is a class attribute 3 | salary = 1200000 4 | 5 | def __init__(self, name, salary, language): # dunder method which is automatically called 6 | self.name = name 7 | self.salary = salary 8 | self.language = language 9 | print("I am creating an object") 10 | 11 | 12 | def getInfo(self): 13 | print(f"The language is {self.language}. The salary is {self.salary}") 14 | 15 | @staticmethod 16 | def greet(): 17 | print("Good morning") 18 | 19 | 20 | harry = Employee("Harry", 1300000, "JavaScript") 21 | # harry.name = "Harry" 22 | print(harry.name, harry.salary, harry.language) 23 | 24 | rohan = Employee() -------------------------------------------------------------------------------- /Chapter 11 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | class TwoDVector: 2 | def __init__(self, i, j): 3 | self.i = i 4 | self.j = j 5 | 6 | def show(self): 7 | print(f"The vector is {self.i}i + {self.j}j ") 8 | 9 | 10 | class ThreeDVector(TwoDVector): 11 | def __init__(self, i, j, k): 12 | super().__init__(i, j) 13 | self.k = k 14 | 15 | def show(self): 16 | print(f"The vector is {self.i}i + {self.j}j + {self.k}k") 17 | 18 | a = TwoDVector(1, 2) 19 | a.show() 20 | b = ThreeDVector(5, 2, 3) 21 | b.show() -------------------------------------------------------------------------------- /Chapter 11 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | class Animals: 2 | pass 3 | 4 | class Pets(Animals): 5 | pass 6 | 7 | class Dog(Pets): 8 | 9 | @staticmethod 10 | def bark(): 11 | print("Bow Bow!") 12 | 13 | 14 | d = Dog() 15 | 16 | d.bark() -------------------------------------------------------------------------------- /Chapter 11 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | salary = 234 3 | increment = 20 4 | 5 | @property 6 | def salaryAfterIncrement(self): 7 | return (self.salary + self.salary * (self.increment/100)) 8 | 9 | @salaryAfterIncrement.setter 10 | def salaryAfterIncrement(self, salary): 11 | self.increment = ((salary/self.salary) -1)*100 12 | 13 | 14 | 15 | 16 | e = Employee() 17 | # print(e.salaryAfterIncrement) 18 | e.salaryAfterIncrement = 280.8 19 | print(e.increment) -------------------------------------------------------------------------------- /Chapter 11 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | class Complex: 2 | def __init__(self, r, i): 3 | self.r = r 4 | self.i = i 5 | 6 | def __add__(self, c2): 7 | return Complex(self.r + c2.r, self.i + c2.i) 8 | 9 | def __mul__(self, c2): 10 | real_part = self.r * c2.r - self.i * c2.i 11 | imag_part = self.r * c2.i + self.i * c2.r 12 | return Complex(real_part, imag_part) 13 | 14 | def __str__(self): 15 | return f"{self.r} + {self.i}i" 16 | 17 | 18 | 19 | c1 = Complex(1, 2) 20 | c2 = Complex(3, 4) 21 | print(c1 + c2) 22 | print(c1*c2) -------------------------------------------------------------------------------- /Chapter 11 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | class Vector: 2 | def __init__(self, x, y, z): 3 | self.x = x 4 | self.y = y 5 | self.z = z 6 | 7 | def __add__(self, other): 8 | result = Vector(self.x + other.x, self.y + other.y, self.z + other.z) 9 | return result 10 | 11 | def __mul__(self, other): 12 | result = self.x * other.x + self.y * other.y + self.z * other.z 13 | return result 14 | 15 | def __str__(self): 16 | return f"Vector({self.x}, {self.y}, {self.z})" 17 | 18 | # Test the implementation 19 | v1 = Vector(1, 2, 3) 20 | v2 = Vector(4, 5, 6) 21 | v3 = Vector(7, 8, 9) # Same dimension vector 22 | 23 | print(v1 + v2) # Output: Vector(5, 7, 9) 24 | print(v1 * v2) # Output: 32 25 | 26 | print(v1 + v3) # Output: Vector(8, 10, 12) 27 | print(v1 * v3) # Output: 50 28 | -------------------------------------------------------------------------------- /Chapter 11 - PS/06_problem6.py: -------------------------------------------------------------------------------- 1 | class Vector: 2 | def __init__(self, x, y, z): 3 | self.x = x 4 | self.y = y 5 | self.z = z 6 | 7 | def __add__(self, other): 8 | result = Vector(self.x + other.x, self.y + other.y, self.z + other.z) 9 | return result 10 | 11 | def __mul__(self, other): 12 | result = self.x * other.x + self.y * other.y + self.z * other.z 13 | return result 14 | 15 | def __str__(self): 16 | return f"{self.x}i + {self.y}j + {self.z}k" 17 | 18 | # Test the implementation 19 | v1 = Vector(1, 2, 3) 20 | v2 = Vector(4, 5, 6) 21 | v3 = Vector(7, 8, 9) # Same dimension vector 22 | 23 | print(v1 + v2) # Output: Vector(5, 7, 9) 24 | print(v1 * v2) # Output: 32 25 | 26 | print(v1 + v3) # Output: Vector(8, 10, 12) 27 | print(v1 * v3) # Output: 50 28 | -------------------------------------------------------------------------------- /Chapter 11 - PS/07_problem7.py: -------------------------------------------------------------------------------- 1 | class Vector: 2 | def __init__(self, l): 3 | self.l = l 4 | 5 | 6 | 7 | def __len__(self): 8 | return len(self.l) 9 | 10 | # Test the implementation 11 | v1 = Vector([1, 2, 3]) 12 | print(len(v1)) -------------------------------------------------------------------------------- /Chapter 11/01_inheritance.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | company = "ITC" 3 | def show(self): 4 | print(f"The name of the Employee is {self.name} and the salary is {self.salary}") 5 | 6 | 7 | # class Programmer: 8 | # company = "ITC Infotech" 9 | # def show(self): 10 | # print(f"The name is {self.name} and the salary is {self.salary}") 11 | 12 | # def showLanguage(self): 13 | # print(f"The name is {self.name} and he is good with {self.language} language") 14 | 15 | 16 | class Programmer(Employee): 17 | company = "ITC Infotech" 18 | def showLanguage(self): 19 | print(f"The name is {self.name} and he is good with {self.language} language") 20 | 21 | 22 | a = Employee() 23 | b = Programmer() 24 | 25 | print(a.company, b.company) -------------------------------------------------------------------------------- /Chapter 11/02_multiple_inheritance.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | company = "ITC" 3 | name = "Default name" 4 | def show(self): 5 | print(f"The name of the Employee is {self.name} and the company is {self.company}") 6 | 7 | class Coder: 8 | language = "Python" 9 | def printLanguages(self): 10 | print(f"Out of all the languages here is your language: {self.language}") 11 | 12 | 13 | 14 | class Programmer(Employee, Coder): 15 | company = "ITC Infotech" 16 | def showLanguage(self): 17 | print(f"The name is {self.company} and he is good with {self.language} language") 18 | 19 | 20 | a = Employee() 21 | b = Programmer() 22 | 23 | b.show() 24 | b.printLanguages() 25 | b.showLanguage() -------------------------------------------------------------------------------- /Chapter 11/03_multilevel_inheritance.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | a = 1 3 | 4 | class Programmer(Employee): 5 | b = 2 6 | 7 | class Manager(Programmer): 8 | c = 3 9 | 10 | o = Employee() 11 | print(o.a) # Prints the a attribute 12 | # print(o.b) # Shows an error as there is no b attribute in Employee class 13 | 14 | o = Programmer() 15 | print(o.a, o.b) 16 | 17 | 18 | o = Manager() 19 | print(o.a, o.b, o.c) 20 | -------------------------------------------------------------------------------- /Chapter 11/04_super.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | def __init__(self): 3 | print("Constructor of Employee") 4 | a = 1 5 | 6 | class Programmer(Employee): 7 | def __init__(self): 8 | print("Constructor of Programmer") 9 | b = 2 10 | 11 | class Manager(Programmer): 12 | def __init__(self): 13 | super().__init__() 14 | print("Constructor of Manager") 15 | c = 3 16 | 17 | # o = Employee() 18 | # print(o.a) # Prints the a attribute 19 | 20 | # o = Programmer() 21 | # print(o.a, o.b) 22 | 23 | 24 | o = Manager() 25 | print(o.a, o.b, o.c) 26 | -------------------------------------------------------------------------------- /Chapter 11/05_class_methods.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | a = 1 3 | 4 | @classmethod 5 | def show(cls): 6 | print(f"The class attribute of a is {cls.a}") 7 | 8 | e = Employee() 9 | e.a = 45 10 | 11 | e.show() -------------------------------------------------------------------------------- /Chapter 11/06_property_decorators.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | a = 1 3 | 4 | @classmethod 5 | def show(cls): 6 | print(f"The class attribute of a is {cls.a}") 7 | 8 | @property 9 | def name(self): 10 | return f"{self.fname} {self.lname}" 11 | 12 | @name.setter 13 | def name (self,value): 14 | self.fname = value.split(" ")[0] 15 | self.lname = value.split(" ")[1] 16 | 17 | e = Employee() 18 | e.a = 45 19 | 20 | e.name = "Harry Khan" 21 | print(e.fname, e.lname) 22 | 23 | e.show() -------------------------------------------------------------------------------- /Chapter 11/07_operator_overloading.py: -------------------------------------------------------------------------------- 1 | class Number: 2 | def __init__(self, n): 3 | self.n = n 4 | 5 | def __add__(self, num): 6 | return self.n + num.n 7 | 8 | n = Number(1) 9 | m = Number(2) 10 | 11 | print(n + m) 12 | 13 | -------------------------------------------------------------------------------- /Chapter 12 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | 2 | try: 3 | with open("1.txt", "r") as f: 4 | print(f.read()) 5 | except Exception as e: 6 | print(e) 7 | 8 | try: 9 | with open("2.txt", "r") as f: 10 | print(f.read()) 11 | except Exception as e: 12 | print(e) 13 | 14 | try: 15 | with open("3.txt", "r") as f: 16 | print(f.read()) 17 | except Exception as e: 18 | print(e) 19 | 20 | 21 | print("Thank You!") -------------------------------------------------------------------------------- /Chapter 12 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | l = [1, 2, 3, 4, 5, 6 ,7 , 8] 2 | 3 | for i, item in enumerate(l): 4 | if i == 2 or i == 4 or i == 6: 5 | print(item) 6 | -------------------------------------------------------------------------------- /Chapter 12 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter a number: ")) 2 | 3 | table = [n*i for i in range(1, 11)] 4 | print(table) -------------------------------------------------------------------------------- /Chapter 12 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | try: 2 | a = int(input("Enter a: ")) 3 | b = int(input("Enter b: ")) 4 | print(a/b) 5 | except ZeroDivisionError as v: 6 | print("Infinite") 7 | -------------------------------------------------------------------------------- /Chapter 12 - PS/05problem5.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter a number: ")) 2 | 3 | table = [n*i for i in range(1, 11)] 4 | with open("tables.txt", "a") as f: 5 | f.write(f"Table of {n}: {str(table)} \n") -------------------------------------------------------------------------------- /Chapter 12 - PS/tables.txt: -------------------------------------------------------------------------------- 1 | Table of 5: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] 2 | Table of 2: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] 3 | Table of 22: [22, 44, 66, 88, 110, 132, 154, 176, 198, 220] 4 | Table of 3323423: [3323423, 6646846, 9970269, 13293692, 16617115, 19940538, 23263961, 26587384, 29910807, 33234230] 5 | -------------------------------------------------------------------------------- /Chapter 12/01_walrus.py: -------------------------------------------------------------------------------- 1 | # Using walrus operator 2 | if (n := len([1, 2, 3, 4, 5])) > 3: 3 | print(f"List is too long ({n} elements, expected <= 3)") # Output: List is too long (5 elements, expected <= 3) -------------------------------------------------------------------------------- /Chapter 12/02_types.py: -------------------------------------------------------------------------------- 1 | from typing import List, Union, Tuple 2 | 3 | n : int = 5 4 | 5 | name: str = "Harry" 6 | 7 | 8 | def sum(a: int, b: int) -> int: 9 | return a+b 10 | 11 | -------------------------------------------------------------------------------- /Chapter 12/03_match_case.py: -------------------------------------------------------------------------------- 1 | def http_status(status): 2 | match status: 3 | case 200: 4 | return "OK" 5 | case 404: 6 | return "Not Found" 7 | case 500: 8 | return "Internal Server Error" 9 | case _: 10 | return "Unknown status" 11 | 12 | 13 | print(http_status(5007)) -------------------------------------------------------------------------------- /Chapter 12/04_exception.py: -------------------------------------------------------------------------------- 1 | try: 2 | a = int(input("Hey, Enter a number: ")) 3 | print(a) 4 | 5 | except ValueError as v: 6 | print("Heyyyy") 7 | print(v) 8 | 9 | except Exception as e: 10 | print(e) 11 | 12 | print("Thank You") -------------------------------------------------------------------------------- /Chapter 12/05_raising_exceptions.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter a number: ")) 2 | b = int(input("Enter second number: ")) 3 | 4 | if(b == 0): 5 | raise ZeroDivisionError("Hey our program is not meant to divide numbers by zero") 6 | else: 7 | print(f"The division a/b is {a/b}") -------------------------------------------------------------------------------- /Chapter 12/06_try_else.py: -------------------------------------------------------------------------------- 1 | try: 2 | a = int(input("Hey, Enter a number: ")) 3 | print(a) 4 | 5 | 6 | except Exception as e: 7 | print(e) 8 | 9 | 10 | else: 11 | print("I am inside else") 12 | 13 | -------------------------------------------------------------------------------- /Chapter 12/07_try_finally.py: -------------------------------------------------------------------------------- 1 | def main(): 2 | try: 3 | a = int(input("Hey, Enter a number: ")) 4 | print(a) 5 | return 6 | 7 | 8 | except Exception as e: 9 | print(e) 10 | return 11 | 12 | 13 | finally: 14 | print("Hey I am inside of finally") 15 | 16 | 17 | main() -------------------------------------------------------------------------------- /Chapter 12/08_main.py: -------------------------------------------------------------------------------- 1 | from module import myFunc 2 | 3 | -------------------------------------------------------------------------------- /Chapter 12/09_global.py: -------------------------------------------------------------------------------- 1 | a = 89 2 | 3 | def fun(): 4 | # global a 5 | a = 3 6 | print(a) 7 | 8 | 9 | fun() 10 | print(a) -------------------------------------------------------------------------------- /Chapter 12/10_enumerate.py: -------------------------------------------------------------------------------- 1 | l = [3, 513, 53, 535] 2 | 3 | # index = 0 4 | # for item in l: 5 | # print(f"The item number at index {index} is {item}") 6 | # index += 1 7 | 8 | # This can be simplified using enumerate function 9 | 10 | for index, item in enumerate(l): 11 | print(f"The item number at index {index} is {item}") 12 | -------------------------------------------------------------------------------- /Chapter 12/11_list_comprehensions.py: -------------------------------------------------------------------------------- 1 | myList = [1, 2, 9, 5, 3, 5] 2 | 3 | # squaredList = [] 4 | # for item in myList: 5 | # squaredList.append(item*item) 6 | 7 | squaredList = [i*i for i in myList] 8 | 9 | print(squaredList) -------------------------------------------------------------------------------- /Chapter 12/module.py: -------------------------------------------------------------------------------- 1 | def myFunc(): 2 | print("Hello world!") 3 | 4 | 5 | 6 | if __name__ == "__main__": 7 | # If this code is directly executed by running the file its present in 8 | print("We are directly running this code") 9 | myFunc() 10 | print(__name__) -------------------------------------------------------------------------------- /Chapter 13 - PS/01_problem1.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/The-Ultimate-Python-Course/19865a0acd48c4b093ef81ead7fa190a0f99db69/Chapter 13 - PS/01_problem1.py -------------------------------------------------------------------------------- /Chapter 13 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | name = input("Enter name: ") 2 | marks = int(input("Enter marks: ")) 3 | phone = int(input("Phone number: ")) 4 | 5 | s = "The name of the student is {}, his marks are {} and phone number is {}".format(name, marks, phone) 6 | 7 | print(s) -------------------------------------------------------------------------------- /Chapter 13 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | table = [str(7*i) for i in range(1, 11)] 2 | 3 | s = "\n".join(table) 4 | print(s) -------------------------------------------------------------------------------- /Chapter 13 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | def divisible5(n): 2 | if(n%5 == 0): 3 | return True 4 | return False 5 | 6 | a = [1,2,34234,53,6234235,64343, 65,754,45,55] 7 | 8 | f = list(filter(divisible5, a)) 9 | print(f) -------------------------------------------------------------------------------- /Chapter 13 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | l = [111, 2, 65, 5553, 635, 65, 74, 45,55] 3 | 4 | 5 | def greater(a, b): 6 | if (a>b): 7 | return a 8 | return b 9 | 10 | print(reduce(greater, l)) -------------------------------------------------------------------------------- /Chapter 13 - PS/06_problem6.py: -------------------------------------------------------------------------------- 1 | # pip freeze > requirements.txt 2 | # virtualenv harryenv -------------------------------------------------------------------------------- /Chapter 13 - PS/07_problem7.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | 3 | app = Flask(__name__) 4 | 5 | @app.route("/") 6 | def hello_world(): 7 | return "
Hello, World!
" 8 | 9 | app.run() -------------------------------------------------------------------------------- /Chapter 13 - PS/env1/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/The-Ultimate-Python-Course/19865a0acd48c4b093ef81ead7fa190a0f99db69/Chapter 13 - PS/env1/.gitkeep -------------------------------------------------------------------------------- /Chapter 13 - PS/env2/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/The-Ultimate-Python-Course/19865a0acd48c4b093ef81ead7fa190a0f99db69/Chapter 13 - PS/env2/.gitkeep -------------------------------------------------------------------------------- /Chapter 13 - PS/harryenv/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/The-Ultimate-Python-Course/19865a0acd48c4b093ef81ead7fa190a0f99db69/Chapter 13 - PS/harryenv/.gitkeep -------------------------------------------------------------------------------- /Chapter 13 - PS/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/The-Ultimate-Python-Course/19865a0acd48c4b093ef81ead7fa190a0f99db69/Chapter 13 - PS/requirements.txt -------------------------------------------------------------------------------- /Chapter 13/01_venv.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/The-Ultimate-Python-Course/19865a0acd48c4b093ef81ead7fa190a0f99db69/Chapter 13/01_venv.py -------------------------------------------------------------------------------- /Chapter 13/02_lambda.py: -------------------------------------------------------------------------------- 1 | # def square(n): 2 | # return n*n 3 | 4 | square = lambda x: x*x 5 | 6 | print(square(5)) -------------------------------------------------------------------------------- /Chapter 13/03_join.py: -------------------------------------------------------------------------------- 1 | a = ["Harry", "Rohan", "Shubham"] 2 | 3 | final = "::".join(a) 4 | print(final) -------------------------------------------------------------------------------- /Chapter 13/04_format.py: -------------------------------------------------------------------------------- 1 | a = "{1} is a good {0}".format("harry", "boy") 2 | 3 | print(a) -------------------------------------------------------------------------------- /Chapter 13/05_map_filter_reduce.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | # Map Example 3 | l = [1, 2, 3, 4, 5] 4 | 5 | square = lambda x: x*x 6 | 7 | sqList = map(square, l) 8 | print(list(sqList)) 9 | 10 | # Filter Example 11 | def even(n): 12 | if (n%2 == 0): 13 | return True 14 | return False 15 | 16 | onlyEven= filter(even, l) 17 | print(list(onlyEven)) 18 | 19 | # Reduce Example 20 | def sum(a, b): 21 | return a + b 22 | 23 | mul = lambda x,y:x*y 24 | 25 | print(reduce(sum, l)) 26 | print(reduce(mul, l)) -------------------------------------------------------------------------------- /Chapter 13/requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/The-Ultimate-Python-Course/19865a0acd48c4b093ef81ead7fa190a0f99db69/Chapter 13/requirements.txt -------------------------------------------------------------------------------- /Chapter 2 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | a = 1 2 | 3 | b = 5 4 | 5 | print(a + b) -------------------------------------------------------------------------------- /Chapter 2 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | a = 37 2 | 3 | b = 5 4 | 5 | print("Remainder when a is divided by b is", a % b) -------------------------------------------------------------------------------- /Chapter 2 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | a = input("Enter the value of a: ") 2 | print(type(a)) -------------------------------------------------------------------------------- /Chapter 2 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter number 1: ")) 2 | b = int(input("Enter number 2: ")) 3 | 4 | print("a is greater than b is", a>b) -------------------------------------------------------------------------------- /Chapter 2 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter number 1: ")) 2 | b = int(input("Enter number 2: ")) 3 | 4 | 5 | print("The average of these two number is", (a+b)/2) -------------------------------------------------------------------------------- /Chapter 2 - PS/06_problem6.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter your number: ")) 2 | 3 | 4 | print("The square of the number is", a**2) 5 | print("The square of the number is", a*a) 6 | # print("The square of the number is", a^2) # Incorrect for finding square of a number in Python -------------------------------------------------------------------------------- /Chapter 2/01_variables.py: -------------------------------------------------------------------------------- 1 | a = 1 2 | 3 | b = 2 4 | 5 | c = 7 6 | 7 | name = "harry" 8 | 9 | print(a + b) -------------------------------------------------------------------------------- /Chapter 2/02_datatypes.py: -------------------------------------------------------------------------------- 1 | a = 1 # a is an integer 2 | 3 | b = 5.22 # b is a floating point number 4 | 5 | c = "Harry" # c is a string 6 | 7 | d = False # d is a boolean variable 8 | 9 | e = None # e is a none type variable -------------------------------------------------------------------------------- /Chapter 2/03_rules_variables.py: -------------------------------------------------------------------------------- 1 | a = 23 2 | 3 | aaa = 435 4 | 5 | harry = 34 6 | 7 | sameer = 45 8 | 9 | _samerr = 34 10 | 11 | # @sameer = 56 # Invalid due to @ symbol 12 | # s@meer # Invalid due to @ symbol -------------------------------------------------------------------------------- /Chapter 2/04_operators.py: -------------------------------------------------------------------------------- 1 | # Arithmetic Operators 2 | a = 7 3 | b = 4 4 | c = a + b 5 | print(c) 6 | 7 | # Assignment Operators 8 | a = 4-2 # Assign 4-2 in a 9 | print(a) 10 | b = 6 11 | # b += 3 # Increment the value of b by 3 and then assign it to b 12 | b -= 3 # Decrement the value of b by 3 and then assign it to b 13 | print(b) 14 | 15 | # Comparison Operators 16 | 17 | d = 5==5 18 | 19 | print(d) 20 | 21 | 22 | # Logical Operators 23 | 24 | e = True or False 25 | 26 | # Truth table of 'or' 27 | print("True or False is ", True or False) 28 | print("True or True is ", True or True) 29 | print("False or True is ", False or True) 30 | print("False or False is ", False or False) 31 | 32 | # Truth table of 'and' 33 | print("True and False is ", True and False) 34 | print("True and True is ", True and True) 35 | print("False and True is ", False and True) 36 | print("False and False is ", False and False) 37 | 38 | print(not(True)) -------------------------------------------------------------------------------- /Chapter 2/05_type.py: -------------------------------------------------------------------------------- 1 | a = "31.2" 2 | b = float(a) # a but the type should be float 3 | t = type(b) 4 | 5 | print(t) -------------------------------------------------------------------------------- /Chapter 2/06_input.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter number 1: ")) 2 | 3 | b = int(input("Enter number 2: ")) 4 | 5 | print("Number a is: ", a) 6 | print("Number b is: ", b) 7 | print("Sum is ", a + b) 8 | -------------------------------------------------------------------------------- /Chapter 3 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | name = input("Enter your name: ") 2 | 3 | print(f"Good Afternoon, {name} ") -------------------------------------------------------------------------------- /Chapter 3 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | letter = '''Dear <|Name|>, 2 | You are selected! 3 | <|Date|> ''' 4 | 5 | print(letter.replace("<|Name|>", "Harry").replace("<|Date|", "24 September 2050")) -------------------------------------------------------------------------------- /Chapter 3 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | name = "Harry is a good boy and " 2 | 3 | print(name.find(" ")) -------------------------------------------------------------------------------- /Chapter 3 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | name = "Harry is a good boy and " 2 | 3 | print(name.replace(" ", " ")) 4 | print(name) # Strings are immutable which means that you cannot change them by running functions on them -------------------------------------------------------------------------------- /Chapter 3 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | letter = "Dear Harry,\n\tThis python course is nice.\nThanks!" 2 | 3 | print(letter) -------------------------------------------------------------------------------- /Chapter 3/01_intro_to_strings.py: -------------------------------------------------------------------------------- 1 | name = "Harry" 2 | 3 | nameshort = name[0:3] # start from index 0 all the way till 3 (excluding 3) 4 | print(nameshort) 5 | character1 = name[1] 6 | print(character1) -------------------------------------------------------------------------------- /Chapter 3/02_negative_slicing.py: -------------------------------------------------------------------------------- 1 | name = "Harry" 2 | 3 | print(name[0:3]) 4 | 5 | print(name[-4: -1]) 6 | print(name[1:4]) 7 | 8 | print(name[:4]) # is same as print(name[0:4]) 9 | print(name[1:]) # is same as print(name[1:5]) 10 | print(name[1:5]) -------------------------------------------------------------------------------- /Chapter 3/03_str_functions.py: -------------------------------------------------------------------------------- 1 | name = "harry" 2 | 3 | print(len(name)) 4 | print(name.endswith("rry")) 5 | print(name.startswith("ha")) 6 | print(name.capitalize()) -------------------------------------------------------------------------------- /Chapter 3/04_escape_seq.py: -------------------------------------------------------------------------------- 1 | a = 'Harry is a good boy\nbut not a bad \'boy\'' 2 | 3 | 4 | print(a) -------------------------------------------------------------------------------- /Chapter 4 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | fruits = [] 2 | 3 | f1 = input("Enter Fruit name: ") 4 | fruits.append(f1) 5 | f2 = input("Enter Fruit name: ") 6 | fruits.append(f2) 7 | f3 = input("Enter Fruit name: ") 8 | fruits.append(f3) 9 | f4 = input("Enter Fruit name: ") 10 | fruits.append(f4) 11 | f5 = input("Enter Fruit name: ") 12 | fruits.append(f5) 13 | f6 = input("Enter Fruit name: ") 14 | fruits.append(f6) 15 | f7 = input("Enter Fruit name: ") 16 | fruits.append(f7) 17 | 18 | print(fruits) -------------------------------------------------------------------------------- /Chapter 4 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | marks = [] 2 | 3 | f1 = int(input("Enter Marks here: ")) 4 | marks.append(f1) 5 | f2 = int(input("Enter Marks here: ")) 6 | marks.append(f2) 7 | f3 = int(input("Enter Marks here: ")) 8 | marks.append(f3) 9 | f4 = int(input("Enter Marks here: ")) 10 | marks.append(f4) 11 | f5 = int(input("Enter Marks here: ")) 12 | marks.append(f5) 13 | f6 = int(input("Enter Marks here: ")) 14 | marks.append(f6) 15 | 16 | marks.sort() 17 | 18 | print(marks) -------------------------------------------------------------------------------- /Chapter 4 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | a = (34, 234, "Harry") 2 | 3 | a[2] = "Larry" -------------------------------------------------------------------------------- /Chapter 4 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | l = [3, 3, 5, 1] 2 | 3 | 4 | print(sum(l)) -------------------------------------------------------------------------------- /Chapter 4 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | a = (7, 0, 8, 0, 0, 9) 2 | 3 | n = a.count(0) 4 | print(n) -------------------------------------------------------------------------------- /Chapter 4/01_list.py: -------------------------------------------------------------------------------- 1 | friends = ["Apple", "Orange", 5, 345.06, False, "Aakash", "Rohan"] 2 | 3 | print(friends[0]) 4 | friends[0] = "Grapes" # Unlike Strings lists are mutable 5 | 6 | print(friends[0]) 7 | print(friends[1:4]) -------------------------------------------------------------------------------- /Chapter 4/02_list_methods.py: -------------------------------------------------------------------------------- 1 | friends = ["Apple", "Orange", 5, 345.06, False, "Aakash", "Rohan"] 2 | print(friends) 3 | friends.append("Harry") 4 | print(friends) 5 | 6 | l1 = [1, 34,62, 2, 6, 11] 7 | # l1.sort() 8 | # l1.reverse() 9 | # l1.insert(2, 333333) # Insert 333333 such that its index in the list is 3 10 | value = l1.pop(3) 11 | print(value) 12 | print(l1) -------------------------------------------------------------------------------- /Chapter 4/03_tuple.py: -------------------------------------------------------------------------------- 1 | a = (1,45,342,3424,False, "Rohan", "Shivam") 2 | print(a) 3 | print(type(a)) -------------------------------------------------------------------------------- /Chapter 4/04_tuple_methods.py: -------------------------------------------------------------------------------- 1 | a = (1,45,342,3424,False, 45, "Rohan", "Shivam") 2 | print(a) 3 | 4 | no = a.count(45) 5 | print(no) 6 | 7 | i = a.index(3424) 8 | print(i) 9 | 10 | print(len(a)) -------------------------------------------------------------------------------- /Chapter 5 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | words = { 2 | "madad": "Help", 3 | "kursi": "Chair", 4 | "billi": "Cat" 5 | } 6 | 7 | word = input("Enter the word you want meaning of: ") 8 | 9 | print(words[word]) -------------------------------------------------------------------------------- /Chapter 5 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | s = set() 2 | n = input("Enter number: ") 3 | s.add(int(n)) 4 | n = input("Enter number: ") 5 | s.add(int(n)) 6 | n = input("Enter number: ") 7 | s.add(int(n)) 8 | n = input("Enter number: ") 9 | s.add(int(n)) 10 | n = input("Enter number: ") 11 | s.add(int(n)) 12 | n = input("Enter number: ") 13 | s.add(int(n)) 14 | n = input("Enter number: ") 15 | s.add(int(n)) 16 | n = input("Enter number: ") 17 | s.add(int(n)) 18 | 19 | print(s) -------------------------------------------------------------------------------- /Chapter 5 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | s = set() 2 | s.add(18) 3 | s.add("18") 4 | 5 | print(s) -------------------------------------------------------------------------------- /Chapter 5 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | s = set() 2 | s.add(20) 3 | s.add(20.0) 4 | s.add('20') # length of s after these operations? 5 | 6 | print(len(s)) -------------------------------------------------------------------------------- /Chapter 5 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | s = {} 2 | print(type(s)) -------------------------------------------------------------------------------- /Chapter 5 - PS/06_problem6.py: -------------------------------------------------------------------------------- 1 | d = {} 2 | 3 | name = input("Enter friends name: ") 4 | lang = input("Enter Language name: ") 5 | d.update({name: lang}) 6 | 7 | name = input("Enter friends name: ") 8 | lang = input("Enter Language name: ") 9 | d.update({name: lang}) 10 | 11 | name = input("Enter friends name: ") 12 | lang = input("Enter Language name: ") 13 | d.update({name: lang}) 14 | 15 | name = input("Enter friends name: ") 16 | lang = input("Enter Language name: ") 17 | d.update({name: lang}) 18 | 19 | 20 | print(d) -------------------------------------------------------------------------------- /Chapter 5 - PS/07_problem7.py: -------------------------------------------------------------------------------- 1 | # The values entered later will be updated -------------------------------------------------------------------------------- /Chapter 5 - PS/08_problem8.py: -------------------------------------------------------------------------------- 1 | # Nothing will happen. The values can be same 2 | -------------------------------------------------------------------------------- /Chapter 5 - PS/09_problem9.py: -------------------------------------------------------------------------------- 1 | s = {8, 7, 12, "Harry", [1,2]} 2 | 3 | s[4][0] = 9 -------------------------------------------------------------------------------- /Chapter 5/01_dict.py: -------------------------------------------------------------------------------- 1 | d = {} # Empty dictionary 2 | marks = { 3 | "Harry": 100, 4 | "Shubham": 56, 5 | "Rohan": 23 6 | } 7 | 8 | 9 | # print(marks, type(marks)) 10 | print(marks["Harry"]) -------------------------------------------------------------------------------- /Chapter 5/02_dict_methods.py: -------------------------------------------------------------------------------- 1 | marks = { 2 | "Harry": 100, 3 | "Shubham": 56, 4 | "Rohan": 23, 5 | 0: "Harry" 6 | } 7 | 8 | # print(marks.items()) 9 | # print(marks.keys()) 10 | # print(marks.values()) 11 | # marks.update({"Harry": 99, "Renuka": 100}) 12 | # print(marks) 13 | 14 | print(marks.get("Harry2")) # Prints None 15 | print(marks["Harry2"]) # Returns an error -------------------------------------------------------------------------------- /Chapter 5/03_sets.py: -------------------------------------------------------------------------------- 1 | e = set() # Dont use s = {} as it will create an empty dictionary 2 | s = {1, 5, 32, 54,5, 5, 5} 3 | 4 | 5 | print(s) -------------------------------------------------------------------------------- /Chapter 5/04_set_methods.py: -------------------------------------------------------------------------------- 1 | s = {1, 5, 32, 54,5, 5, 5, "Harry"} 2 | 3 | print(s, type(s)) 4 | 5 | s.add(566) 6 | print(s, type(s)) 7 | s.remove(1) 8 | print(s, type(s)) -------------------------------------------------------------------------------- /Chapter 5/05_set_union_intersection.py: -------------------------------------------------------------------------------- 1 | s1 = {1, 45, 6, 78} 2 | s2 = {7, 8, 1, 78} 3 | 4 | print(s1.union(s2)) 5 | print(s1.intersection(s2)) -------------------------------------------------------------------------------- /Chapter 6 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | a1 = int(input("Enter number 1: ")) 2 | a2 = int(input("Enter number 2: ")) 3 | a3 = int(input("Enter number 3: ")) 4 | a4 = int(input("Enter number 4: ")) 5 | 6 | if(a1>a2 and a1>a3 and a1>a4): 7 | print("Greatest number is a1:", a1) 8 | 9 | elif(a2>a1 and a2>a3 and a2>a4): 10 | print("Greatest number is a2:", a2) 11 | 12 | elif(a3>a1 and a3>a2 and a3>a4): 13 | print("Greatest number is a3:", a3) 14 | 15 | elif(a4>a1 and a4>a2 and a4>a3): 16 | print("Greatest number is a4:", a4) -------------------------------------------------------------------------------- /Chapter 6 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | marks1 = int(input("Enter Marks 1: ")) 2 | marks2 = int(input("Enter Marks 2: ")) 3 | marks3 = int(input("Enter Marks 3: ")) 4 | 5 | # Check for total percentage 6 | total_percentage = (100*(marks1 + marks2 + marks3))/300 7 | 8 | if(total_percentage>=40 and marks1>=33 and marks2>=33 and marks3>=33): 9 | print("You are passed:", total_percentage) 10 | 11 | else: 12 | print("You failed, try again next year:", total_percentage) 13 | -------------------------------------------------------------------------------- /Chapter 6 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | p1 = "Make a lot of money" 2 | p2 = "buy now" 3 | p3 = "subscribe this" 4 | p4 = "click this" 5 | 6 | message = input("Enter your comment: ") 7 | 8 | if((p1 in message) or (p2 in message )or (p3 in message) or (p4 in message)): 9 | print("This comment is a spam") 10 | 11 | else: 12 | print("This comment is not a spam") -------------------------------------------------------------------------------- /Chapter 6 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | username = input("Enter username: ") 2 | 3 | if(len(username)<10): 4 | print("Your username contains less than 10 characters") 5 | else: 6 | print("Your username contains more than or equal to 10 characters") -------------------------------------------------------------------------------- /Chapter 6 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | l = ["Harry", "Rohan", "Shubham", "Divya"] 2 | 3 | name = input("Enter your name: ") 4 | 5 | if(name in l): 6 | print("Your name is in the list") 7 | else: 8 | print("Your name is not in the list") -------------------------------------------------------------------------------- /Chapter 6 - PS/06_problem6.py: -------------------------------------------------------------------------------- 1 | marks = int(input("Enter your marks: ")) 2 | 3 | if(marks<=100 and marks>=90): 4 | grade = "Ex" 5 | elif(marks<90 and marks>=80): 6 | grade = "A" 7 | elif(marks<80 and marks>=70): 8 | grade = "B" 9 | elif(marks<70 and marks>=60): 10 | grade = "C" 11 | elif(marks<60 and marks>=50): 12 | grade = "D" 13 | elif(marks<50): 14 | grade = "F" 15 | 16 | print("Your grade is:", grade) -------------------------------------------------------------------------------- /Chapter 6 - PS/07_problem7.py: -------------------------------------------------------------------------------- 1 | post = input("Enter the post: ") 2 | 3 | 4 | 5 | if("harry" in post.lower()): 6 | print("This post is talking about harry") 7 | 8 | else: 9 | print("This post is not talking about harry") -------------------------------------------------------------------------------- /Chapter 6/01_conditionals.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter your age: ")) 2 | 3 | # If else statement 4 | if(a>=18): 5 | print("You are above the age of consent") 6 | print("Good for you") 7 | 8 | else: 9 | print("You are below the age of consent") 10 | 11 | 12 | print("End of Program") 13 | 14 | -------------------------------------------------------------------------------- /Chapter 6/02_if_elif_else_ladder.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter your age: ")) 2 | 3 | # If elif else ladder 4 | if(a>=18): 5 | print("You are above the age of consent") 6 | print("Good for you") 7 | 8 | elif(a<0): 9 | print("You are entering an invalid negative age") 10 | 11 | elif(a==0): 12 | print("You are entering 0 which is not a valid age") 13 | 14 | else: 15 | print("You are below the age of consent") 16 | 17 | 18 | print("End of Program") 19 | 20 | -------------------------------------------------------------------------------- /Chapter 6/03_multiple_if_statements.py: -------------------------------------------------------------------------------- 1 | a = int(input("Enter your age: ")) 2 | 3 | # If statement no: 1 4 | if(a%2 == 0): 5 | print("a is even") 6 | # End of If statement no: 1 7 | 8 | # If statement no: 2 9 | if(a>=18): 10 | print("You are above the age of consent") 11 | print("Good for you") 12 | 13 | elif(a<0): 14 | print("You are entering an invalid negative age") 15 | 16 | else: 17 | print("You are below the age of consent") 18 | # End of If statement no: 2 19 | 20 | print("End of Program") 21 | 22 | -------------------------------------------------------------------------------- /Chapter 7 - PS/01_problem1.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter a number: ")) 2 | 3 | for i in range(1, 11): 4 | print(f"{n} X {i} = {n * i}") -------------------------------------------------------------------------------- /Chapter 7 - PS/02_problem2.py: -------------------------------------------------------------------------------- 1 | l = ["Harry", "Soham", "Sachin", "Rahul"] 2 | 3 | for name in l: 4 | if(name.startswith("S")): 5 | print(f"Hello {name}") -------------------------------------------------------------------------------- /Chapter 7 - PS/03_problem3.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter a number: ")) 2 | 3 | i = 1 4 | 5 | while(i<11): 6 | print(f"{n} X {i} = {n * i}") 7 | i += 1 -------------------------------------------------------------------------------- /Chapter 7 - PS/04_problem4.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter a number: ")) 2 | 3 | for i in range(2, n): 4 | if(n%i) == 0: 5 | print("Number is not prime") 6 | break 7 | else: 8 | print("Number is prime") -------------------------------------------------------------------------------- /Chapter 7 - PS/05_problem5.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter the number: ")) 2 | i = 1 3 | sum = 0 4 | while(i<=n): 5 | sum += i 6 | i+=1 7 | 8 | print(sum) -------------------------------------------------------------------------------- /Chapter 7 - PS/06_problem6.py: -------------------------------------------------------------------------------- 1 | # 5! = 1 X 2 X 3 X 4 X 5 2 | 3 | n = int(input("Enter the number: ")) 4 | product = 1 5 | for i in range(1, n+1): 6 | product = product * i 7 | 8 | print(f"The factorial of {n} is {product}") -------------------------------------------------------------------------------- /Chapter 7 - PS/07_problem7.py: -------------------------------------------------------------------------------- 1 | ''' 2 | For n = 3 3 | * 4 | *** 5 | ***** 6 | 7 | For n = 5 8 | * 9 | *** 10 | ***** 11 | ******** 12 | ********** 13 | 14 | ''' 15 | 16 | n = int(input("Enter the number: ")) 17 | for i in range(1, n+1): 18 | print(" "* (n-i), end="") 19 | print("*"* (2*i-1), end="") 20 | print("") -------------------------------------------------------------------------------- /Chapter 7 - PS/08_problem8.py: -------------------------------------------------------------------------------- 1 | 2 | n = int(input("Enter the number: ")) 3 | for i in range(1, n+1): 4 | print("*"* i, end="") 5 | print("") -------------------------------------------------------------------------------- /Chapter 7 - PS/09_problem9.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | *** 4 | * * for n = 3 5 | *** 6 | 7 | 8 | ''' 9 | n = int(input("Enter the number: ")) 10 | for i in range(1, n+1): 11 | if(i==1 or i==n): 12 | print("*"* n, end="") 13 | else: 14 | print("*", end="") 15 | print(" "* (n-2), end="") 16 | print("*", end="") 17 | print("") -------------------------------------------------------------------------------- /Chapter 7 - PS/10_problem10.py: -------------------------------------------------------------------------------- 1 | n = int(input("Enter the number: ")) 2 | 3 | for i in range(1, 11): 4 | print(f"{n} X {11 -i} = {n*(11-i)}") 5 | 6 | -------------------------------------------------------------------------------- /Chapter 7/01_loops.py: -------------------------------------------------------------------------------- 1 | print(1) 2 | print(2) 3 | print(3) 4 | print(4) 5 | print(5) 6 | 7 | # The same task can be done like this: 8 | for i in range(1, 6): 9 | print(i) -------------------------------------------------------------------------------- /Chapter 7/02_while_loops.py: -------------------------------------------------------------------------------- 1 | i = 1 2 | 3 | while(i<51): 4 | print(i) 5 | i +=1 # or i = i + 1 6 | ''' 7 | Output: 8 | 1 9 | 2 10 | 3 11 | 4 12 | 5 13 | ''' 14 | -------------------------------------------------------------------------------- /Chapter 7/03_list_using_while.py: -------------------------------------------------------------------------------- 1 | l = [1, "Harry", False, "This", "Rohan", "Shubham", "Shubhi"] 2 | 3 | i = 0 4 | 5 | while(i