-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise-4.py
More file actions
109 lines (82 loc) · 2.05 KB
/
Exercise-4.py
File metadata and controls
109 lines (82 loc) · 2.05 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
# Exceptions (so, the program will never crash)
try:
age = int(input('Enter the number: '))
income = 20000
risk = income / age
print(age)
except ZeroDivisionError:
print('Age cannot be zero!')
except ValueError:
print('Invalid value')
# Classes in python
class PointClass:
def move(self):
print("move")
def draw(self):
print("draw")
point1 = PointClass() #create new object
point1.draw() #objects are the instance of class
point1.x = 10
point1.y = 20
print(point1.x)
point2 = PointClass()
point2.x = 66
print(point2.x)
#constructor in classes
class Axis:
def __init__(self, x, y): #this is a constructore
self.x = x #self represents the current object
self.y = y
aaxis = Axis(10,25)
print(aaxis.x)
aaxis.y = 25
print(aaxis.y)
# Exec 1 - classes
class Person:
def __init__ (self, name):
self.name = name
def talk(self):
print(f"Hi, im {self.name}")
john = Person("John smith")
john.talk()
bob = Person("Bob polly")
bob.talk()
# Inheritance
class Mammals:
def walk(self):
print("walk")
class Dog(Mammals):
pass # with pass we can create empty class without error
class Cat(Mammals):
def meow(self):
print("Meow")
dog1 = Dog()
dog1.walk()
cat1 = Cat()
cat1.meow()
# Import modules
#File1 name - converters.py (Module)
def lbs_kg(weight):
return weight * 0.45
def kb_lbs(weight):
return weight / 0.45
#File2 name - app.py
import converters
weight11 = converters.lbs_kg(70)
print(weight11)
#File3 name app2.py
from converters import kg_lbs
print(kg_lbs(80))
#Exec 2 - modules
#Filename - utils.py
def find_max(numbers):
maximum = numbers[0]
for number in numbers:
if number > maximum:
maximum = number
return maximum
#Filename - app.py
from utils import find_max
number = [5,10,66,99,10,2]
max1 = find_max()
print(max1) # max is also an inbuilt python function to find max value [ print(max{numbers}) ]