-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP.py
More file actions
221 lines (177 loc) · 6 KB
/
Copy pathOOP.py
File metadata and controls
221 lines (177 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Class is basically the blueprint for objects.
# Object are the production generated out pf class
#---We cannot directly use class in our code, Thus we need to use objects---
'''Syntax: class class_Name:
related_infos = "related_info"
variable = class_name()------------------>object
print(variable.related_infos)...'''
#ex:
'''class Car:
color = "blue"
brand = "Porsche"
car1 = Car()
car2 = Car()
print(car1.color)
print(car2.brand)'''
#__init__() function --------------> constructor
#Parameterized constructor-----> These are the constructor with arguments or parameters.
'''class Students:
def __init__(self,name,roll_no): #------------> self can be changed to any name but self is good!
self.name = name
self.roll = roll_no
s1 = Students("Moon",96)#--------> these stored data or variables are known as attributes
print(s1.name, s1.roll)
s2 = Students("Asplin",1)
print(s2.name, s2.roll)'''
#default constructor:
'''class Anything:
def __init__(self):
pass #---------> Not used much'''
'''ATTRIBUTES
1) class.attr = class attributes
2) obj.attr = instance attributes '''
#ex:
'''class Students:
college_name = "Great World children's association"
name = "Anonymous"#----> priority : obj attr> class attr
def __init__(self,name,roll_no):
self.name = name
self.roll_no = roll_no
s1 = Students("Moon", 96)
print("Name:",s1.name,"\nRoll no.:",s1.roll_no,"\nCollege name:",s1.college_name)'''
#Methods :
'''class Students:
def __init__(self,name):
self.name = name
def welcome(self):#-------> metod
print("Welcome student",self.name)
s1 = Students("Asplin")
print(s1.welcome())'''
#ex : create student class that takes name and marks of three subjects as argumates in constructor. then create a method to print their average
'''class Student:
def __init__(self,name,marks1,marks2,marks3):
self.marks1 = marks1
self.marks2 = marks2
self.marks3 = marks3
self.name = name
def avg(self):
average = int(((self.marks1 + self.marks2 + self.marks3)/3))
print(average)
s1 = Student('Asplin',96,99,95)
s1.avg()
#or
class Student:
def __init__(self,name,marks):
self.name = name
self.marks = marks
def get_avg(self):
sum = 0
for val in self.marks:
sum += val
print("Hello there",self.name,"your average score is",int(sum/len(self.marks)))
s1 = Student("Asplin",[99,98,99,97,98,99])#------>object atribute
s1.get_avg()
#Staric Methods: ---> methods not using self parameter. (works at class level)
class Student:
@staticmethod #decorator
def college():
print("Hello!")
FOUR PILLARS OF OOP!
1)Abstraction
2)Encapsulation
3)Inheritence
4)Polymorphism
#Abstraction: not clear/unnnecessray info Hidden from user.(basically everything we were writing inside the code)
#define :- Hiding the implementation details of a class and only showing the essential features to the user.
class Car:
def __init__(self):
self.acc = False
self.brk = False
self.clth = False
def start(self):
self.acc = True
self.clch = True
print("Car Started")
car1 = Car()
car1.start()
#Encapsulation: Wrapping data and related funtions into a single unit. (everything we were wtiting so far in a class was encapsulated)
#ex:- create an account class with 2 attributes-balance and account no. create methods to debit,credit and printing balance
class Account:
def __init__(self,bal,acc):
self.bal = bal
self.acc = acc
def debit(self, amtd):
self.bal -= amtd
print(" Rs.",amtd,"was debited from your account no.",self.acc,"\n","total balance = ",self.get_balance() )
def credit(self, amtc):
self.bal += amtc
print(" Rs.",amtc,"was credited in your account no.",self.acc,"\n","total balance = ",self.get_balance() )
def get_balance(self):
return self.bal
s1 = Account(12000, 859437803)
s1.debit(1000)
s1.credit(5000)'''
#del is a key word to delete some specific properties or deleting the whole object.
#ex: del s1-------->for deleting object
#ex: del s1.name--->for deleting object name
'''class Student:
def __init__(self,name):
self.name = name
s1 = Student("Moon")
print(s1.name)
del s1.name
print(s1.name)'''
#Private(like) attribute & methods
# to privatise any information we simply put two unerscores at the start.
#ex:
'''class Account:
def __init__(self, acc_no, acc_pass):
self.acc_no = acc_no
self.__acc_pass = acc_pass
def reset_pass(self):
print(self.__acc_pass)
acc1 = Account("123456","asdf")
print(acc1.acc_no)
#
# print(acc1.acc_pass)'''
#Inheritance: when one class (child / derived) derives the properties and methods of another class (parents / base).
#Types: 1) Single Inheritance 2)Multi-level Inheritance 3) Multiple Inheritance
#ex: Single class:- from single parent to single child
'''class Car:
@staticmethod
def start():
print("Car started...")
class New(Car):
#same code as car + modifications
def __init__(self, name):
self.name = name
print(self.name)
car1 = New("fortuner")
print(car1.start())'''
#Multi-level Inheritance:- from grandparents and parents to child base(parent)--->derived1 (child / parent)--->derived(child)...
'''class Car:
@staticmethod
def start():
print("Car started...")
class New(Car):
#same code as car + modifications
def __init__(self, name):
self.name = name
print(self.name)
class New_Car(New):
def __init__(self, fuel):
self.fuel = fuel
car1 = New_Car("diesel")
print(car1.start())'''
#Multiple inheritence:- child class derived from more than one parent class
'''class A:
str1 = "Welcome to class A"
class B:
str2 = "Welcome to class B"
class C(A,B):
str3 = "Welcome to class C"
c1 = C()
print(c1.str1)
print(c1.str2)'''
lst = [1,2,3,4,5]
print(lst[5])