-
Notifications
You must be signed in to change notification settings - Fork 12
/
va2021april5.py
70 lines (49 loc) · 1.34 KB
/
va2021april5.py
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
def get_average(numbers):
running_total = 0
# loop
for n in numbers:
# add each item to the running total
running_total = running_total + n
# running_total += n
count = len(numbers)
average = running_total / count
return average
# Sample Data
data = [123, 597, 631, 61, 93, 509]
# Test function
result = get_average(data)
print(result)
print(get_average([123, 597, 631, 61, 93, 509]))
class Human:
# Constructor
def __init__(self, first, last):
# PROPERTIES - variables in a class/object
self.first = first
self.last = last
# METHODS - function defined inside of a class/object
def full_name(self):
return f"{self.last}, {self.first}"
h = Human("Kevin", "Long")
print(h.full_name())
print(h.full_name())
s = Human("Kay", "Long")
print(s.full_name())
print(s.full_name())
class Animal:
def __init__(self, noise):
self.noise = noise
def speak(self):
print(f"Animal goes '{self.noise}'.")
# INHERITANCE - IS A KIND OF...
class Pet(Animal): #A PET IS A KIND OF ANIMAL
def __init__(self, name, noise):
self.name=name
super(Pet, self).__init__(noise)
# def speak(self):
# print(f"{self.name} goes '{self.noise}'.")
a = Animal("Grrr")
a.speak()
b = Animal("cluck")
b.speak()
p = Pet("Bianca", "Meow")
p.speak()