-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
204 lines (151 loc) · 3.95 KB
/
main.py
File metadata and controls
204 lines (151 loc) · 3.95 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
x = 10 #initialization
x = 23 #assignment
y = 5 #declaration
z = ( x + y)/x + (78%3) #usual mathematical operations supported
print(z)
name = "bobby" #string
age = 12 #integer
height = 6.5 # float
hasDate = False
comp = 7j #complex
# using fstrings for interpolation
message = f'Hi my name is {name} I am {age} years old'
print(message) # ‘Hi my name is bobby I am 12 years old'
# type casting to convert types
intHeight = int(height) # 6
strHeight = str(height) # '6'
floatHeight = float(intHeight) # 6.0
ageType = type(age)
name = input("Enter name: ") # reads input
print (name) # prints output
#correct
if 3 > 5:
print("more")
else :
print("less")
#wrong
if 3 > 5: print ("more")
else : print ("less")
# elif === else if
mark = input("Enter mark: ")
mark = int(mark)
if mark > 70 :
print("A")
elif mark > 60:
print("B")
elif mark > 50:
print("C")
else :
print("F")
i = 1
while i < 10:
print(i)
i+=1
else:
print("This is run when the loop condition is no longer met")
# iterating an iterable such as a list
list = ["bob", "sally", "john"]
for j in list:
print(j)
# iterating between custom range an increment
for i in range(0, 10, 2):
print(i)
#basic function
def add(a, b):
return a + b
def printPerson(name, age, height):
print(name, age, height)
# you can specify arguments in any order if they are named
printPerson(age=12, name='bob', height=5);
#default arguments are used when they are not given in the function call
def sayHello(fname, lname='Smith'):
print('Hello '+fname + ' ' + lname)
sayHello('John');
sayHello('Bill', 'Young');
# functions can return multiple values
list = ['item1', 'item2', 'item3']
list2 = [12, 33, 45, 58, 23]
print(list);
# negative indexing can access elements starting from the end
print(list2[-1])
# select a subset of a list
print(list2[2:4])
# gets the length of a list
print(len(list2))
#add items to list
list.append('item4');
#remove item from list
item4 = list.pop()
#copy list
list3 = list2.copy()
# list comprehension, lets you create new lists from existing lists
num = [ 1, 2, 3, 4]
doubled = [n*2 for n in num]
print(doubled) # [ 2, 4, 6, 8]
odd = [ n for n in num if n%2 == 1]
print(odd) # [ 1, 3]
# unpacking a list, lets you create variables from items in the list
num = [ 1, 2, 3, 4]
[first, second, *rest] = num
print(first)
print(second)
print(rest)
# joining lists
num2 = [5, 6]
num3 = num + num2
print(num3) # [1, 2, 3, 4, 5, 6]
# copying lists
num4 = num2.copy()
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple); # ('apple', 'banana', 'cherry', 'apple', 'cherry')
print(thistuple[0]); # ‘apple'
data = [ 20, 3, 20, 42, 2, 3, 10, 32, 2]
myset = {0, 1}
for num in data:
myset.add(num)
print(myset)# {0, 1, 2, 3, 32, 42, 10, 20}
num_unique = len(myset)
mydict = {
"name":"bob",
"age": 34
}
print(mydict)
# assessing a key
print(mydict['age'])
# adding a new key and value
mydict['height'] = 7
# iterating keys
for key in mydict:
print(key)
# iterating values
for key in mydict:
print(mydict[key])
# check for a key
if 'weight' in mydict:
print(mydict['weight'])
else:
print('no weight property!')
#Parent class
class Person:
def __init__(self, name, height, weight):
self.name = name;
self.height = height;
self.weight = weight;
def sayHello(self):
print("Hello! I'm a person, my name is", self.name)
# Child class inherits from Person
class Student(Person):
# super is the reference to the parent class Person so
# we call Person's constructor here to set the Person
# properties of the student instance
def __init__(self, stid, name, height, weight):
super().__init__(name, height, weight)
self.stid = stid
# override method of parent
def sayHello(self):
print("Hello! I'm a student, my name is", self.name)
bob = Person('bob', 12, 34)
sally = Student(123, 'sally', 7, 34)
bob.sayHello();
sally.sayHello();
print(bob.name);