-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtips.html
More file actions
319 lines (249 loc) · 9.9 KB
/
tips.html
File metadata and controls
319 lines (249 loc) · 9.9 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSCI-128 Fall 2024</title>
<meta charset="utf-8" />
<meta name="author" content="James Shima" />
<meta name="description" content="CSCI 128 Fall 2024" />
<meta name="keywords" content="James Shima, Colorado School of Mines">
<link rel="stylesheet" href="128.css">
<link rel="icon" type="image/x-icon" href="img/No-Rattly_Sign.ico">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<div class="container">
<header>
<h1>CSCI 128 Fall 2024</h1>
<h2>
<a href="csci128.html">Home</a> |
<a href="lecture.html">Lecture</a> |
<a href="tips.html">Tutorials</a> |
<a href="finalgradecalc.html">Grade Calculator</a> |
<a href="https://edstem.org/us/courses/60585/discussion/">Ed</a> |
<a href="https://www.gradescope.com/">Gradescope</a> |
<a href="https://elearning.mines.edu/courses/68734">Canvas</a> |
<a href="https://bartik.mines.edu">Studio</a> |
<a href="https://elearning.mines.edu/courses/68734/pages/office-hours-2">Office Hours</a>
</h2>
</header>
<main>
<h2>Tutorials</h2>
<h3>Week 1 - Basics</h3>
<body>
<pre><code class="python">
# This is a comment you can write any thing you want here
# Name: James Shima
# Purpose of this python program: show some basic examples
"""
This is a multi line comment
I can write comments on multiple lines without using a #
everytime!
"""
# if you want to comment out a peace of your code (program),
# highlight it, hold CONTROL and also press / to comment it out and repeat to undo
# to undo a change(s) hold CONTROL and press z
# ================ TYPES ===================
x = 5 # this is a INTEGER aka a whole number (we will call it an Integer in this class)
x = 5.1 # this is a FLOAT aka a number with a decimal place
x = "Hello" # this is a STRING, which is data that holds letters or keyboard CHARACTERS as they appear in the quotes
# You can tell this is a string due to the quotes "" around it
# you can also use '' single quotes to make a string but I like the double quotes for a string and '' for a string with just
# one letter aka a CHAR
x = 'A' # this is a STRING as well but more formally called a CHAR. A string is a group/multiple CHARs or keyboard characters
x = [] # this is a LIST, it can hold multiple variables/types or items for you. This one is empty
x = [5, 5.1, "Hello", 'A', 0, 3.14] # they are usefull for storing groups of things in one place
5 # what does this do?
# NOTHING! its just a 5 floating in empty space within our program without a variable to be assigned to xd
# this is an example of a SEMANTIC ERROR (an error that works but doesn't do what we want)
x = float("infinity") # trick if you need infinity
x = input("Enter your name> ") # this is the input function, it takes input from the user and stores it in x AS A STRING!!!!
# the string "Enter your name> " is a custom prompt for the user to see what to input, it is also generally called
# a parameter, more on this and functions later...
print("Hello World") # this is the print function, it takes in anything and outputs it to the user to see
print(x) # printing a variable
print(f"The user put in {x}") # this is print with an f string. f strings allow you to put variables or expresions
# inside a string for example f"The value of 2+2 is {2+2}" --> The value of 2+2 is 4
print(f"The value of 2+2 is {2+2}") # --> The value of 2+2 is 4
x = "3.3" # is this a float or a string?
x = float("3.3") # what about now
x = "5" # is x a string or an int?
x = int("5") # what about now?
# ever unsure of what type something is?
print(type(x))
x = x + 1 # How is this possible???
# = does not mean equals! it means assign a new value to the left hand side.
# so this is assigning x to be what x is currently, but with one added to it
# so if x is initally 5, then x = x+1, would make x now be 6 as x = x-->(5) + 1 is 6
# WE WILL USE THIS TRICK A LOT SO DON'T MIX THIS UP
# what's e? SCIENTIFIC NOTATION!
x = 1e3 # scientific notation (USEFUL for your first assignment)
x = 5.2e11 # 520000000000.0 lot nicer than writing all those zeros!
# 1e3 means 1 * 10^3 = 1000
# CAREFUL this give you a float and not an int so use the x = int(x) statement if needed
x = x % 2 # Gives us the remainder when dividing by 2
# (think back to 3rd grade long division and you did R for the remainder)
# this is the R value! It is useful for solving many problems
x = x // 2 # division but chop off any decimal point (gives us a integer without rounding)
"""
Variable Names:
- Must not start with a number (but can after the first character)
- No spaces!
- Should be specific (not a or b or c... etc.)
- Seperate words with an underscore i.e. (variable_with_long_name) this is called Snake Case
- No dashes! or other special symbols except _
- No words reserved for python (sum max min count int print ...) are a few No Nos
"""
</code></pre>
<h2>Week 2 - Lists, Strings, and Binary Oh My!</h2>
<h3>f-strings</h3>
f strings or "format" strings are a nice way to print your output without using commas
<pre><code>
x = 5
print(f"the value of x is {x}")
print(f"x+1 is {x+1}!")
# the value of x is 5
# x+1 is 6
</code></pre>
Nice right!?
<h3>Rounding</h3>
f strings allow you to round floats as well!
<pre><code> print(f"round this number to 2 decimal places {2.111111:.2f}")</code></pre>
just add the :.Xf where X is the number of decimal places to add/round. This works even if its zero it wil add X amount of zeros to the end.
<h3>String Magic</h3>
<pre><code>
x = "hello"
y = " world!"
print(x+y)
# hello world!
print(x*5)
# hellohellohellohellohello
print(f'{(x+" ")*5}') # notice I had to change "" and '' in an f string!
# hello hello hello hello hello
</code></pre>
<h3>String Methods</h3>
the .split() method will seperate a string into elements of a list seperated by whitespace(by default) i.e(spaces newlines tabs)
or by a common seperator between the strings that is provided. The .join() method will take in a list and do the opposite of split joining each element of the list by the String
example
<pre><code>
s = "hello world!"
s = s.split()
print(s)
# ["hello", "world!"]
s = " ".join(s)
print(s)
# hello world!
s = "hello@$!there@$!mate"
s = s.split("@$!")
print(s)
# ["hello", "there", "mate"]
s = "@".join(["my","dog","tommy"])
print(f"{s}")
# my@dog@tommy
</code></pre>
<h3>Lists and List Methods</h3>
<ul>
<li>sorted() sorts a list</li>
<li>map() applies a function to each list member</li>
<li>max() takes the max value from the list</li>
<li>min() takes the minimum value from the list</li>
<li>.index() takes in an element and returns the first index of its location</li>
<li>.append() adds an element to the end of a list</li>
<li>.pop() removes an element from the end of a list</li>
<li>.remove() removes all element from the the list</li>
<li>list() creates a new list/converts something into a new list</li>
<li>[] creates a new empty list i.e. l = []</li>
</ul>
<pre><code>
l = []
l.append(1)
l.append(3)
l.append(2)
print(l)
# [1, 3, 2]
l = sorted(l)
print(l)
# [1, 2, 3]
l = list(map(str, l))
print(l)
# ["1", "2", "3"]
l = list(map(int, l)) # convert each element back to int
print(f"MAX:{max(l)}, MIN:{min(l)}")
# MAX:3, MIN:1
l.pop()
print(l)
# [1, 2]
l.index(1)
# 0
l.index(2)
# 1
l.index("hello")
# Error!
</code></pre>
<h3>Slicing</h3>
uses the [] operator -> [start index : end index(non-inclusive) : optional step]<br><br>
REMEBER THIS: <br>
<h4>Reverse a string/list</h4>
<pre><code>
l = [1,2,3]
l = l[::-1]
print(l)
# [3,2,1]
</code></pre>
<pre><code>
s = "hello"
print(s[0])
# h
print(s[-1])
# o
print(s[-2])
# l
print(s[1:])
# ello
print(s[2:4])
# ll
print(s[::-1])
# olleh
"""
Everything above applies to lists as well
"""
</code></pre>
<h3>Mutable vs Immutable</h3>
Mutable -> you can change/reassign an element in it! ex: lists, dictionaries<br>
Immutable -> you cannot change any of its elements! example: strings, tuples
<pre><code>
l = ["H","I"]
l[0] = "h"
# ["h", "I"]
s = "hello"
s[0] = 'H'
# ERROR strings are Immutable!
# you have to make a new entire string to accomplish this!
s = "H" + s[1:]
# or just...
s = "Hello"
print(s)
# Hello
</code></pre>
<h2>BINARY</h2>
<p>
Binary is how computers understand instructions or know what to do. Binary is simply a string of 1's and 0's. When python runs under the hood, the computer is actually reading what we tell it to do in this format. <br>i.e print("hello") -> 010101011110101011110101010101101000001010101110101<br>
Binary is actually a number system with base 2. We humans use a base 10 number system due to us having 10 fingers. i.e 15 in base 10 is just 15 while in binary it is 1111. How this works
is simple. 15 = 1 * 10^1 + 5 * 10^0 for base 10! 15 = 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 for base 2 (binary). Generally a number in any base can be calculated as. Number = {0...base-1} * base^place + ... + {0...base-1} * base^0. All that to say when our computers do math or calculations, its doing them in base 2 then just converting or showing us the result in base 10! For example 2+2 = 0010 + 0010 = 0100 = 4
</p>
In python
<pre><code>print(bin(4)) # "0b100"</code></pre> the 0b means its a binary string in python, if you want just the numbers you can do bin(4)[2:]
</main>
<footer>
<small>
Have a question? Need help? ask on <a href="https://edstem.org/us/courses/60585/discussion/">Ed</a>
</small><br>
<small>
Developed by James Shima
</small>
</footer>
</div>
</body>
</html>