-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-01-02.qmd
393 lines (275 loc) · 7.77 KB
/
slides-01-02.qmd
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
---
title: "python basics (slides)"
format: revealjs
slide-number: true
---
# CSc 110 Python Basics
## CS Buddy Mentor Program Sign-Up

## Announcements
- Sign-up for your lab session at [xinchenyu.github.io/csc110-spring2025/short-projects.html](https://xinchenyu.github.io/csc110-spring2025/short-projects.html)
- My office hours changed to Wednesday 10:30am-12:00pm.
- Full schedule for office hours can be found at [xinchenyu.github.io/csc110-spring2025/tas.html](https://xinchenyu.github.io/csc110-spring2025/tas.html)
- Next lecture is Jan 22, next Wednesday.
## Did you set up your workspace?
- Did you download Python 3?
- Did you download VS Code (or PyCharm, or Mu)?
- If not, that's fine, you can use [vscode.dev](https://vscode.dev/)
# What is a program?
## The `print` function
- What does the print function do?
## The `print` function
- `print()` sends characters (strings) to the **standard output**
- By default, the **standard output** of a python program goes to the console.
```{python}
#| echo: true
print("some characters")
```
<br />
```{python}
#| echo: true
print('some characters')
```
## String operations
String concatenation:
- What will the standard output display when the code below is run?
```{python}
#| echo: true
#| eval: false
print("hello" + "world")
```
## String operations
In your groups, try the following operations:
- String repetition: `"abc" * 4`
- String concatenation with `+` and `,`. What is the difference?
- Combine both repetition and concatenation to print the following:
```{python}
#| echo: false
#| eval: true
print("They sang", "Aa" * 10)
```
## String operations
solution 1:
```{python}
#| echo: true
#| eval: true
print("They sang", "Aa" * 10)
```
solution 2:
```{python}
#| echo: true
#| eval: true
print("They", "sang", "Aa" * 10)
```
solution 3:
```{python}
#| echo: true
#| eval: true
print("They" + " " + "sang" + " " + "Aa" * 10)
```
## Multiple `print` statements
- What will the standard output display when the code below is run?
```{python}
#| echo: true
#| eval: false
print('Are')
print('You')
print('In')
print('College?')
```
## Multiple `print` statements
- By default, the `print` function moves the cursor to the next line after printing, unless you specify otherwise.
```{python}
#| echo: true
#| eval: true
print('Are', end = ' ')
print('You', end = ' ')
print('In', end = ' ')
print('College?', end = ' ')
```
What other characters can you use for values for the `end` parameter?
## Dealing with special characters
Write a simple program that prints the following output to the python console:
<br />
```{=html}
<pre style="font-size: 30px">
He said, "What's up?"
Joe's friend didn't reply.
</pre>
```
## Dealing with special characters
double quotes:
```{python}
#| echo: true
#| eval: true
print("He said, \"What's up?\"")
print("Joe's friend didn't reply.")
```
single quotes:
```{python}
#| echo: true
#| eval: true
print('He said, "What\'s up?"')
print('Joe\'s friend didn\'t reply.')
```
# Variables
## Variables and assignment
- Variables are names in a program that represent a stored value
- We can assign names to particular values in our program
- When we give a value a name, or update the value stored in a variable, this is called assigning a variable
## Demonstration of variable assignment
Variable assignment does not produce any output to the console. Run the following code:
```{python}
#| echo: true
#| eval: false
first_name = "Mary"
family_name = "Silva"
```
How would we print these variables?
## Variables improve readability
Without variables:
```{python}
#| echo: true
#| eval: true
print("The total price is", 100 * 1.08)
```
With variables:
```{python}
#| echo: true
#| eval: true
base_price = 100
tax_rate = 1.08
total_price = base_price * tax_rate
print("The total price is", total_price)
```
## Basic Types
- string: `"hello"` or `'hello'`
- integer: `3`
- float: `3.14`
- bool: `True` or `False`
Use the function `type()` with each literal type.
## Operations
- `+` : plus
- `-` : minus
- `*` : multiple
- `/` : divide
- `**` : power
- `//` : divide and floor
- `%` : modulos
<br />
## Operations - examples
```{python}
#| echo: true
#| eval: true
print(10 + 5 - 5) # addition and subtraction
print(10 * 5 / 4) # multiplication and division
print(25 ** 2) # square
print(25 ** 0.5) # square root
print(13 // 4) # division and round down to the nearest integer
print(13 % 4) # remainder
```
# Order of Operations
## PEMDAS
- What does PEMDAS stand for?
- The operator precedence:
- **P**arentheses
- **E**xponentiation
- **M**ultiplication and **D**ivision (including // and %)
- **A**ddition and **S**ubtraction
## PEMDAS
What value will each of these variables take on? No computers!
```{=html}
<pre>
a1 = 5 / 5 * 10 * 5
a2 = 5 / (5 * 10) * 5
b1 = 5 * 10 - 2
b2 = 5 * (10 - 2)
c = (3 // (4 // 5)) + 1
</pre>
```
## PEMDAS -- answer
```{python}
#| echo: true
#| eval: true
a1 = 5 / 5 * 10 * 5
a2 = 5 / (5 * 10) * 5
b1 = 5 * 10 - 2
b2 = 5 * (10 - 2)
# c = (3 // (4 // 5)) + 1 ERROR -- Zero Division
print(a1)
print(a2)
print(b1)
print(b2)
```
Note that the division operator returns a float even when both numerator and denominator are integers
# Rounding numbers with built-in function `round()`
## `round()`
Use the round() function to get a floating-point number rounded to the specified number of decimals.
Syntax:
```{python}
#| echo: true
#| eval: false
round(number, ndigits*)
```
The number of digits (`ndigits`) is optional, but we will often round number to two decimals:
```{python}
#| echo: true
#| eval: true
round(392.68750000000006, 2)
```
## `round()`
Use the round() function to get a floating-point number rounded to the specified number of decimals.
Syntax:
```{python}
#| echo: true
#| eval: false
round(number, ndigits*)
```
Rounding is done toward the nearest even choice:
```{python}
#| echo: true
#| eval: true
print(round(51.6))
print(round(51.4))
print(round(51.5))
print(round(50.5))
```
## Operations and Variable Assignments
Variable assignments are not like math --- they can appear in equations that make no mathematical sense.
<br />
```{python}
#| echo: true
#| eval: false
x = 5
print(x)
x = x + 2
print(x)
```
<br />
`x = x + 2` means "take the current value of x, add it with 2, and assign the resulting value (7) to x as its new value."
## Combining variables and operations
Write code to calculate the area of a circle when radius is 3:
1. Assign integer 3 to radius
2. Calculate the area (use $\pi$ as 3.1415) and round it to two decimals
3. Print the area
## Comments
Comments are text that is meant to be read, but not executed. The purpose is to improve readability.
```{python}
#| echo: true
#| eval: true
# assign a radius value
radius = 3
# compute the area of a circle and round to two decimals
area = round(3.1415 * radius ** 2, 2)
# print the area
print(area)
```
## Questions
- What if we want to get the area of circles when the radius is 1, 2, 3, 4 and 5?
- Can we do it more efficiently?
## Quiz 02
<center>
<div class="cleanslate w24tz-current-time w24tz-large" style="display: inline-block !important; visibility: hidden !important; min-width:300px !important; min-height:145px !important;"><a href="//24timezones.com/Tucson/time" style="text-decoration: none" class="clock24" id="tz24-1695057604-c1393-eyJob3VydHlwZSI6IjEyIiwic2hvd2RhdGUiOiIwIiwic2hvd3NlY29uZHMiOiIwIiwiY29udGFpbmVyX2lkIjoiY2xvY2tfYmxvY2tfY2I2NTA4ODZjNDg0OWVlIiwidHlwZSI6ImRiIiwibGFuZyI6ImVuIn0=" title="World Time :: Tucson" target="_blank" rel="nofollow"></a>current time<div id="clock_block_cb650886c4849ee"></div></div>
<script type="text/javascript" src="//w.24timezones.com/l.js" async></script>
</center>
You have 5 minutes to complete the quiz.