-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-03-02.qmd
201 lines (146 loc) · 5.07 KB
/
slides-03-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
---
title: "built-in functions (slides)"
format: revealjs
slide-number: true
---
# CSC 110 Python Functions
## Built-in Functions
- `print()`
- `round()`
- `type()`
- `input()`
- `len()`
- `int()`
- `float()`
## Write a function
Write a Python function that does the following:
1. Its name is `greeting`
2. It takes two arguments, `first_name` and `last_name`
3. It returns a string with a greeting using `first_name` and `last_name`
```{python}
#| echo: true
#| eval: false
print( greeting("Mickey", "Mouse") ) # Hello, Mickey Mouse!
```
## `input()` function
- The `input()` function prompts the user to input text in the standard output
- Whatever is inside the parentheses in `input()` will be written to the standard output (without a trailing newline, which you can add using `\n`).
- The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that
- `input()` always returns a string
```{python}
#| echo: true
#| eval: false
input("What's your name?\n")
```
## Write a function
Write a Python function that does the following:
1. Its name is `greeting_again` with no parameters
2. It first prompts you to enter your first name
3. It again prompts you to enter your last name
3. It returns a string with the same greeting as `greeting` but replace with your first and last name
```{python}
#| echo: true
#| eval: false
print( greeting_again() )
```
## Write a function
```{python}
#| echo: true
#| eval: false
def greeting(first, last):
message = "Hello, " + first + " " + last + "!"
return message
def greeting_again():
first = input("Enter your first name:\n")
last = input("Enter your last name:\n")
message = "Hello, " + first + " " + last + "!"
return message
def main():
print(greeting("Mickey", "Mouse"))
print(greeting_again())
main()
```
## `len()` function
- The `len()` function can be used with many types -- we will be using it with `string` for now
- It returns the number of characters in a string
```{python}
#| echo: true
#| eval: true
character_count = len("Mickey")
print(character_count)
```
## Write a function
Write a Python function that does the following:
1. Its name is `count_characters`
2. It takes three string arguments, `a`, `b` and `c`
3. It returns the total number of characters for all three strings
```{python}
#| echo: true
#| eval: false
print( count_characters("hel", "lo", "world") ) # 10
print( count_characters("", "", "") ) # 0
print( count_characters(" ", " ", " ") ) # 3
print( count_characters("10", "2", "3") ) # 4
```
## Write a function
```{python}
#| echo: true
#| eval: true
def count_characters(a, b, c):
return len(a) + len(b) + len(c)
def main():
print( count_characters("hel", "lo", "world") ) # 10
print( count_characters("", "", "") ) # 0
print( count_characters(" ", " ", " ") ) # 3
print( count_characters("10", "2", "3") ) # 4
main()
```
## Quiz 03
<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 10 minutes to complete the quiz.
Your formula in your function should be Python code (in other words, do not just copy the formula in the instructions, make it so it uses **actual symbols for Python operators**)
## `int()` function
- The `int()` function can be used to convert a string to an integer type
- It only works if the string only contains digits
```{python}
#| echo: true
#| eval: true
age = '35'
age_int = int(age)
print(type(age), type(age_int))
```
## `float()` function
- The `float()` function can be used to convert a string to a float type
- It only works if the string only contains digits and optionally a decimal point
```{python}
#| echo: true
#| eval: true
age = '35'
age_float = float(age)
print(type(age), type(age_float))
```
## Write a function
Write a Python function `calculate_year_born` with no parameters. It prompts user to enter their age `input()`.
It converts user's `age` to integer and calculates (imperfectly) the year a person of `age` was born by subtracting `age` from `2025`.
It returns an integer representing the approximate year person of `age` was born.
```{python}
#| echo: true
#| eval: false
print( calculate_year_born() ) # user enters 60, function returns 1965
```
## Write a function
```{python}
#| echo: true
#| eval: false
def calculate_year_born():
str_age = input("what's your age?\n")
age = int(str_age)
year_born = 2025 - age
return year_born
def main():
print(calculate_year_born())
main()
```