-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheatsheet.py
232 lines (158 loc) · 4.86 KB
/
cheatsheet.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
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
# a number
x = 5
y = -10.6
# strings
hello = "Hi, Python!"
pythonIsHard = False
pythonIsFun = True
#======================================================================================
# Printing
city0 = "Evanston"
city1 = "Chicago"
cities = city0 + " and " + city1
print(cities)
# printing things
print("hello") # prints "hello"
print(hello) # prints "Hi, Python!"
print("the best number is " + str(x) + ", but " + str(y) + " is good too")
print(f"the worst number is {x}, but {y} is bad too.")
print(f"....but {y + x + 1000} would be the best number")
# assert city0 == city1, f"cities should be the same! {city0} {city1}"
#======================================================================================
# Comparisons
print(city0 != city1 or x == y)
city0 != city1 or not x == y
(city0 != city1) or not (x == y)
# comparing things
notTheSame = x != y
stillNotTheSame = not x == y
print(f"notTheSame: {notTheSame}")
print(f"stillNotTheSame: {stillNotTheSame}")
citiesNotTheSame = city0 != city1
numbersNotTheSame = not (x == y)
allDifferent = citiesNotTheSame and citiesNotTheSame
print(f"citiesNotTheSame:{citiesNotTheSame}, numbersNotTheSame:{numbersNotTheSame}")
print(f"allDifferent:{allDifferent}")
if x > y:
print(f"{x} is bigger than {y}")
elif x == y:
print(f"{x} is the same as {y}")
else:
print(f"{x} is less than {y}")
if city0 > city1:
print(f"{city0} is after {city1}")
elif city0 == city1:
print(f"{city0} is the same as {city1}")
else:
print(f"{city0} is before {city1}")
#======================================================================================
# Lists
# creating an list...
# you can do this by declaring one, or using the range function
arr0 = [0,1,2,3,4]
arr1 = range(0,5)
print(f"arr0 is: {arr0}")
print(f"arr0 is: {arr1}")
allExes = "X"*15
squareList = [number*number for number in arr0]
print(f"allExes is: {allExes}")
evenSquareList = [x for x in squareList if (x%2==0)]
print(f"squareList is: {squareList}") # [0, 1, 4, 9, 16]
print(f"evenSquareList is: {evenSquareList}") # [0, 4, 16]
for i in range(5):
print (f"The square of {i} is {i*i}")
for number in evenSquareList:
print (f"{number} is an even square")
elements = ["water", "air", "fire", "earth", "love"]
print(f"The first element is {elements[0]}") # "water"
print(f"The final element is {elements[-1]}") # "love"
# elements[0] = "skittles"
# print(f"The first element is {elements[0]}")
print(f"The first two elements are: {elements[0:2]}") # ['water', 'air']
print(f"All but the first and last elements: {elements[1:-1]}") # ['air', 'fire', 'earth']
laughter = []
while len(laughter) < 4:
laughter.append("ha")
print(laughter)
# Errors
# x = 5
# print(elements[x])
# planets = ("jupiter", "saturn", "uranus", "neptune", "pluto")
# planets[5] = "xena"
#======================================================================================
# Functions
import math
def getDistance(x, y, z):
sum = x*x + y*y + z*z
return math.sqrt(sum)
vx = 5
vy = 10
vz = 15
dist = getDistance(vx, vy, vz)
print(f"The length of a vector {vx} {vy} {vz} is {dist}, about {round(dist)}")
#======================================================================================
# Loops
count = 5
while count > 0:
count = count - 1
print(f"countdown: {count}")
print("*boom!*")
# run forever....
# count = 5
# while count > 0:
# count = count + 1
# print(f"never gonna: {count}")
# print("give you up!")
# Command-C or CTRL-C will break the infinite loop
#======================================================================================
# Dictionaries
translations = {
"cat": {
"spanish": "gato",
"german": "katze",
"finnish": "kissa",
"yoruba": "ologbo"},
"dog": {
"afrikaans": "hond",
"gujarati": "kutto",
"hawaiian": "ilio"
}
}
catDictionary = translations["cat"]
# Get all the keys of a dictionary
catLanguages = catDictionary.keys()
for lang in catLanguages:
print(f"'cat' in {lang} is '{catDictionary[lang]}' ")
canTranslateFish = "fish" in translations
print(f"Can I translate 'fish' with this dictionary? {canTranslateFish}")
if "french" in catLanguages:
print(f"'A French cat is '{catDictionary['french']}' ")
else:
print("No cat translation for French")
#======================================================================================
# Classes
class Cat:
catWord = "meow"
def __init__(self, name):
self.fish = 0
self.mood = "happy"
self.name = name
def speak(self):
return f"{self.name} says '{self.catWord}'"
def __str__(self):
return f"{self.name}, a {self.mood} cat"
cat0 = Cat("Bustopher")
cat1 = Cat("Grizabella")
cat1.mood = "sad"
print(cat0)
print(cat1)
print(cat0.speak())
print(cat1.speak())
class MagicalCat(Cat):
catWord = "abracadabra"
def __init__(self, name):
super().__init__(name)
def speak(self):
return f"{self.name} says ✨'{self.catWord}'✨"
cat2 = MagicalCat("Mistoffelees")
print(cat2.speak())