-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop5.py
More file actions
244 lines (189 loc) · 4.63 KB
/
oop5.py
File metadata and controls
244 lines (189 loc) · 4.63 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
from machines.vehicle_stuff import Vehicle, Truck, Motorcycle
from utils.utility_stuff import DictionaryShortner, ListAndCharShortner
#methods are invoked on objects
#functions are invoked stand alone
#object oriented programming is about allowing better organization
#method is a object oriented concept
car1 = Vehicle('sedan', 'tesla')
print(car1.vehicle_body)
car2 = Vehicle('suv', 'tesla')
print(car2.get_vehicle_count())
truck1 = Truck('big rig', 'dodge')
car1 = Vehicle('sedan', 'ford')
motorcycle1 = Motorcycle('rider', 'ducatti')
print(truck1.get_vehicle_count())
# remember __init__ is constructor for python
# below is example of polymorphism aka different behavior, don't get too fancy to avoid errors
# comments are key in python as it is such a flexible language
truck1.drive()
car1.drive()
motorcycle1.drive()
myshortner = ListAndCharShortner([1,2,3,4,5])
myshortner.print_shortened_items()
myshortner = DictionaryShortner({1: 'mike', 2: 'tom', 3: 'rebeca', 4: 'mike', 5: 'paul'})
myshortner.print_shortened_items()
# double under - dunder methods
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return self.name + " age is " + str(self.age)
def __len__(self):
return 45
tom = Employee('Tom Lanister', 47)
# as we made a __str__ method it will the the representation of the object we made instead of a memory location
print(tom)
print(len(tom))
# Assignment 1:
"""
Define a class hierarchy representing animals with a parent class Animal
and child classes such as dog, fish and bird. Each subclass animal should have
a name and an age and should have 2 methods defined in it: move(), eat().
The move method needs to be specific for a given animal where as the
eat() method should be generic for all animals. The methods don't need to
do anything except print information about who is doing what.
After creating the class hierarchy, create instances of each of the 3 animals
dog, fish and bird and make them eat and move.
"""
class Animal:
def __init__(self, name, age):
print('animal created')
self.name = name
self.age = age
def eat(self):
print('the animal is eating')
def move(self):
print('the animal is moving')
class Dog(Animal):
def move(self):
print('the dog runs')
class Fish(Animal):
def move(self):
print('the fish swims')
mydog = Dog('Lassie', 2)
mydog.move()
print(mydog.name)
myfish = Fish('Goopy', 1)
myfish.eat()
print("hey \'ann\'s house\' ",2 ** 3 ** 2 ** 1)
print(type(len("Python")))
z, y, x = 2, 1, 0
# put line here
print(x, y, z)
print(0 ** 0)
for i in range(1, 4, 2):
print("*")
# for i in range(1, 4, 2):
# print("*1", end=".")
for i in range(1, 4, 2):
print("*", end="**")
print("***")
x = "20"
y = "30"
print(x > y)
s = "Hello, Python!"
print(s[-14:15])
lst = ["A", "B", "C", 2, 4]
del lst[0:-2]
print(lst)
dict={'a':1,'b':2, 'c':3}
for item in dict:
print(item)
s = 'python'
for i in range(len(s)):
i = s[i].upper()
print(s, end="")
lst = [[c for c in range(r)] for r in range(3)]
for x in lst:
for y in x:
if y < 2:
print('*')
lst = [2 ** x for x in range(0, 11)]
print(lst[-1])
print(2**3)
def list1():
val = []
for x in range(0,11):
val.append(2**x)
return val
lst1 = "12,34"
lst2 = lst1.split(',')
print(152,len(lst1) < len(lst2))
def fun(a, b=0, c=5, d=1):
return a ** b ** c
print(fun(b=2, a=4, c=3))
x=5
f = lambda x: 1 + 2
print(f(x))
from math import pi as xyz # line 01
print(xyz)
x=1
def a(x):
return 2 * x
x = 2 + a(x)
print(a(x))
#line1 #line2 #line3 #line4 #line5
a = 'hello'
def x(a,b='a'):
z = a[0]
return z
print(x(a))
#line1 #line2 #line3 #line4 #line5
s = 'SPAM'
def f(x):
return s + 'MAPS'
print(f(s))
lst = range(5)
print(len(lst))
def gen():
lst = range(5)
for i in lst:
yield i*i
for i in gen():
print(i, end="")
class A:
def a(self):
print("A", end='')
class B(A):
def a(self):
print("B", end='')
class C(B):
def b(self):
print("B", end='')
a = A()
b = B()
c = C()
a.a()
b.a()
c.b()
print('\nhi')
try:
print("Hello")
raise Exception
print(1/0)
except Exception as e:
print(e)
Val = 1
Val2 = 0
Val = Val ^ Val2
print(Val)
Val2 = Val ^ Val2
print(Val2)
Val = Val ^ Val2
print(Val)
print(1^1)
dict={'a':1,'b':2, 'c':3}
for item in dict:
print(item)
lst = [[c for c in range(r)] for r in range(3)]
for x in lst:
for y in x:
if y < 2:
print('*', end='')
print(lst)
from random import randint
for i in range(10):
# print(random(1, 5))
pass
print(randint(0,1))