Skip to content

Commit 9050828

Browse files
This files were created for the Advanced python tutorial youtube series. Feel free to copy or download or fork the repo.
0 parents  commit 9050828

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1447
-0
lines changed
2.37 KB
Binary file not shown.
1.72 KB
Binary file not shown.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Dec 3 10:40:38 2021
4+
5+
@author: mjach
6+
"""
7+
8+
'''
9+
Strong type of polymorphism
10+
11+
Abstraction
12+
13+
- OOP abstraction is one of the main principle out of four.
14+
- Inheritance
15+
- Encapsulation
16+
- Polymorphism
17+
- Abstraction
18+
- with abstraction we hide all the data to increase the efficiency of the program and reduce the program complexity
19+
- at the sane time we hide the implementation details and have to accessed through explicity
20+
- through abstraction we decleare a abstraction method but we do not implement it
21+
- abstraction class can have more than one abstract method.
22+
- abstract class can not be instantiated. majority of the concept similar to Java
23+
- After saying all of these actually python does not have real abstract classes and methods like Java but it can be implement or achived
24+
- In python abstract class can be implemented using the Abstract Base Class (ABC) module. The module name is abc
25+
- when you implement ABC(Abstract Base Classes) you must
26+
implement its properties and methods
27+
'''
28+
from math import pi
29+
from abc import ABC, abstractmethod
30+
31+
class Shape(ABC):
32+
33+
@abstractmethod
34+
def find_area(self):
35+
pass
36+
37+
@abstractmethod
38+
def find_perimeter(self):
39+
pass
40+
41+
42+
class Circle(Shape):
43+
44+
def __init__(self, radius):
45+
self.radius = radius
46+
47+
def find_area(self):
48+
#return pi * self.radius ** 2
49+
print('Area of the circle:',pi * self.radius ** 2)
50+
51+
def find_perimeter(self):
52+
#return 2 * pi * self.radius
53+
print('Perimeter of the circle:',2 * pi * self.radius)
54+
55+
class Rectange(Shape):
56+
57+
def __init__(self, height, width):
58+
self.height = height
59+
self.width = width
60+
61+
def find_area(self):
62+
return self.height * self.width
63+
64+
def find_perimeter(self):
65+
return 2 * (self.height + self.width)
66+
67+
circle = Circle(3)
68+
# print('Area of the circle :', circle.find_area())
69+
# print('Parimeter of the circle:', circle.find_perimeter())
70+
circle.find_area()
71+
circle.find_perimeter()
72+
73+
rectangle = Rectange(4, 6)
74+
print('Area of the rectange:', rectangle.find_area())
75+
print('Perimeter of the rectangle:', rectangle.find_perimeter())
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Dec 3 10:44:22 2021
4+
5+
@author: mjach
6+
"""
7+
8+
from abc import ABC, abstractmethod
9+
10+
11+
class Vehicle(ABC):
12+
@abstractmethod
13+
def drive(self, distance):
14+
pass
15+
16+
@abstractmethod
17+
def refuel(self, fuel):
18+
pass
19+
20+
21+
@staticmethod
22+
def can_drive(consumption, fuel):
23+
return consumption <= fuel
24+
# result = consumption - fuel
25+
# return result
26+
27+
@staticmethod
28+
def fuel_level(consumption,fuel):
29+
return 'Current fuel level:', consumption - fuel
30+
31+
32+
class Car(Vehicle):
33+
ADD_CONSUMPTION = 0.9
34+
#ADD_CONSUMPTION = 1.0
35+
36+
def __init__(self, fuel_quantity, fuel_consumption):
37+
self.fuel_quantity = fuel_quantity
38+
self.fuel_consumption = fuel_consumption
39+
40+
def drive(self, distance):
41+
consumption = distance * (self.fuel_consumption + Car.ADD_CONSUMPTION)
42+
if self.can_drive(consumption, self.fuel_quantity):
43+
self.fuel_quantity -= consumption
44+
45+
def refuel(self, fuel):
46+
self.fuel_quantity += fuel
47+
48+
def fuel_level(self):
49+
#return self.fuel_quantity
50+
self.fuel_quantity = self.fuel_quantity - self.fuel_consumption
51+
return self.fuel_quantity
52+
53+
car = Car(20, 5)
54+
print('Starting fuel quantity:',car.fuel_quantity)
55+
car.drive(3)
56+
print('After driving fuel quantity:',car.fuel_level())
57+
car.refuel(10)
58+
print('After refiliing fuel level:',car.fuel_quantity)
59+
#car.drive(33)
60+
print('fuel level :',car.fuel_level())
61+
#car.drive(10)
62+
#print('Current fuel level :',car.fuel_level())
63+
class Truck(Vehicle):
64+
ADD_CONSUMPTION = 1.6
65+
REFUEL_PROBLEM = 0.95
66+
67+
def __init__(self, fuel_quantity, fuel_consumption):
68+
self.fuel_quantity = fuel_quantity
69+
self.fuel_consumption = fuel_consumption
70+
71+
def drive(self, distance):
72+
consumption = distance * (self.fuel_consumption + Truck.ADD_CONSUMPTION)
73+
if self.can_drive(consumption, self.fuel_quantity):
74+
self.fuel_quantity -= consumption
75+
76+
def refuel(self, fuel):
77+
self.fuel_quantity += fuel * Truck.REFUEL_PROBLEM
78+
79+
80+
81+
82+
83+
print("----------")
84+
85+
# truck = Truck(100, 15)
86+
# truck.drive(5)
87+
# print(truck.fuel_quantity)
88+
# truck.refuel(50)
89+
# print(truck.fuel_quantity)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sun Dec 5 10:30:46 2021
4+
5+
@author: mjach
6+
"""
7+
8+
from abc import ABC, abstractmethod
9+
10+
class Animal(ABC):
11+
@abstractmethod
12+
def eat(self):
13+
pass
14+
@abstractmethod
15+
def sleep(self):
16+
pass
17+
@abstractmethod
18+
def play(self):
19+
pass
20+
21+
class Cat(Animal):
22+
def eat(self):
23+
print('Cat is eating now..')
24+
25+
def sleep(self):
26+
print('Kity is leeping now')
27+
28+
def play(self):
29+
print('Kitty is very happy and playing now')
30+
31+
class Dog(Animal):
32+
def eat(self):
33+
print('charly is eating now..')
34+
35+
def sleep(self):
36+
print('charly is leeping now')
37+
38+
def play(self):
39+
print('charly is very happy and playing now')
40+
41+
42+
cat = Cat()
43+
cat.eat()
44+
cat.play()
45+
cat.sleep()
46+
47+
print('------------------')
48+
dog = Dog()
49+
dog.eat()
50+
dog.play()
51+
dog.sleep()

OOP/circle.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sun Nov 28 09:40:19 2021
4+
5+
@author: mjach
6+
"""
7+
8+
'''
9+
What is Object?
10+
11+
An object can be a function, a variable, a property, a class… everything in Python is an object.
12+
13+
- to create an object we need an __init__ method. if we don't use it by default python use __init__ method to create an object.
14+
- you can pass instance variable through __init__ method to create an object.
15+
'''
16+
'''
17+
What is a Class?
18+
19+
-Class is a blue print of a an Object? A typr of a something e.g car class, animal class
20+
-Every thing or object in Python is an instance of a class.
21+
- The number 42 is an instance of the class int.
22+
-The string Hello, world is an instance of the str (or string) class.
23+
- From a class you can create a new types of object.
24+
25+
What is class variable?
26+
- class variable shared by all instance of the class.
27+
28+
What is an instance?
29+
30+
- if car is a type of something you can think of instance as a specific things like my_VWW is a type of car.
31+
- Both class and instance can have there own variables and methods.
32+
33+
What is self keyword?
34+
35+
- self is used inside the classes to refer its bound to that specific class all of its instance variables and methods.
36+
37+
What is method?
38+
- class can also have a method which is a behaviour of an object.
39+
- that is belong to the object.
40+
41+
-variable is an attribute of an object.
42+
43+
What is __init__ function/method?
44+
-it is a constructor?
45+
-called magic method.
46+
- methods that are used underscores with bracket called magic method - e.g - __init__(), __gt__(), __ge__(), __str__(), __repr__()
47+
48+
What is a static method?
49+
- static method don''t know about class or instance
50+
- you can create static method without self keyword
51+
- method can be trun to static method just add @staticmethod on top of the method.
52+
- you can call static method either using by class or by the instance
53+
'''
54+
55+
from math import pi
56+
57+
class Circle():
58+
59+
def __init__(self, radius):
60+
self.radius = radius
61+
62+
def find_area(self):
63+
return pi * self.radius ** 2
64+
65+
def find_perimeter(self):
66+
return 2 * pi * self.radius
67+
68+
69+
circle_one = Circle(3)
70+
print('Area of the circle:', circle_one.find_area())
71+
print('Perimeter of the circle:', circle_one.find_perimeter())
72+
73+
circle_two = Circle(6)
74+
print('Area of the circle:', circle_two.find_area())
75+
print('Perimeter of the circle:', circle_two.find_perimeter())
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Tue Nov 30 10:16:15 2021
4+
5+
@author: mjach
6+
"""
7+
'''
8+
__repr__ is to find out the representation of an object official way
9+
__str__ is to find out the informal way to representation of an object
10+
11+
'''
12+
class Dog():
13+
14+
def __init__(self, name, breed, age):
15+
self.name= name
16+
self.breed = breed
17+
self.age = age
18+
19+
def __repr__(self):
20+
return 'Belong to class of {}, name of {}, breed of the dog{}, age of the dog{}'.format(
21+
self.__class__.__bases__[0].__name__,self.__class__.__name__, self.breed, self.age)
22+
23+
def __str__(self):
24+
return 'This dog name is {}, breed typr is {}, age of the dog is {}'.format(self.name, self.breed, self.age)
25+
26+
dog = Dog('Rita', ' Golden', ' 10')
27+
print(dog)
28+
#print(repr(dog))
29+
#print(dog.__repr__())

0 commit comments

Comments
 (0)