-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek3_Conditions_loops_functions_classes.py
More file actions
179 lines (128 loc) · 3.23 KB
/
week3_Conditions_loops_functions_classes.py
File metadata and controls
179 lines (128 loc) · 3.23 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
######### comparison operators #########
from operator import ne
from statistics import multimode
from symbol import for_stmt, while_stmt
a = 6
a == 7
"A" == "B" # works also with str
# 1=2 #produces an error
6 >= 5 # True
2 != 3 # inequality test
######### branching #########
age = 17
if age > 18:
print("you can enter")
elif age == 18:
print("go see plink floyd")
else:
print("go see Meat Loaf")
print("move on")
######### range, loops for, enumerate, range, while #########
N = 3
range(N) # => sequence [0,...,N-1]
print(range(3))
print(range(10, 15))
# Write a for loop the prints out all the element between -5 and 5 using the range function.
for index in range(-5,6):
print(index)
squares = ["red", "yellow", "green", "purple", "blue"]
print(squares)
for i in range(len(squares)):
squares[i] = squares[i] + str(i)
print(squares)
for square in squares:
print(square)
for index, label in enumerate(squares):
print("index:" + str(index) + ", label:" + label)
print(index, label)
for kv in enumerate(squares):
print(kv)
newSquares = []
i = 0
while not (square := squares[i]).startswith("green"):
print(f"adding {square} to newSquares")
newSquares.append(square)
i = i + 1
print(newSquares)
######### functions #########
def add1(a):
"""
increment by 1\n
:param a: my comment about 'a'
:return: a+1
"""
b = a + 1
return b
c = add1(5)
print(c)
d = add1(8)
print(d)
help(add1)
def mult(a, b):
"""
a * b
:return:
"""
return a * b
print(mult(2, 5))
print(mult(2.7, 5.9))
print(mult(2, "TO"))
def MJ():
"""
no return
"""
print("Mickael Jackson")
MJ()
def nowork():
pass
print(nowork())
def noworkBis():
return None
print(noworkBis())
def artistNames(*names):
for name in names:
print(name)
artistNames("kendy", "jm")
def pinkFloyd():
global claimedSales
claimedSales = "45m"
return claimedSales
pinkFloyd()
print(claimedSales)
def hello():
print("hello")
artist = "Michael Jackson"
def printer(artist):
global internal_var # a way to create global variables from within a function as follows:
internal_var= "Whitney Houston"
print(artist,"is an artist")
printer(artist)
printer(internal_var)
##### Objects and Classes #####
# We could find out the type of an object by using the type command.
class Circle(object # parent object
):
def __init__(self, radius, color = 'white'):
'''
this is a constructor
:param radius:
:param color: has a defaut value 'white'
'''
self.radius = radius
self.color = color
def toPrint(self):
print(self.radius, self.color)
None
myCircle1 = Circle(1, 'red')
print(myCircle1.radius)
print(myCircle1.color)
myCircle1.toPrint()
myCircle1.color = "blue"
myCircle1.toPrint()
myCircle2 = Circle(2)
myCircle2.toPrint()
# The dir function is useful for obtaining the list of data attributes and methods associated with a class.
# The object you're interested in is passed as an argument.
# The return value is a list of the objects data attributes.
# The attribute surrounded by underscores are for internal use, and you shouldn't have to worry about them.
print(dir(Circle))