Skip to content

Commit f989893

Browse files
authored
Added OOP's and Comprehension Programs
1 parent b3d4b7f commit f989893

File tree

4 files changed

+243
-0
lines changed

4 files changed

+243
-0
lines changed

Beginner_Level/Class.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Human:
2+
def __init__(self, n, o):
3+
self.name = n
4+
self.occupation = o
5+
6+
def do_work(self):
7+
if self.occupation == "Tennis Player":
8+
print(self.name,"plays tennis")
9+
elif self.occupation == "Cricket Player":
10+
print(self.name,"Plays Cricket")
11+
12+
def speak(self):
13+
print(self.name,"\nsay how are you?")
14+
15+
sachin = Human("Sachin Tendulkar","Cricker")
16+
sachin.do_work()
17+
sachin.speak()
18+
19+
print('\n')
20+
21+
maria = Human("Maria sharapova","Tennis")
22+
sachin.do_work()
23+
sachin.speak()
24+
25+
print('\n')
26+
27+
28+
# built-in class modules
29+
30+
class Employee:
31+
'Common base class for all employees'
32+
empCount = 0
33+
34+
def __init__(self, name, salary):
35+
self.name = name
36+
self.salary = salary
37+
Employee.empCount += 1
38+
39+
def displayCount(self):
40+
print("Total Employee %d" % Employee.empCount)
41+
42+
def displayEmployee(self):
43+
print("Name : ", self.name, ", Salary: ", self.salary)
44+
45+
46+
print("Employee.__doc__:", Employee.__doc__)
47+
print("Employee.__name__:", Employee.__name__)
48+
print("Employee.__module__:", Employee.__module__)
49+
print("Employee.__bases__:", Employee.__bases__)
50+
print("Employee.__dict__:", Employee.__dict__)

Beginner_Level/Inheritance.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
class Vehicle:
2+
def genral_usage(self):
3+
print("use for transportation")
4+
5+
class Car(Vehicle):
6+
def __init__(self):
7+
print("I'm a car")
8+
self.wheels = 4
9+
self.has_roof = True
10+
11+
def specific_usage(self):
12+
self.genral_usage()
13+
print("specific usage: commute to work, vacation with family")
14+
15+
class Motorcycle(Vehicle):
16+
def __init__(self):
17+
print("I'm a Motorcycle")
18+
self.wheels = 2
19+
self.has_roof = False
20+
21+
def specific_usage(self):
22+
self.genral_usage()
23+
print("specific usage: road trip, racing")
24+
25+
c = Car()
26+
#c.genral_usage() # if you don't want to call general_usage() explicitly, you can call inside class as above
27+
c.specific_usage()
28+
print('\n')
29+
m = Motorcycle()
30+
#m.genral_usage()
31+
m.specific_usage()
32+
33+
print('\n')
34+
35+
print(isinstance(c,Car)) # check and return bool value
36+
print(isinstance(c,Motorcycle))
37+
38+
print('\n')
39+
40+
# another example
41+
42+
class Parent: # define parent class
43+
parentAttr = 100
44+
def __init__(self):
45+
print("Calling parent constructor")
46+
47+
def parentMethod(self):
48+
print('Calling parent method')
49+
50+
def setAttr(self, attr):
51+
Parent.parentAttr = attr
52+
53+
def getAttr(self):
54+
print("Parent attribute :", Parent.parentAttr)
55+
56+
class Child(Parent): # define child class
57+
def __init__(self):
58+
print("Calling child constructor")
59+
60+
def childMethod(self):
61+
print('Calling child method')
62+
63+
c = Child() # instance of child
64+
c.childMethod() # child calls its method
65+
c.parentMethod() # calls parent's method
66+
c.setAttr(200) # again call parent's method
67+
c.getAttr() # again call parent's method
68+
69+
print('\n')
70+
71+
# method overriding
72+
73+
class Parent: # define parent class
74+
def myMethod(self):
75+
print('Calling parent method')
76+
77+
class Child(Parent): # define child class
78+
def myMethod(self):
79+
print('Calling child method')
80+
81+
c = Child() # instance of child
82+
c.myMethod() # child calls overridden method
83+
84+
print('\n')
85+
86+
# Multiple Inheritance
87+
88+
class Father:
89+
def skills(self):
90+
print("Gardening & Programming")
91+
92+
class Mother:
93+
def skills(self):
94+
print("cooking & art")
95+
96+
class child(Father,Mother):
97+
def skills(self):
98+
Father.skills(self)
99+
Mother.skills(self)
100+
print("sports & gaming")
101+
102+
c = child()
103+
c.skills()
104+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
2+
Type "help", "copyright", "credits" or "license()" for more information.
3+
>>> numbers = [1,2,3,4,5,6,7,8,9]
4+
>>> even = []
5+
>>> for i in numbers:
6+
if i%2==0:
7+
even.append(i)
8+
9+
10+
>>> even
11+
[2, 4, 6, 8]
12+
>>> # syntax [output for-loop condition]
13+
>>> even = [i for i in numbers if i%2==0]
14+
>>> even
15+
[2, 4, 6, 8]
16+
>>> cube_numbers = [i*i*i for i in numbers]
17+
>>> cube_numbers
18+
[1, 8, 27, 64, 125, 216, 343, 512, 729]
19+
>>>
20+
>>>
21+
>>>
22+
>>>
23+
>>>
24+
>>> # Set Comprehension
25+
>>> s = set{[1,2,3,4,5,2,3]}
26+
SyntaxError: invalid syntax
27+
>>> s = set([1,2,3,4,5,2,3])
28+
>>> s
29+
{1, 2, 3, 4, 5}
30+
>>> type(s)
31+
<class 'set'>
32+
>>> even = { i for in s if i%2==0}
33+
SyntaxError: invalid syntax
34+
>>> even
35+
[2, 4, 6, 8]
36+
>>>
37+
>>>
38+
>>>
39+
>>>
40+
>>> # Dictionary Comprehension
41+
>>>
42+
>>> cities = ["Mumbai","new york","tokyo"]
43+
>>> country = ["India","USA","Japan"]
44+
>>> z = zip(cities,country)
45+
>>> z
46+
<zip object at 0x00000253A2C7BF00>
47+
>>> for a in z:
48+
print(a)
49+
50+
51+
('Mumbai', 'India')
52+
('new york', 'USA')
53+
('tokyo', 'Japan')
54+
>>> d = {city:country for city, country in zip(cities,country)}
55+
>>> d
56+
{'Mumbai': 'India', 'new york': 'USA', 'tokyo': 'Japan'}
57+
>>> type(d)
58+
<class 'dict'>
59+
>>> dir(d)
60+
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
61+
>>>

Beginner_Level/Polymorphism.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class India():
2+
def capital(self):
3+
print("New Delhi is the capital of India.")
4+
5+
def language(self):
6+
print("Hindi is the most widely spoken language of India.")
7+
8+
def type(self):
9+
print("India is a developing country.")
10+
11+
12+
class USA():
13+
def capital(self):
14+
print("Washington, D.C. is the capital of USA.")
15+
16+
def language(self):
17+
print("English is the primary language of USA.")
18+
19+
def type(self):
20+
print("USA is a developed country.")
21+
22+
23+
obj_ind = India()
24+
obj_usa = USA()
25+
for country in (obj_ind, obj_usa):
26+
country.capital()
27+
country.language()
28+
country.type()

0 commit comments

Comments
 (0)