-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_Practical_Introduction_To_Python.py
More file actions
373 lines (246 loc) · 8.73 KB
/
A_Practical_Introduction_To_Python.py
File metadata and controls
373 lines (246 loc) · 8.73 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
class Student: #student class
perc_rise = 1.05
def __init__(self, first, last, marks): #initialize our instances or constructor
self.first = first
self.last = last
self.marks = marks
self.email = first + '.' + last + '@gmail.com'
def fullname(self): #funtion to print the fullname
return '{} {}'.format(self.first, self.last)
def apply_rise(self):
self.marks = int(self.marks * 1.05)
#CLASS INHERITANCE
class Dumb(Student): #Creating a subclass inheritance from the student class
perc_rise = 1.10 #percent raise in the Dumb class
def __init__(self, first, last, marks, prog_lang): # define an init function for the Dumb class
super().__init__(first , last, marks) # inherit from the student class
self.prog_lang = prog_lang #initialize the prog_lang
Std_1 = Dumb('ibrahim', 'suleiman', 60,'python')#creating an object for student 1
#Std_2 = Student('khalil', 'ibrahim', 90)#creating an object for strudent 2
print(Std_1.prog_lang)
#print(Std_1.perc_rise)
#print(help(Dumb))
#Std_2.apply_rise()
#print(Student.__dict__)
#print(Std_1.fullname())
#print(Std_2.fullname())
#python inheritance
#inheritance allows us to define a class that inherits all the methods and properties from another class.
#parent class and child class
class Person:
def __init__(self,fname,lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#use the person class to create an object, and then execute the printname method:
x = Person('ibrahim', 'suleiman')
x.printname()
#child class
#create a student class that will inherit properties from the person class
class Student(Person):
pass
#use the student class to create an object and then execute the printname method
x = Student('khalil', 'excel')
x.printname()
""" Add the __init__() function so far we have created a child class that inherits the properties and methods from its parent
we want to add the __init__()function to the child class of the pass keyword
"""
class Student(Person):
def __init__(self, fname, lname):
pass
#Add properties etc
"""
when you add the __init__() function, the child class will no longer inherit the parents __init__() function
note the childs __init__function overrides the inheritance of the parents __init__() function
"""
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
"""
Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready
to add functionality in the __init__() function
USe the super()function python also has a super() function that will make the child class inherit all the methods and properties from parent
"""
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
"""
by using the super() function you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent
Add properties
example add a property called graduationyear to the Student class:
"""
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
self.graduationyear = 2019
#In the example below, the year 2019 should be a variable, and passed into the Student class when creating objects. to do so add another parameter in the __init__() function:
#example: add a year parameter, and pass the correct year when creating objects:
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student('ibrahim', 'suleiman', 2016)
#Add methods
"""
Abstract Classes in Python
An abstract class can be considered as a blueprint for other classes, allows you to create
a set of methods that must be created within any child classes built from your abstract class
a class which contains one or abstract methods is called an abstract class.
An abstract method is a method that has declaration but not has any implementation.
How Abstract Base classes work
"""
#code 1: Python program showing abstract base class work
from abc import ABC, abstractmethod
class Polygon(ABC):
#abstract method
def no_of_sides(self):
pass
class Triangle(Polygon):
#overriding abstract method
def no_of_sides(self):
print("i have 3 sides")
class pentagon(Polygon):
#overriding abstract method
def no_of_sides(self):
print("I have 5 sides")
class hexagon(Polygon):
#overriding abstract method
def no_of_sides(self):
print("I have 6 sides")
class Quadrilateral(Polygon):
#overriding abstract method
def no_of_sides(self):
print("I have 4 sides")
#driver code
R = Triangle()
R.no_of_sides()
K = pentagon()
K.no_of_sides()
M = hexagon()
M.no_of_sides()
Q = Quadrilateral()
Q.no_of_sides()
#code 2: python program showing abstract base class work
from abc import ABC, abstractmethod
class Animal(ABC):
def move(self):
pass
class ibrahim(Animal):
def move(self):
print("I can walk")
class sheep(Animal):
def move(self):
print("I can walk with my four legs")
class snake(Animal):
def move(self):
print("I can crawl")
class Bird(Animal):
def move(self):
print("I can fly")
I = ibrahim()
I.move()
s = sheep()
s.move()
S = snake()
S.move()
B = Bird()
B.move()
#implementation through subclassing: by subclassing directly from the base, we can avoid the need to register the class
#explicitly. in this case, the python class management is used to recognize pluginImplementation as implementing the abstract PluginBase.
#python program showing implementation of abstract class through subclasssing
import abc
class parent:
def geeks(self):
pass
class child(parent):
def geeks(self):
print("child class")
print(issubclass(child,parent))
print(isinstance(child(), parent))
#ABSTRACT CLASS
from abc import ABC, abstractmethod #import the abstract class
class Employee(ABC): #the class employee inherit from the abstract class ABC
@abstractmethod #decoration for the abstract method
def calculate_salary(self, sal): #define an abstract method
pass
class Developer(Employee):
def calculate_salary(self, sal):
finalsalary = sal * 1.10
return finalsalary
emp_1 = Developer()
print(emp_1.calculate_salary(10000))
#python program to demonstrate instantiating a class
class Dog:
#a simple class attribute
attr1 = 'mamal'
attr2 = 'dog'
# a sample method
def disp(self):
print('I am a', self.attr1)
print('I am a', self.attr2)
#Driver code object instantiation
billy = Dog()
#Accessing class attributes and method through objects
print(billy.attr1)
billy.disp()
# The __init__ method
class Person:
#init method or constructor
def __init__(self, name):
self.name = name
#sample method
def say_hi(self):
print('hello my name is', self.name)
p = Person('ibrahim')
p.say_hi()
#Class and Instance variable
#Python program to show that the variables with a assigned in class declaration, are class variables and
#variables inside methods and constructors are instance variables
#class for computer science
class Dog:
#class variable
animal = 'dog'
#The init method or constructor
def __init__(self, breed, color):
#instance variable
self.breed = breed
self.color = color
#objects of CSStudent class
Rodger = Dog("pug", "brown")
Buzo = Dog("Bulldog", "black")
print('Rodger details:')
print('Rodger is a',Rodger.animal)
print('Breed:',Rodger.breed)
print('Color:',Rodger.color)
print('\nBuzo details:')
print('Buzo is a',Buzo.animal)
print('Breed:',Buzo.breed)
print('color:',Buzo.color)
#class variables can be assessed using class names also
print('\nAccessing class variable using class name')
print(Dog.animal)
#defining instance variable using normal method
#python program to show that we can create instance variable inside methods
#class for computer science students
class Dog:
#class variable
animal = 'dog'
#the init method or constructor
def __init__(self, breed):
#instance variable
self.breed = breed
#Adds an instance variable
def setColour(self, colour):
self.colour = colour
#Retrieves instance variable
def getColour(self):
return self.colour
#Driver code
Rodger = Dog('pug')
Rodger.setColour('brown')
print(Rodger.getColour())
full_name = input("enter your full name")
favourite_color = input("enter your favourite color")
print(full_name + "likes"+ favourite_color)
full_name.capitalize()