├── day01 ├── concatenation.py ├── hello_world.py ├── if_statements.py ├── module_example.py ├── operators.py ├── test.py ├── timer.py ├── user_input.py └── variable.py ├── day02 ├── branching_statements.py ├── custom_class.py ├── functions.py ├── loops.py ├── strings.py ├── test.py ├── tuples.py └── utility.py ├── day03 ├── abstraction1.py ├── abstraction2.py ├── encapsulation2.py ├── encapsulations1.py ├── inheritance.py └── lists.py └── day04 ├── dictionary.py ├── exceptions.py ├── extras.py ├── file_handlings.py ├── files ├── Test.json ├── Test.txt └── Test2.json ├── final_keyword.py ├── polymorphism.py ├── read_write_json.py └── sets.py /day01/concatenation.py: -------------------------------------------------------------------------------- 1 | 2 | name = 'James' 3 | age = 36 4 | 5 | print('My name is ' + name) 6 | print(str('My age is ') + str(age) ) 7 | 8 | print('My age is {}'.format(age)) 9 | print('My name is {} and I am {} years old'.format(name, age)) 10 | 11 | print(f'My name is {name} and I am {age} years old') 12 | 13 | print('Python', 3, 'is awesome:', True) 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /day01/hello_world.py: -------------------------------------------------------------------------------- 1 | print("Hello World") # this is print statement 2 | print('Hello World') # thi sis also print statement 3 | 4 | """ 5 | comment1 6 | comment2 7 | comment3 8 | ... 9 | """ 10 | 11 | 12 | print('Cydeo\nSchool') 13 | print('\tPython Programming') 14 | print('\\') 15 | 16 | 17 | print('I love \"Python\"') 18 | print('I love "Python"') 19 | 20 | print("I love \"CYDEO\"") 21 | print('I love \'CYDEO\'') 22 | -------------------------------------------------------------------------------- /day01/if_statements.py: -------------------------------------------------------------------------------- 1 | if False: 2 | print('Python Programming') 3 | 4 | print('Java Programming') 5 | 6 | print('------------------------------------') 7 | 8 | score = 70 9 | 10 | if score >= 60: 11 | print('Congrats! you passed the exam') 12 | 13 | """ 14 | if(true){ 15 | } 16 | 17 | """ 18 | 19 | s = 'Hello World' 20 | 21 | if 'H' and 'W' in s: 22 | print(s, 'has', 'H and W') 23 | 24 | print('------------------------------------') 25 | 26 | if score >= 60: 27 | print('Passed') 28 | else: 29 | print('Failed') 30 | 31 | print('---------------------------------') 32 | age = 20 33 | result = None 34 | 35 | if age >= 21: 36 | result = 'Eligible' 37 | else: 38 | result = "Not Eligible" 39 | 40 | print(result) 41 | 42 | print('---------------------------------') 43 | 44 | # Ternary: 45 | 46 | age = 26 47 | result = 'Eligible' if age >= 21 else 'Not Eligible' 48 | 49 | print(result) 50 | 51 | print('---------------------------------') 52 | 53 | num = 100 54 | 55 | result = None 56 | 57 | if num > 0: 58 | result = "Positive" 59 | elif num < 0: # same with else if block of Java 60 | result = "Negative" 61 | else: 62 | result = "Zero" 63 | 64 | print(result) 65 | 66 | print('---------------------------------') 67 | 68 | num = 200 69 | 70 | result2 = 'Positive' if num >= 0 else 'Zero' 71 | 72 | print(result2) 73 | 74 | print('---------------------------------') 75 | 76 | score = -300 77 | 78 | if 0 <= score <= 100: 79 | if score >= 60: 80 | print('Passed') 81 | else: 82 | print('Failed') 83 | else: 84 | print('Invalid Score') 85 | 86 | print('---------------------------------') 87 | 88 | score = 95 89 | 90 | if 0 <= score <= 100: 91 | if score >= 90: 92 | print('A') 93 | elif score >= 80: 94 | print('B') 95 | elif score >= 70: 96 | print('C') 97 | elif score >= 60: 98 | print('D') 99 | else: 100 | print('F') 101 | else: 102 | print('Invalid Score') 103 | 104 | 105 | """ 106 | A score of a student is given, write a program that can calculate the grade of the student 107 | 108 | Possible grades are: 109 | 1. A ( 90 ~ 100) 110 | 2. B ( 80 ~ 90 ) 111 | 3. C ( 70 ~ 80 ) 112 | 4. D ( 60 ~ 70 ) 113 | 5. F ( less than 60) 114 | """ 115 | 116 | 117 | -------------------------------------------------------------------------------- /day01/module_example.py: -------------------------------------------------------------------------------- 1 | 2 | print('Java') 3 | 4 | variable = 'Python' 5 | 6 | def test(): 7 | pass 8 | 9 | class Animal: 10 | pass 11 | 12 | """ 13 | ....... 14 | """ -------------------------------------------------------------------------------- /day01/operators.py: -------------------------------------------------------------------------------- 1 | # arithmetic: +, -, *, /, % 2 | 3 | print(10 - 2) 4 | print(10 + 2) 5 | 6 | print(10 * 3) 7 | 8 | print(10 / 3) 9 | 10 | print(10 / 2) 11 | 12 | print(10 % 3) 13 | 14 | print(int(100 / 2)) 15 | 16 | 17 | # shorthand assignment operators: =, +=, -=, *=, /=, %= 18 | 19 | a = 100 20 | 21 | a = 200 22 | 23 | print(a) 24 | 25 | 26 | a += 100 27 | 28 | print(a) 29 | 30 | a -= 50 31 | 32 | print(a) 33 | 34 | a /= 2 35 | 36 | print(a) 37 | 38 | 39 | # logical operators: and, or, not 40 | s = 'Hello World' 41 | 42 | print('H' and 'W' in s) 43 | 44 | print(True and True) 45 | print(True or False) 46 | 47 | s = 'Java Python C# C++' 48 | 49 | print( 'Java' or 'Ruby' in s) 50 | 51 | age = 29 52 | citizen_ship = 'USA' 53 | is_eligible = age >= 18 and citizen_ship == 'USA' 54 | 55 | print(is_eligible) 56 | 57 | has_java = 'Java' in s 58 | print(has_java) 59 | 60 | # !contains() 61 | print('Python' not in s) 62 | 63 | print(not False) 64 | print(not True) # ! 65 | 66 | print('----------------------------------') 67 | 68 | s = 'Java' 69 | s2 = 'Java' 70 | 71 | print( s is s2) # to heck if two variables are referencing to the same objects ( == operator of java) 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /day01/test.py: -------------------------------------------------------------------------------- 1 | import day02.utility as u 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /day01/timer.py: -------------------------------------------------------------------------------- 1 | import time as t 2 | 3 | minutes = int(input('please enter the number of minutes\n')) 4 | 5 | while minutes > 0: 6 | seconds = 59 7 | 8 | while seconds >=0: 9 | print(f'\r{minutes-1} minutes and {seconds} seconds left', end='') 10 | t.sleep(1) 11 | seconds -= 1 12 | 13 | minutes -= 1 14 | 15 | 16 | print('\n\033[31m') 17 | print("\t\t\t* * * * * * * * * * * * * * * * * * * * * * * * * *") 18 | print("\t\t\t* *") 19 | print("\t\t\t* Times is Up, Please take your seats! *") 20 | print("\t\t\t* *") 21 | print("\t\t\t* * * * * * * * * * * * * * * * * * * * * * * * * *") -------------------------------------------------------------------------------- /day01/user_input.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | #print( help(input) ) 4 | 5 | name = input('Enter your name:\n') 6 | age = int( input('Enter your age:\n') ) 7 | gpa = float(input('Enter your gpa:\n')) 8 | boolean_value = bool(input('Enter True or False:\n')) 9 | 10 | print(name) 11 | print( type(name) ) 12 | 13 | print( age ) 14 | print( type(age) ) 15 | 16 | print(gpa) 17 | print( type(gpa)) 18 | 19 | print(boolean_value) 20 | print( type(boolean_value)) -------------------------------------------------------------------------------- /day01/variable.py: -------------------------------------------------------------------------------- 1 | """ 2 | In Java: static Typing 3 | DataType var = Data 4 | 5 | In Python: Dynamic Typing 6 | var = Data 7 | """ 8 | 9 | name = None 10 | 11 | print(name) 12 | 13 | name = 'James' 14 | 15 | print(name) 16 | 17 | print(type(name)) 18 | 19 | age = 25 20 | 21 | print( type(age)) 22 | 23 | gpa = 3.5 24 | 25 | print( type(gpa) ) 26 | 27 | is_employed = True 28 | 29 | print( type(is_employed)) 30 | 31 | 32 | s = '25' 33 | 34 | #str(s) 35 | s = int(s) 36 | print(s * 5) 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /day02/branching_statements.py: -------------------------------------------------------------------------------- 1 | 2 | for i in range(1, 6): 3 | print(i) 4 | if i == 3: 5 | break # exits the loop 6 | 7 | print('-------------') 8 | 9 | for i in range(1, 6): 10 | if i == 3 or i == 4: 11 | continue # skips to the next iteration 12 | print(i) 13 | 14 | print('-------------') 15 | 16 | 17 | for i in range(1, 6): 18 | if i == 3 or i == 4: 19 | pass 20 | print(i) 21 | 22 | 23 | def function(): 24 | pass 25 | 26 | if True: 27 | pass 28 | 29 | class Class: 30 | 31 | def method(self): 32 | pass 33 | 34 | pass 35 | 36 | -------------------------------------------------------------------------------- /day02/custom_class.py: -------------------------------------------------------------------------------- 1 | import numbers 2 | 3 | 4 | class Employee: 5 | is_human = True # similar to static variable of Java 6 | planet = 'Earth' 7 | 8 | def __init__(self, name: str = 'Unknown', job_title: str = 'Janitor', salary: numbers = 0): 9 | self.name = name 10 | self.job_title = job_title 11 | self.salary = salary 12 | 13 | def work(self): 14 | print(f'{self.name} is working') 15 | 16 | def __str__(self): 17 | return f'{type(self).__name__}{self.__dict__}' 18 | 19 | 20 | employee1 = Employee() 21 | 22 | print(employee1.name) 23 | print(employee1.job_title) 24 | print(employee1.salary) 25 | 26 | employee2 = Employee('Daniel') 27 | 28 | print(employee2.name) 29 | print(employee2.job_title) 30 | print(employee2.salary) 31 | 32 | employee3 = Employee('Breanna', 'SDET') 33 | 34 | print(employee3.name) 35 | print(employee3.job_title) 36 | print(employee3.salary) 37 | 38 | employee4 = Employee('Yulia', 'Python Developer', 150_000) 39 | print(employee4.name) 40 | print(employee4.job_title) 41 | print(employee4.salary) 42 | 43 | # print(Employee.name) 44 | print(Employee.is_human) 45 | print(Employee.planet) 46 | 47 | employee1.work() 48 | employee2.work() 49 | employee3.work() 50 | employee4.work() 51 | 52 | print(employee1) 53 | print(employee2) 54 | print(employee3) 55 | print(employee4) 56 | -------------------------------------------------------------------------------- /day02/functions.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | Java Methods: 4 | public static void method(parameter){ 5 | 6 | } 7 | 8 | 9 | """ 10 | import numbers 11 | 12 | 13 | def display_message(): 14 | print('Hello Cydeo') 15 | print('Hello World') 16 | 17 | 18 | display_message() 19 | 20 | 21 | def value(): 22 | return 'Python Programming' 23 | 24 | 25 | print(value()) 26 | 27 | 28 | def return_int() -> int: 29 | return 10.0 30 | 31 | 32 | print(return_int()) 33 | 34 | 35 | def square(num: int): 36 | return num * num 37 | 38 | 39 | print(square(10)) 40 | 41 | 42 | # print( square('Java')) 43 | 44 | # print( divide('C#', 'Python')) 45 | 46 | def add(num1: numbers, num2: numbers) -> numbers: 47 | return num1 + num2 48 | 49 | 50 | print(add(10, 20)) 51 | 52 | print(add(10.5, 20.5)) 53 | 54 | print('------------------------------------------') 55 | 56 | 57 | def calculate(num1: numbers, num2: numbers, operator: str) -> numbers: 58 | if operator == '-': 59 | return num1 - num2 60 | elif operator == '+': 61 | return num1 + num2 62 | elif operator == '*': 63 | return num1 * num2 64 | elif operator == '/': 65 | return num1 / num2 66 | else: 67 | print('Invalid operator') 68 | return 0 69 | 70 | 71 | print(calculate(10, 20, '+')) 72 | 73 | print(calculate(100.5, 2.5, '/')) 74 | 75 | print('---------------------------------------------------------') 76 | 77 | 78 | # example of method overloading 79 | 80 | def sum(n1: numbers, n2: numbers, n3: numbers = 0, n4: numbers = 0) -> numbers: 81 | return n1 + n2 + n3 + n4 82 | 83 | 84 | print(sum(10, 20)) 85 | 86 | print(sum(10, 20, 30)) 87 | 88 | print(sum(10, 20, 30, 40)) 89 | 90 | 91 | class test: 92 | def method(self): 93 | pass 94 | 95 | 96 | print('----------------------------------------') 97 | 98 | 99 | def concat(a: str, b, c='', d='', e=''): 100 | print(f'{a} {b} {c} {d} {e}'.strip()) 101 | 102 | 103 | concat('Cydeo', 'School') 104 | concat('Python', 3, 2.5) 105 | concat('Python', 3, 2.5, True) 106 | concat('Python', 3, 2.5, True, False) 107 | 108 | 109 | """ 110 | 1. Declaring 111 | 2. parameters 112 | 3. restricting parameter' data type 113 | 4. setting default value to parameter 114 | 5. restricting return type 115 | 116 | Dynamic Typing 117 | """ -------------------------------------------------------------------------------- /day02/loops.py: -------------------------------------------------------------------------------- 1 | 2 | s = 'Python Programming' 3 | 4 | for each in s: 5 | print(each) 6 | 7 | print('-----------------------') 8 | 9 | for i in range(0, len(s)): 10 | print(s[i]) 11 | 12 | print('-----------------------') 13 | 14 | for i in range(-len(s), 0): 15 | print(s[i]) 16 | 17 | print('-----------------------') 18 | 19 | for i in reversed( range(0, len(s)) ): 20 | print(s[i]) 21 | 22 | print('-----------------------') 23 | 24 | result = '' 25 | 26 | for x in s[::-1]: 27 | result += x 28 | 29 | print(result) 30 | 31 | print('-----------------------') 32 | 33 | for x in range(1, 11): 34 | print('Hello World') 35 | 36 | print('-----------------------') 37 | 38 | num = int( input('Enter a positive number:\n')) 39 | 40 | while num <= 0: 41 | num = int( input('Enter a positive number:\n')) 42 | 43 | print(f'You have entered {num}') 44 | 45 | print('-----------------------') 46 | 47 | answer = input('Enter yes or no:\n') 48 | 49 | while not ( answer.lower() == 'yes' or answer.lower() =='no'): 50 | answer = input('Enter yes or no:\n') 51 | 52 | print(f'You have entered {answer}') 53 | -------------------------------------------------------------------------------- /day02/strings.py: -------------------------------------------------------------------------------- 1 | name = 'Python' 2 | 3 | print(len(name)) 4 | print(name[0]) 5 | print(name[len(name) - 1]) 6 | 7 | print(name[-1]) 8 | print(name[-2]) 9 | 10 | s = 'Java Programming Language' 11 | s2 = s[5:16] 12 | 13 | print(s2) 14 | 15 | s3 = s[:4] 16 | 17 | print(s3) 18 | 19 | s4 = s[::-1] # reverses the string after slicing 20 | 21 | print(s4) 22 | 23 | s = 'Python Programming' 24 | 25 | s5 = str(reversed(s)) 26 | 27 | print(type(s5)) 28 | reversed(s5) 29 | 30 | print(s5) 31 | 32 | s5 = s[::-1] 33 | 34 | print(s5) 35 | 36 | s5 = 'python Programming' 37 | 38 | s6 = s5[7:] # by default it slices the string from index 7 to the last character 39 | 40 | print(s6) 41 | 42 | 43 | s7 = 'CYDEO SCHOOL' 44 | # str(reversed(s7)) 45 | 46 | print('----------------------') 47 | 48 | # print( help(str)) 49 | 50 | 51 | print('----------------------') 52 | 53 | s = 'python programming language' 54 | 55 | # s = s.capitalize() 56 | s = s.title() 57 | print(s) 58 | 59 | s = " Python " 60 | s = s.strip() # trim method of java 61 | 62 | print(s) 63 | 64 | 65 | s = 'JAVA' 66 | 67 | print( s.index('A')) 68 | print(s.rindex('A')) 69 | 70 | s = 'Java Java' 71 | 72 | s = s.replace('Java', 'Python', 1) 73 | 74 | print(s) 75 | 76 | s = 'C# C# Python' 77 | 78 | s = s.replace(' C#', ' Java') 79 | print(s) 80 | 81 | 82 | s = 'Java jAVA java JAVA Python Python' 83 | 84 | count_java = s.lower().count('java') 85 | count_python = s.count('Python') 86 | 87 | print(count_java) 88 | print(count_python) 89 | 90 | 91 | s1 = 'java' 92 | s2 = 'JAVA' 93 | 94 | print( s1.lower() == s2.lower()) # ignore case 95 | 96 | 97 | s = 'Java' 98 | 99 | print(s[0].islower()) 100 | print(s[0].isupper() ) 101 | 102 | 103 | s = 'a' 104 | 105 | print(s.isalpha()) 106 | 107 | s = '1' 108 | 109 | print(s.isdigit()) 110 | 111 | 112 | s = 'Cydeo School' 113 | 114 | print(s.istitle()) 115 | 116 | -------------------------------------------------------------------------------- /day02/test.py: -------------------------------------------------------------------------------- 1 | # import utility 2 | 3 | from utility import sum, calculate # similar to static import of java 4 | 5 | result = sum(10, 20) 6 | 7 | print(result) 8 | 9 | result = calculate(10, 200, '+') 10 | 11 | print(result) 12 | 13 | import utility 14 | 15 | utility.concat("Java", "Python") 16 | utility.sum(10, 20) 17 | utility.calculate(10, 30, "*") 18 | 19 | print('---------------------------------------------------------') 20 | 21 | import utility as u 22 | 23 | u.sum(10, 20) 24 | u.concat("A", 1, 2) 25 | u.calculate(20, 30, "/") 26 | 27 | print('---------------------------------------------------------') 28 | 29 | from utility import sum as s 30 | 31 | print( s(10, 20) ) 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /day02/tuples.py: -------------------------------------------------------------------------------- 1 | days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", 1, 2, 3, 4, 5, 6, 7, True, False) 2 | 3 | print(type(days)) 4 | print(len(days)) 5 | 6 | print(days) 7 | 8 | # days[0] = 'Java' 9 | # print(days) 10 | 11 | 12 | print(days[0]) 13 | print(days[-1]) 14 | 15 | days = ('Monday', "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") 16 | 17 | # work_days = days[1:-3] 18 | work_days = days[1:4] 19 | print(work_days) 20 | 21 | week_days = days[:-2] 22 | 23 | print(week_days) 24 | 25 | weekend = days[-3:] 26 | print(weekend) 27 | 28 | # == , is 29 | 30 | tuple1 = (1, 2, 3) 31 | tuple2 = (1, 2, 3) 32 | 33 | print( tuple1 == tuple2) 34 | print( tuple1 is tuple2) 35 | 36 | tuple3 = tuple1 + tuple2 37 | print(tuple3) 38 | 39 | tuple4 = tuple1 * 5 40 | print(tuple4) 41 | 42 | reversed_tuple1 = days[::-1] 43 | 44 | print(reversed_tuple1) 45 | 46 | reversed_tuple2 = tuple(reversed(days)) 47 | print(reversed_tuple2) 48 | 49 | print(days) 50 | 51 | print(days.index('Wednesday')) 52 | 53 | numbers = (10, 10, 10, 10, 20, 30, 40, 50, 10) 54 | 55 | print( numbers.count(10) ) 56 | 57 | print('-------------------------') 58 | 59 | for x in days: 60 | print(x) 61 | 62 | print('-------------------------') 63 | 64 | for x in range(0, len(days)): 65 | print(x) 66 | 67 | 68 | print('-------------------------') 69 | 70 | 71 | for x in reversed(range(0, len(days))): 72 | print(x) 73 | 74 | print('-------------------------') 75 | 76 | nested_tuple = ( (1, 2, 3), (4, 5, 6, 7,8), (9, 10) ) 77 | 78 | print(len(nested_tuple)) 79 | 80 | print('-------------------------') 81 | 82 | for x in nested_tuple: 83 | print(x) 84 | for y in x: 85 | print(y) 86 | 87 | print('-------------------------') 88 | 89 | for i in range(0, len(nested_tuple)): 90 | for j in range(0, len(nested_tuple[i])): 91 | print(nested_tuple[i][j]) -------------------------------------------------------------------------------- /day02/utility.py: -------------------------------------------------------------------------------- 1 | import numbers 2 | 3 | 4 | def concat(a: str, b, c='', d='', e=''): 5 | print(f'{a} {b} {c} {d} {e}'.strip()) 6 | 7 | 8 | def sum(n1: numbers, n2: numbers, n3: numbers = 0, n4: numbers = 0) -> numbers: 9 | return n1 + n2 + n3 + n4 10 | 11 | 12 | def calculate(num1: numbers, num2: numbers, operator: str) -> numbers: 13 | if operator == '-': 14 | return num1 - num2 15 | elif operator == '+': 16 | return num1 + num2 17 | elif operator == '*': 18 | return num1 * num2 19 | elif operator == '/': 20 | return num1 / num2 21 | else: 22 | print('Invalid operator') 23 | return 0 -------------------------------------------------------------------------------- /day03/abstraction1.py: -------------------------------------------------------------------------------- 1 | import numbers 2 | # from day02.utility import className 3 | 4 | 5 | class Shape: 6 | 7 | def __init__(self): 8 | self.name = type(self).__name__ 9 | 10 | def area(self) -> numbers: 11 | pass 12 | 13 | def __str__(self): 14 | return f'{type(self).__name__}{self.__dict__}' 15 | 16 | 17 | class Square(Shape): 18 | 19 | def __init__(self, side): 20 | super().__init__() 21 | self.side = side 22 | 23 | 24 | square = Square(5) 25 | 26 | print(square) 27 | print( square.area()) 28 | -------------------------------------------------------------------------------- /day03/abstraction2.py: -------------------------------------------------------------------------------- 1 | import math 2 | import numbers 3 | 4 | from abc import ABC, abstractmethod 5 | 6 | # import abc 7 | 8 | """ 9 | abc: module name (built in module) 10 | ABC: Abstract class in abc module 11 | abstractmethod: annotations that can be given to abstract methods 12 | """ 13 | 14 | 15 | class Volume(ABC): 16 | 17 | @abstractmethod 18 | def volume(self): 19 | pass 20 | 21 | 22 | class Shape(ABC): 23 | 24 | def __init__(self): 25 | self.name = type(self).__name__ 26 | 27 | @abstractmethod 28 | def area(self) -> numbers: 29 | pass 30 | 31 | def __str__(self): 32 | return f'{type(self).__name__}{self.__dict__}' 33 | 34 | 35 | class Square(Shape): 36 | 37 | def __init__(self, side): 38 | super().__init__() 39 | self.side = side 40 | 41 | def area(self) -> numbers: 42 | return self.side * self.side 43 | 44 | 45 | class Circle(Shape): 46 | 47 | def __init__(self, radius): 48 | super().__init__() 49 | self.radius = radius 50 | 51 | def area(self) -> numbers: 52 | return math.pow(self.radius) * math.pi 53 | 54 | 55 | class Rectangle(Shape): 56 | 57 | def __init__(self, width, length): 58 | super().__init__() 59 | self.width = width 60 | self.length = length 61 | 62 | def area(self) -> numbers: 63 | return self.width * self.length 64 | 65 | 66 | class Cube(Shape, Volume): 67 | 68 | def area(self) -> numbers: 69 | pass 70 | 71 | def volume(self): 72 | pass 73 | 74 | 75 | class Cylinder(Shape, Volume): 76 | 77 | def area(self) -> numbers: 78 | pass 79 | 80 | def volume(self): 81 | pass 82 | 83 | 84 | square1 = Square(5) 85 | 86 | print(square1) 87 | print(square1.area()) 88 | -------------------------------------------------------------------------------- /day03/encapsulation2.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | 3 | def __init__(self, name: str = 'Jimmy', age: int = 2): 4 | self.__name = None 5 | self.__age = None 6 | self.person_name = name 7 | self.person_age = age 8 | 9 | @property 10 | def person_name(self): 11 | if self.__name is None or self.__name == '' or type(self.__name) != str: 12 | raise RuntimeError(f'Invalid name has been set: {self.__name}') 13 | 14 | return self.__name 15 | 16 | @person_name.setter 17 | def person_name(self, name): 18 | if type(name) != str: 19 | raise RuntimeError(f'Person name must be string') 20 | 21 | if name == '': 22 | raise RuntimeError(f'Person name can not be empty') 23 | 24 | self.__name = name 25 | 26 | @property 27 | def person_age(self): 28 | return self.__age 29 | 30 | @person_age.setter 31 | def person_age(self, age): 32 | if age < 0 or age > 150: 33 | raise RuntimeError(f'Inavlid age {age}') 34 | 35 | self.__age = age 36 | 37 | def __str__(self): 38 | return f'{type(self).__name__}{str(self.__dict__).replace("_Person__", "")}' 39 | 40 | 41 | person1 = Person() 42 | 43 | person1.person_name = "Daniel" 44 | print(person1.person_name) 45 | -------------------------------------------------------------------------------- /day03/encapsulations1.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | 3 | def __init__(self, name: str = 'Unknown', age: int = 1): 4 | self.__name = None 5 | self.__age = None 6 | self.set_name(name) 7 | self.set_age(age) 8 | 9 | def get_name(self) -> str: 10 | if self.__name is None or self.__name == '' or type(self.__name) != str: 11 | raise RuntimeError(f'Invalid name has been set: {self.__name}') 12 | 13 | return self.__name 14 | 15 | def set_name(self, name: str): 16 | if type(name) != str: 17 | raise RuntimeError(f'Person name must be string') 18 | 19 | if name == '': 20 | raise RuntimeError(f'Person name can not be empty') 21 | 22 | self.__name = name 23 | 24 | def get_age(self) -> int: 25 | return self.__age 26 | 27 | def set_age(self, age): 28 | 29 | if age < 0 or age > 150: 30 | raise RuntimeError(f'Inavlid age {age}') 31 | self.__age = age 32 | 33 | def __str__(self): 34 | return f'{type(self).__name__}{ str(self.__dict__).replace("_Person__", "")}' 35 | 36 | 37 | 38 | 39 | 40 | person1 = Person() 41 | 42 | print(person1.get_name()) 43 | print(person1.get_age()) 44 | 45 | person2 = Person('James') 46 | 47 | print(person2.get_name()) 48 | print(person2.get_age()) 49 | 50 | 51 | person3 = Person('Hazel', 27) 52 | 53 | print(person3.get_name()) 54 | print(person3.get_age()) 55 | 56 | print('------------------') 57 | 58 | print(person1) 59 | print(person2) 60 | print(person3) 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /day03/inheritance.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | def __init__(self, name: str, age: int): 3 | self.name = name 4 | self.age = age 5 | 6 | def __str__(self): 7 | return f'{type(self).__name__}{self.__dict__}' 8 | 9 | 10 | class Employee(Person): 11 | 12 | def __init__(self, name: str, age: int, job_title: str, company_name: str = 'Unknown', salary: int = 0): 13 | super().__init__(name, age) # calling parent class' constructor 14 | self.job_title = job_title 15 | self.company_name = company_name 16 | self.salary = salary 17 | 18 | def work(self): 19 | print(f'{self.name} is working') 20 | 21 | 22 | class Developer(Employee): 23 | 24 | def work(self): 25 | print(f'{self.job_title} {self.name} is coding') 26 | 27 | 28 | class Teacher(Employee): 29 | 30 | def __init__(self, name: str, age: int, job_title: str = "Teacher", company_name: str = 'Unknown', salary: int = 0): 31 | super().__init__(name, age, job_title, company_name, salary) # calling parent class' constructor 32 | 33 | def work(self): 34 | print(f'{self.name} is teaching') 35 | 36 | 37 | 38 | 39 | employee1 = Employee('Hazel', 27, 'QA', 'Apple Inc') 40 | 41 | developer1 = Developer('Daniel', 35, 'Python Developer', 'Google Inc', 100_000) 42 | 43 | teacher = Teacher('Breanna', 45 ) 44 | 45 | print(employee1) 46 | print(developer1) 47 | print(teacher) 48 | 49 | employee1.work() 50 | developer1.work() 51 | teacher.work() 52 | 53 | 54 | -------------------------------------------------------------------------------- /day03/lists.py: -------------------------------------------------------------------------------- 1 | groceries_list = ['Eggs', 'Milk', 'Rice'] 2 | 3 | groceries_list.append('Chicken') 4 | groceries_list.extend( ('Beef', 'Oranges', 'Onion') ) 5 | 6 | print(len(groceries_list)) 7 | print(groceries_list) 8 | 9 | groceries_list[-2] = 'Cherry' 10 | 11 | print(groceries_list) 12 | 13 | print('------------------------------------') 14 | 15 | numbers_list = [10, 20, 30, 40, 50, 60, 70, 80] 16 | 17 | print(numbers_list) 18 | 19 | numbers_list[2:-2] = [300, 4000, 5, 60000] 20 | 21 | print(numbers_list) 22 | 23 | print('------------------------------------') 24 | 25 | characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] 26 | 27 | list1 = characters[2: -3] 28 | print(list1) 29 | 30 | list2 = characters[:4] 31 | print(list2) 32 | 33 | list3 = characters[2:] 34 | print(list3) 35 | 36 | characters[2:5] = ['C', "D", 'E', 'C', 'D', 'E', 'X', 'Y', 'Z'] 37 | 38 | print(characters) 39 | 40 | print('------------------------------------') 41 | 42 | names = ['Conor', 'Steve', 'Hazel', 'Breanna'] 43 | 44 | for x in names: 45 | print(x) 46 | 47 | print('------------------------------------') 48 | 49 | nums = [10, 20, 30, 40, 50, 60] 50 | 51 | for i in range(len(nums)): 52 | nums[i] = int(nums[i] / 5) 53 | 54 | print(nums) 55 | 56 | print('------------------------------------') 57 | 58 | nums = [10, 20, 30, 40, 50, 60] 59 | 60 | for i in reversed(range(len(nums))): 61 | print(nums[i]) 62 | 63 | print('------------------------------------') 64 | 65 | for x in nums[::-1]: 66 | print(x) 67 | 68 | print('------------------------------------') 69 | 70 | for x in reversed(nums): 71 | print(x) 72 | 73 | print('------------------------------------') 74 | 75 | i = 0 76 | 77 | while i < len(nums): 78 | print(nums[i]) 79 | i += 1 80 | 81 | print('------------------------------------') 82 | 83 | for i in range(1, 6): 84 | i += 2 85 | print(i) 86 | 87 | print('------------------------------------') 88 | 89 | nums = [60, 500, 10, 20, 15, 5, 0] 90 | 91 | # nums.sort() # ascending order 92 | 93 | nums.sort(reverse=True) 94 | 95 | print(nums) 96 | 97 | print('------------------------------------') 98 | 99 | list1 = [10, 20, 30, 40] 100 | 101 | # list1 = list( reversed(list1) ) 102 | 103 | list1.reverse() 104 | 105 | print(list1) 106 | 107 | print('------------------------------------') 108 | 109 | tuple_elements = ('Java', 'Python', 'C#', 'Ruby') 110 | 111 | list_elements = list(tuple_elements) 112 | list_elements[-2] = 'C++' 113 | list_elements.append('SWIFT') 114 | 115 | print(list_elements) 116 | 117 | tuple_elements = tuple(list_elements) 118 | 119 | print(tuple_elements) 120 | 121 | print('------------------------------------') 122 | 123 | list1 = [1, 2, 3, 4, 5] 124 | list2 = [1, 2, 3, 4, 5] 125 | 126 | print(list1 is list2) 127 | 128 | tuple1 = (1, 2, 3, 4, 5) 129 | tuple2 = (1, 2, 3, 4, 5) 130 | 131 | print(tuple1 is tuple2) 132 | 133 | 134 | print('------------------------------------') 135 | 136 | 137 | groceries_list = ['Eggs', 'Milk', 'Rice'] 138 | 139 | groceries_list.append('Chicken') 140 | groceries_list.extend(('Beef', 'Oranges', 'Onion')) 141 | 142 | print(groceries_list) 143 | 144 | groceries_list.remove('Beef') 145 | 146 | print(groceries_list) 147 | 148 | groceries_list.pop() 149 | 150 | print(groceries_list) 151 | 152 | groceries_list.pop(1) 153 | 154 | print(groceries_list) 155 | 156 | groceries_list.insert(2, 'Apple') 157 | 158 | print(groceries_list) 159 | 160 | 161 | print( groceries_list.index('Eggs')) 162 | 163 | nums = [1, 2, 3, 4, 5, 1, 1, 1, 1, 1] 164 | 165 | print(nums.count(1)) 166 | 167 | 168 | print('--------------- Comprehensions -------------------------') 169 | 170 | nums = [] 171 | 172 | for x in range(1, 51): 173 | nums.append(x) 174 | 175 | print(nums) 176 | 177 | print('------------------------------------') 178 | 179 | """ 180 | divisible_by_5 = [] 181 | 182 | for x in nums: 183 | if x % 5 == 0: 184 | divisible_by_5.append(x) 185 | 186 | print(divisible_by_5) 187 | """ 188 | 189 | divisible_by_5 = tuple( [ x for x in nums if x % 5 == 0 ] ) 190 | 191 | print(divisible_by_5) 192 | 193 | print('------------------------------------') 194 | 195 | even_nums = [ x for x in nums if x % 2 == 0] 196 | odd_numbers = [x for x in nums if x % 2 != 0] 197 | 198 | print(even_nums) 199 | print(odd_numbers) 200 | 201 | print('------------------------------------') 202 | 203 | names = ['Python', 'Java', 'Java', 'JavaScript', 'java', 'JaVA', 'Ruby'] 204 | 205 | names = [x for x in names if x.lower() != 'java'] 206 | 207 | print(names) 208 | -------------------------------------------------------------------------------- /day04/dictionary.py: -------------------------------------------------------------------------------- 1 | employee1 = {} 2 | 3 | employee1['name'] = 'James' 4 | employee1['name'] = 'Daniel' 5 | employee1['age'] = 45 6 | employee1['salary'] = 60_000 7 | 8 | print(employee1) 9 | 10 | employee2 = { 11 | 'name': 'James', 12 | 'age': 29, 13 | 'salary': 80_000, 14 | 'full_time': False 15 | } 16 | 17 | print(employee2) 18 | print(employee2['name']) 19 | 20 | employee2['salary'] += 10000 21 | print(employee2) 22 | 23 | employee2.update({'age': 40}) 24 | 25 | print(employee2) 26 | 27 | employee2['full_time'] = True 28 | 29 | # employee2.update( {'full_time': True} ) 30 | 31 | print(employee2) 32 | 33 | employee2.pop('full_time') 34 | 35 | print(employee2) 36 | 37 | # print( help(dict.popitem) ) 38 | 39 | employee2.popitem() 40 | 41 | print(employee2) 42 | 43 | l = employee2.copy() 44 | 45 | print(l) 46 | 47 | print(employee2 is l) 48 | 49 | print('--------- Iterating Dictionary -----------------') 50 | 51 | employee3 = { 52 | 'name': 'Shay', 53 | 'age': 29, 54 | 'salary': 110_000, 55 | 'full_time': False, 56 | 'job_title': 'Developer', 57 | 'company': 'Apple Inc' 58 | } 59 | 60 | print(list(employee3.keys())) 61 | 62 | for key in employee3.keys(): 63 | print(f'{key} : {employee3[key]}') 64 | 65 | print('---------------------------------------') 66 | 67 | values = list(employee3.values()) 68 | 69 | print(values) 70 | 71 | for value in employee3.values(): 72 | print(value) 73 | 74 | print('---------------------------------------') 75 | 76 | for x in employee3.items(): # items(): returns a collection of tuples, in each tuple there will be two elements 77 | # print(x) 78 | print(f'{x[0]} : {x[1]}') 79 | 80 | print('---------------------------------------') 81 | 82 | students = { 83 | 'A01': { 84 | 'name': 'James', 85 | 'gender': 'Male', 86 | 'gpa': 3.5, 87 | 'subjects': ['Math', 'Physics'] 88 | }, 89 | 90 | 'A02': { 91 | 'name': 'Hazel', 92 | 'gender': 'Female', 93 | 'gpa': 3.8, 94 | 'subjects': ['Biology', 'Programming'] 95 | }, 96 | 97 | 'A03': { 98 | 'name': 'Yulia', 99 | 'gender': 'Female', 100 | 'gpa': 4, 101 | 'subjects': ['Chemistry', 'Programming'] 102 | } 103 | } 104 | 105 | print(students) 106 | 107 | students['A01']['gpa'] = 2.5 108 | 109 | print(students) 110 | 111 | # students['A02'].update( {'name': 'Daniel' , 'gender': 'Male'} ) 112 | students['A02']['name'] = 'Daniel' 113 | students['A02']['gender'] = 'Male' 114 | 115 | print(students) 116 | 117 | students['A03']['subjects'][1] = 'Biology' 118 | 119 | print(students['A03']) 120 | 121 | print('---------------------------------------------') 122 | 123 | 124 | for x in students.items(): 125 | print(x[1]) 126 | for y in x[1]: 127 | print(y) 128 | 129 | print('---------------------------------------------') 130 | 131 | for value in students.values(): 132 | print(value) 133 | for item in value.items(): 134 | print(item[1]) 135 | 136 | 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /day04/exceptions.py: -------------------------------------------------------------------------------- 1 | try: 2 | num = 10 / 0 3 | print(num) 4 | except: 5 | print('Something went wrong') 6 | else: 7 | print('Nothing went wrong') 8 | finally: 9 | print('finally block') 10 | 11 | print('Test Completed') 12 | 13 | print('----------------------------') 14 | 15 | tuple1 = (10, 20, 30, 40) 16 | 17 | try: 18 | print(tuple1[2]) 19 | except: 20 | print('The index number is too large') 21 | else: 22 | print('The index number is valid') 23 | 24 | print('---------------------------------------------') 25 | 26 | try: 27 | raise FileNotFoundError('No such a file') 28 | except SyntaxError: 29 | print("It is a syntax error") 30 | except OSError: 31 | print('It is an operating system error') 32 | except ArithmeticError: 33 | print('It is an arithmetic error') 34 | finally: 35 | print('Finally block') 36 | 37 | 38 | print('-------------------------------------') 39 | 40 | raise LookupError('Invalid entry') 41 | 42 | 43 | 44 | """ 45 | Java 46 | throw: used for manually throwing exception 47 | throws: for exception handings (in method signature) 48 | """ 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /day04/extras.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | 3 | print('------------------closure-----------------') 4 | 5 | 6 | def display_message(): 7 | def method(): 8 | print('Hello World') 9 | print('I Love Python') 10 | 11 | method() 12 | method() 13 | method() 14 | method() 15 | 16 | 17 | display_message() 18 | 19 | print('------------') 20 | 21 | 22 | def display_palindromes(strings: list): 23 | def is_palindrome(s: str) -> bool: 24 | return s[::-1].lower() == s.lower() 25 | 26 | for s in strings: 27 | if is_palindrome(s): 28 | print(s) 29 | 30 | 31 | print('-----------------------map-------------') 32 | 33 | nums = [10, 20, 30, 40, 50, 60, 70, 80] 34 | 35 | nums = list(map(lambda x: x * 10, nums)) 36 | 37 | print(nums) 38 | 39 | names = ['Java', 'JAVA', 'java', 'ruby', 'swift', 'CyDeO', 'javaSCRipt'] 40 | 41 | names = list(map(lambda s: str(s).upper(), names)) 42 | 43 | print(names) 44 | 45 | dictionary = {'x': 100, 'y': 200, 'z': 300} 46 | 47 | dictionary = dict(map(lambda t: (t[0], t[1] * 10), dictionary.items())) 48 | 49 | print(dictionary) 50 | 51 | print('--------------------filter------------------') 52 | 53 | nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 54 | 55 | # nums = [ x for x in nums if x % 5 ==0] 56 | 57 | nums = list(filter(lambda x: x % 5 == 0, nums)) 58 | 59 | print(nums) 60 | 61 | 62 | names = ['Java', 'JAVA', 'java', 'ruby', 'swift', 'CyDeO', 'javaSCRipt'] 63 | 64 | # names = [a for a in names if not a.lower().startswith('j')] 65 | 66 | names = list( filter(lambda x: not str(x).lower().startswith('j'), names ) ) 67 | 68 | print(names) 69 | 70 | 71 | print('--------------------reduce------------------') 72 | 73 | list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] 74 | 75 | print( reduce( lambda x, y: x+y , list1) ) 76 | 77 | 78 | list2 = ['Java', 'Python', 'C#', 'Ruby'] 79 | 80 | print( reduce( lambda x, y: f'{x} {y}' , list2 ) ) 81 | 82 | 83 | -------------------------------------------------------------------------------- /day04/file_handlings.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | path = 'files/Test.txt' 4 | 5 | text_file = open(path, 'r') 6 | 7 | print( text_file.read() ) 8 | 9 | 10 | text_file.close() 11 | 12 | """ 13 | print(text_file.readline()) 14 | print(text_file.readline()) 15 | print(text_file.readline()) 16 | """ 17 | 18 | print('----------------------------------') 19 | 20 | path = 'files/Test2.txt' 21 | 22 | text_file2= open(path, 'w') 23 | 24 | text_file2.write('This is a new text file\nPython created this file') 25 | 26 | text_file2.close() 27 | 28 | print('----------------------------------') 29 | 30 | os.remove(path) 31 | 32 | -------------------------------------------------------------------------------- /day04/files/Test.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "John Doe", 3 | "age": 43, 4 | "address": { 5 | "street": "10 Downing Street", 6 | "city": "London" 7 | }, 8 | "phones": [ 9 | "+44 1234567", 10 | "+44 2345678" 11 | ] 12 | } -------------------------------------------------------------------------------- /day04/files/Test.txt: -------------------------------------------------------------------------------- 1 | Wooden Spoon 2 | Python Programming 3 | Cydeo School -------------------------------------------------------------------------------- /day04/files/Test2.json: -------------------------------------------------------------------------------- 1 | { 2 | "A01": { 3 | "name": "James", 4 | "gender": "Male", 5 | "gpa": 3.5, 6 | "subjects": [ 7 | "Math", 8 | "Physics" 9 | ] 10 | }, 11 | "A02": { 12 | "name": "Hazel", 13 | "gender": "Female", 14 | "gpa": 3.8, 15 | "subjects": [ 16 | "Biology", 17 | "Programming" 18 | ] 19 | }, 20 | "A03": { 21 | "name": "Yulia", 22 | "gender": "Female", 23 | "gpa": 4, 24 | "subjects": [ 25 | "Chemistry", 26 | "Programming" 27 | ] 28 | } 29 | } -------------------------------------------------------------------------------- /day04/final_keyword.py: -------------------------------------------------------------------------------- 1 | from typing import final 2 | 3 | pi: final = 3.14 4 | 5 | pi = 3.5 6 | 7 | 8 | @final 9 | class Animal: 10 | pass 11 | 12 | 13 | class Dog(Animal): 14 | pass 15 | 16 | 17 | class Employee: 18 | 19 | @final 20 | def work(self): 21 | print('Working') 22 | 23 | 24 | class Driver(Employee): 25 | 26 | def work(self): 27 | print('Driving') 28 | 29 | 30 | class Person: 31 | 32 | def __init__(self, age: int): 33 | self.__age = age 34 | 35 | @property 36 | def person_age(self): 37 | return self.__age 38 | 39 | @person_age.setter 40 | def person_age(self, value): 41 | raise RuntimeError(f' age is constant, can not be changed') 42 | 43 | 44 | person1 = Person(10) 45 | 46 | print(person1.person_age) 47 | 48 | # person1.person_age = 100 49 | 50 | print(person1.person_age) 51 | 52 | print(person1.__age) 53 | -------------------------------------------------------------------------------- /day04/polymorphism.py: -------------------------------------------------------------------------------- 1 | from day03.abstraction2 import Shape, Square, Rectangle 2 | from day03.inheritance import Person 3 | 4 | shape1: Shape = Square(5) 5 | 6 | shape2: Shape = Rectangle(3, 4) 7 | 8 | 9 | def display_area(shape: Shape): # parameter's type is restricted to shape objects ONLY 10 | print(f'the {shape.name}\' area is {shape.area()} ') 11 | 12 | 13 | display_area( shape1 ) 14 | display_area( shape2) 15 | 16 | 17 | person1 = Person('James', 35) 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /day04/read_write_json.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | 4 | path = 'files/Test.json' 5 | 6 | jason_file = open(path, 'r') 7 | 8 | dictionary = json.load(jason_file) 9 | 10 | print(dictionary) 11 | print(type(dictionary)) 12 | 13 | for x in dict(dictionary).keys(): 14 | print(x) 15 | 16 | jason_file.close() 17 | 18 | print('------------ Write Json file----------------------') 19 | 20 | 21 | students = { 22 | 'A01': { 23 | 'name': 'James', 24 | 'gender': 'Male', 25 | 'gpa': 3.5, 26 | 'subjects': ['Math', 'Physics'] 27 | }, 28 | 29 | 'A02': { 30 | 'name': 'Hazel', 31 | 'gender': 'Female', 32 | 'gpa': 3.8, 33 | 'subjects': ['Biology', 'Programming'] 34 | }, 35 | 36 | 'A03': { 37 | 'name': 'Yulia', 38 | 'gender': 'Female', 39 | 'gpa': 4, 40 | 'subjects': ['Chemistry', 'Programming'] 41 | } 42 | } 43 | 44 | jason_file = open('files/Test2.json', 'w') 45 | 46 | json_object = json.dumps(students, indent=3) # converting dictionary object to json object 47 | 48 | jason_file.write(json_object) 49 | 50 | 51 | """ 52 | Web Automation: 53 | BeautifulSoup4 54 | request 55 | pytest 56 | robot 57 | 58 | Web Development: 59 | Django 60 | FastAPI 61 | flask 62 | ... 63 | """ 64 | 65 | -------------------------------------------------------------------------------- /day04/sets.py: -------------------------------------------------------------------------------- 1 | 2 | unique_element = set() 3 | 4 | unique_element.add(10) 5 | unique_element.add(10) 6 | unique_element.add(10) 7 | unique_element.add(20) 8 | unique_element.add(20) 9 | 10 | print( type(unique_element)) 11 | print(unique_element) 12 | 13 | #print(unique_element[1]) 14 | 15 | #print( unique_element[1:] ) 16 | 17 | unique_element.remove(10) 18 | 19 | print(unique_element) 20 | 21 | unique_element.update( (1, 2, 3, 4, 5,1 ,2 ,3 , 4, 5) ) 22 | 23 | # print( help(set.update) ) 24 | 25 | print(unique_element) 26 | 27 | n = unique_element.pop() 28 | 29 | print(unique_element) 30 | 31 | print(n) 32 | 33 | # print( help( str.istitle ) ) 34 | 35 | 36 | 37 | 38 | print('-------------difference-----------------------') 39 | 40 | # difference(): compares two sets and returns a new set which contains the items that only exist in first set 41 | s1 = {'Java', 'Python', 'C#'} 42 | s2 = {'Ruby', 'Java', 'C++', 'Swift'} 43 | 44 | s3 = s1.difference(s2) 45 | 46 | print(s3) 47 | 48 | 49 | print('----------------------intersection-------------------') 50 | 51 | # intersection(): compares two sets and returns a new set which contains the common elements of two sets 52 | set1 = {'Java', 'Python', 'C#', 'Cydeo'} 53 | set2 = {'C++', 'Ruby', 'Swift', 'Java', 'Python'} 54 | 55 | set3 = set1.intersection(set2) 56 | 57 | print(set3) 58 | 59 | 60 | print('------------------different_update-------------------') 61 | 62 | # different_update(): removes the elements of the first Set that exist in the second Set 63 | a1 = {'Book', 'Pen', 'Apple', 'Cherry', 'Coffee'} 64 | a2 = {'Book', 'Apple', 'Banana', 'Grape', 'TV'} 65 | 66 | a1.difference_update(a2) 67 | print(a1) 68 | 69 | 70 | print('---------------intersection_update----------------------') 71 | 72 | # intersection_update(): removes the uncommon elements of first & second sets 73 | b1 = {'Cydeo', 'Python', 'Java', 'C#', 'Wooden Spoon', 'Ruby', 'Swift'} 74 | b2 = {'Wooden Spoon', 'Python', 'Cydeo'} 75 | 76 | b1.intersection_update(b2) 77 | 78 | print(b1) 79 | 80 | 81 | 82 | 83 | print('------------------symmetric_update-------------------') 84 | 85 | # symmetric_difference(): Compares two sets and returns a new set which contains all elements except the common once 86 | e1 = {'Apple', 'Banana', 'Cherry'} 87 | e2 = {'Grape', 'Strawberry', 'Banana', 'Mango', 'Lemon', 'Apple'} 88 | 89 | e3 = e1.symmetric_difference(e2) 90 | 91 | print(e3) 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | --------------------------------------------------------------------------------