Skip to content

Commit 193bd6d

Browse files
committed
OOP Basic added
1 parent 9b781bf commit 193bd6d

File tree

9 files changed

+600
-0
lines changed

9 files changed

+600
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Object-Oriented Programming (OOP)
2+
# OOP is a programming languages paradigm, where classes and objects are used.
3+
# Objects in OOP are the same as any system components, remember the shoe shop
4+
# where the shop is a class (main object, system), while the salesperson, other
5+
# employees, the goods, etc., are all objects (components) inside the shop
6+
# serve the main purpose of the shoe shop, and that is selling shoes.
7+
# OOP has similarities to the shoe shop, where the class has a purpose and the
8+
# objects inside the class serve that purpose, and from the class, an object
9+
# can be created
10+
# Let's take an example, the list class:
11+
12+
nums = [7, 5, 1, 6, 4, 3, 2]
13+
14+
# "nums" is an object created from the list class
15+
16+
# print(type(nums))
17+
18+
# The list class has many methods or functions, when using these methods with
19+
# the list object (nums), we can modify or change the list object (nums), to
20+
# create the final list, which is the class's main purpose.
21+
nums.append(9)
22+
nums.sort()
23+
num_pop = nums.pop()
24+
# print(nums)
25+
# print(num_pop)
26+
27+
# OOP is used to bind the objects (data and functions) work together as a
28+
# single unit (class), in which these objects inside the class are isolated
29+
# from the outside scope, and used to serve the purpose of this class.
30+
31+
# In OOP there are six concepts, that should be taken in account:
32+
33+
# The first two concepts are:
34+
# 1. Classes
35+
# 2. Objects
36+
37+
# The other four concepts are called the four pillars of object-oriented
38+
# programming:
39+
# 1. Inheritance
40+
# 2. Polymorphism
41+
# 3. Encapsulation
42+
# 4. Abstraction
43+
44+
# When writing classes, same as functions, your code will be:
45+
# 1. Clean.
46+
# 2. Readable.
47+
# 3. D.R.Y Don't Repeat Yourself.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# What are a class and objects?
2+
# 1. A class is a category of things (objects) that have some properties in
3+
# common and these properties differentiate it from other classes.
4+
# Let's take cars for example, underneath the cars there are the brands,
5+
# models, sizes, speed, etc., so the car is the general category or class (main
6+
# object) that is used to create objects related to it, such as brands, sizes,
7+
# number of wheels, speeds, etc., each one of them is an object that can be
8+
# created from this car class.
9+
10+
# In Python classes are the blueprint or code template to create objects.
11+
# To define a class, use the class keyword and then choose a class name, it is
12+
# recommended (not compulsory) for the words in the class name to start with
13+
# capital letters, without using an underscore to separate the words, if more
14+
# than one word is used.
15+
# Define a class:
16+
class ClassName:
17+
# Class attributes (class variables)
18+
number = 10
19+
20+
# Instance (object) attributes (instance variables)
21+
def __init__(self):
22+
pass
23+
24+
# Behavior or object (instance) method
25+
def method_class(self):
26+
pass
27+
28+
29+
# Create an instance or object
30+
obj = ClassName()
31+
# print(obj.number)
32+
# print(obj.method_class())
33+
34+
# Creating an object or class instance is done by assigning the class to a
35+
# variable.
36+
37+
38+
# 2. Objects, as mentioned before everything in Python is an object, and
39+
# objects are instances of a class, when a list is created as shown below:
40+
numbers = [1, 2, 3]
41+
42+
# "numbers" is an instance or object of the list class, it is an object of the
43+
# class and has a location in memory.
44+
45+
# Objects are collections of methods (behavior) and attributes (state).
46+
# Below are the object properties:
47+
48+
# 1. State: It is the instance (object) attributes (instance variables), which
49+
# reflects the object's properties or characteristics.
50+
51+
# For example in the Shoe Shop, the defined attributes or states:
52+
# Number of shoes
53+
# Shoe type
54+
# Employee name
55+
# Salespeople name
56+
# color
57+
# size
58+
# etc.
59+
60+
# When using the above attributes to create objects, each object will have a
61+
# different attribute, as shown below.
62+
63+
# Shoe type = “heels”
64+
# Quantity = 50
65+
# Salesperson name = “Tom”
66+
# color = "red"
67+
# size = "36"
68+
# The above attributes or states of the "heel1" object reflect the properties
69+
# and characteristics for this object.
70+
71+
# If other attributes are created as shown below:
72+
# Shoe type = “heels”
73+
# Quantity = 50
74+
# Salesperson name = “Sam”
75+
# color = "red"
76+
# size = "39"
77+
78+
# This is another object, although it shares some properties with "heel1"
79+
# object but they are the state or properties of the second object.
80+
81+
# An important note that even the above objects share some properties and
82+
# their states have common representation, none of these objects shares
83+
# the memory location, meaning the shoe type in the first object has
84+
# a different memory location than the shoe type of object2, because they are
85+
# two different objects.
86+
87+
# 2. Behavior: It is the object (instance) method, it is how the objects act
88+
# on itself or reacts to other objects, changing or using the object states or
89+
# attribute.
90+
# Thus, the behavior of an object is a way to perform actions.
91+
92+
# In the Shoe Shop, the object "heel1" has many attributes, using these
93+
# attributes to:
94+
# Calculate Cost
95+
# Calculate Profit
96+
# Check if the shoe type is popular
97+
# etc.
98+
99+
# The above operations are the behavior and actions (methods) the object does
100+
# on the states (attributes) to get some results.
101+
102+
# 3. Identity: Every object should have a unique name, used to give it and
103+
# identify and differentiate it from other objects.
104+
# In the above example, "heel1" is a unique name for this object.
105+
106+
# Note: the object's state or attributes will be defined using the init dunder
107+
# method which will be discussed in detail later.
108+
# The objects variables are called the instance attributes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# As discussed before a class is used to create an objects, this is how classes
2+
# are different from functions, the function is used to perform one task, for
3+
# example to perform numbers addition, subtraction, division, multiplication,
4+
# different functions should be written.
5+
6+
# While writing one class will be sufficient to perform the above operations.
7+
# Let's create a simple class that does math operations.
8+
# How are classes defined?
9+
# 1. Use the class keyword then give the class its name, after the name
10+
# parentheses can be used or left without parentheses.
11+
# 4. Create the attributes.
12+
# 3. Create the methods needed. Methods are the behavior of an object, where
13+
# actions can be executed, such as lists dictionaries, etc., methods.
14+
# Methods can be considered functions that belong to a class, they are
15+
# created inside the class, and they are linked to the class or the object
16+
# created from it.
17+
# To invoke or call a method, an object should be created object, then methods
18+
# Can be invoked.
19+
# Methods may have arguments and, the self-argument is required as the
20+
# first argument in a method.
21+
22+
class MathOperations:
23+
# class attributes (class variables)
24+
a = 4
25+
b = 5
26+
c = 6
27+
28+
# methods
29+
def addition(self):
30+
return self.a + self.b + self.c
31+
32+
def subtraction(self):
33+
return self.a - self.b - self.c
34+
35+
def multiplication(self):
36+
return self.a * self.b * self.c
37+
38+
def division(self):
39+
return self.a / self.b
40+
41+
42+
# This is how a class is created, let's execute this class.
43+
# Note: the self parameter will be discussed later in this section.
44+
45+
# To execute the class follow the below steps:
46+
# 1. Create an object from the class MathOperations:
47+
math_oper = MathOperations()
48+
# math_oper2 =MathOperations()
49+
# print(MathOperations())
50+
51+
# 2. Use this object math_oper to get the attributes:
52+
x = math_oper.a
53+
y = math_oper.b
54+
z = math_oper.c
55+
56+
# print(f"x = {x}", f"y = {y}", f"z = {z}", sep="\n")
57+
58+
# 3. Using the object math_oper to get results or perform actions:
59+
add = math_oper.addition()
60+
sub = math_oper.subtraction()
61+
div = math_oper.division()
62+
multi = math_oper.multiplication()
63+
# print(f"Addition = {add}")
64+
# print(f"Subtraction = {sub}")
65+
# print(f"Division = {div}")
66+
# print(f"Multiplication = {multi}")
67+
68+
# As shown above instead of writing four different functions for math
69+
# operations, we created one class that combines the four operations, and then
70+
# we created different objects to get the results, the result is clean,
71+
# readable and DRY code.
72+
# That doesn't mean that classes are better than functions, or vice versa,
73+
# because each one has its usage when writing the code and choosing the right
74+
# approach is the programmer's decision.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Two types of variables can be defined inside the class:
2+
# 1. Class variables or attributes are the variables that are defined inside
3+
# the class, and outside of any class method.
4+
# These variables can be shared across all the created objects, they belong to
5+
# the class itself not to any specific instance (object).
6+
# As discussed before when creating objects from a class each object has its
7+
# own state or attributes, which are different for each object.
8+
# This is not true for the class variables, different objects have the same
9+
# class variables and can be used with these objects.
10+
11+
class MathOperations:
12+
# class attributes (class variable2s)
13+
a = 4
14+
b = 5
15+
c = 6
16+
17+
# methods
18+
def addition(self):
19+
return self.a + self.b + self.c
20+
21+
# Another way of accessing the class variable is using the class name
22+
# because these attributes belongs to the class itself.
23+
def subtraction(self):
24+
return MathOperations.a - MathOperations.b - MathOperations.c
25+
26+
def multiplication(self):
27+
return self.a * self.b * self.c
28+
29+
def division(self):
30+
return self.a / self.b
31+
32+
33+
# The class attributes can be called using the class itself:
34+
# print(MathOperations.a)
35+
# print(MathOperations.b)
36+
# print(MathOperations.c)
37+
38+
# Creating two objects and using the class attributes give the same result.
39+
obj1 = MathOperations()
40+
# print(obj1.a)
41+
# print(obj1.subtraction())
42+
# print("")
43+
obj2 = MathOperations()
44+
# print(obj2.a)
45+
# print(obj2.subtraction())
46+
47+
48+
# 2. Instance variables or attributes are the objects' attributes or states,
49+
# and their values or states vary from object to object.
50+
# How can they be defined?
51+
# They can be defined using the __init__ constructor.
52+
53+
class MathOperations:
54+
# instance attributes or variables, __init__ constructor
55+
def __init__(self, a, b, c=0):
56+
self.a = a
57+
self.b = b
58+
self.c = c
59+
60+
# methods
61+
def addition(self):
62+
return self.a + self.b + self.c
63+
64+
def subtraction(self):
65+
return self.a - self.b - self.c
66+
67+
def multiplication(self):
68+
return self.a * self.b * self.c
69+
70+
def division(self):
71+
return self.a / self.b
72+
73+
74+
# 1. Creating the first object
75+
math_op_obj1 = MathOperations(2, 3, 5)
76+
add_obj1 = math_op_obj1.addition()
77+
sub_obj1 = math_op_obj1.subtraction()
78+
div_obj1 = math_op_obj1.division()
79+
multi_obj1 = math_op_obj1.multiplication()
80+
# print("Object1")
81+
# print(f"Addition object1 = {add_obj1}")
82+
# print(f"Subtraction object1 = {sub_obj1}")
83+
# print(f"Division object1 = {div_obj1}")
84+
# print(f"Multiplication object1= {multi_obj1}")
85+
# print("")
86+
87+
# 2. Creating the second object
88+
math_op_obj2 = MathOperations(1, 2)
89+
add_obj2 = math_op_obj2.addition()
90+
sub_obj2 = math_op_obj2.subtraction()
91+
div_obj2 = math_op_obj2.division()
92+
multi_obj2 = math_op_obj2.multiplication()
93+
# print("Object2")
94+
# print(f"Addition object1 = {add_obj2}")
95+
# print(f"Subtraction object1 = {sub_obj2}")
96+
# print(f"Division object1 = {div_obj2}")
97+
# print(f"Multiplication object1= {multi_obj2}")
98+
99+
100+
# Class variables and instance variables can be used together.
101+
# Let's consider a company that sells cars and we will create a class that
102+
# calculates the sales percentage for different car brands.
103+
104+
class Sales:
105+
# creating a class variable that can be shared with all objects
106+
company_name = "Luxury Cars"
107+
108+
# Creating the instance variables, by using the __init__ constructor
109+
def __init__(self, brand, total_brand_sold, total_cars_sold):
110+
self.brand = brand
111+
self.total_brand_sold = total_brand_sold
112+
self.total_cars_sold = total_cars_sold
113+
114+
def sales_percentage(self):
115+
sales_per = self.total_brand_sold/self.total_cars_sold * 100
116+
return f"{Sales.company_name} sales percentage for \
117+
{self.brand} brand = {int(sales_per)}%"
118+
119+
120+
sales_car1 = Sales("Car1", 20000, 100000)
121+
sales_car2 = Sales("Car2", 50000, 100000)
122+
# print(sales_car1.sales_percentage())
123+
# print(sales_car2.sales_percentage())

0 commit comments

Comments
 (0)