Newer
Older
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"\n",
"# Scientific notation\n",
"\n",
"**Learning goals:**\n",
"\n",
"* Scientific notation for very small and very large floating point numbers\n",
"\n",
"**Starting Out with Python:**\n",
"\n",
"* Chapter. 2.8"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"### Useful information about formatting large numbers"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"When writing floats in Python it is often easiest to simply write the number as is, for instance 9.80665 or 0.0002. If the number is very large (~$10^{6}$) or very small (~$10^{-6}$), it can be time consuming to write the whole number, and it can be easy to make a mistake. A mistake can ruin the results of a calculation. For this reason it is common to use scientific notation for small and for large numbers.\n",
"\n",
"In Python we can write the numbers 3.0 × 10 $^{9}$ and 3.19 × 10$^{-10}$ using multiplication (`*`) and exponentiation (`**`) like this: `3.0*10**9` and `3.19*10 **(-10)`. This gives the correct result, but it wastes time and power by doing useless calculations, first exponentiation, then multiplication, to find a number that we already know. It is better to use the notation `3.0e9` and `3.19e-10`, where the number behind `e` denotes the power of 10 needed to express our number. \n",
"\n",
"The notation using `e` (having nothing to do with Eulers number e) lets us input our number into the variable without doing calculations. The example below shows this notation in use in line 3.\n",
"\n",
"Example code (**Try running it!**)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-01T12:28:51.461020Z",
"start_time": "2019-07-01T12:28:46.220125Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"What is your personal best time for the 60-meter dash? 8\n",
"Good! But in order to catch up to the light ...\n",
"...you have to run 40000000.0 times faster\n",
"we can write this as 4.0e+07 times faster.\n"
]
}
],
"source": [
"time = float(input(\"What is your personal best time for the 60-meter dash? \"))\n",
"m_sec = 60/time\n",
"light_speed = 3.0e8\n",
"x = light_speed / m_sec\n",
"print(\"Good! But in order to catch up to the light ...\")\n",
"print(\"...you have to run\", x, \"times faster\")\n",
"print(\"we can write this as\", format(x, '.1e'), \"times faster.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"`.1e` in format shows that we want one decimal in the scientific notation. `.2e` would give `4.25e+07`."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"## a) Physics / chemestry"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false
},
"source": [
"Avogadros number 6.022 × 10<sup>23</sup> tells us how many molecules of a substance that is found in one mole of that substance. Make a program that asks the user for the name of a substance she has, what molar weight this substance has and how much of the substance she has. The program should then print how many molecules the user has of this substance to the screen. Use the `format()`-function to avoid having too many decimals in your answer. \n",
"\n",
"Example of running:\n",
"\n",
" \n",
"```python\n",
"Write a substance you have: water\n",
"What is the molar weight in grams for water? 18\n",
"How many grams of water do you have? 500\n",
"You have 1.7e+25 molecules of water\n",
"```\n",
"\n",
"***Write your code in the block below.***"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"#### Hint"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"run_control": {
"frozen": true
}
},
"source": [
"Hint 1: `print(\"...you have to run\", x, \"times faster\")` allows a variable like `x` to be listed together with text strings because `print()` can take several arguments. The function `input()` however, can only take one argument. If you want the name of the substance in the promt for the next input statements it therefore needs to be added together with another text rather than be listed with a comma.\n",
"\n",
"Hint 2: First calculate the number of moles by taking the amount of the substance in grammes and divide by the molar weight. Then multiply the amount of moles with avogadros number."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"## b) Music"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"run_control": {
"frozen": true
}
},
"source": [
"The amount of possible 10 tone melody-lines (including rythmic variations) is estimated to be 8.25 x 10<sup>19</sup>, from https://plus.maths.org/content/how-many-melodies-are-there. Make a program in the same style as the 60-meter program above that asks the user how many different 10 tone melodies she thinks she has composed herself or heard, then prints how big of a fraction of the possible melodies this constitutes. (The end result will probably be a very small number here).\n",
" \n",
"\n",
"Example run:\n",
"\n",
" \n",
"```python\n",
"The possible 10 tone melodies you have heard? 3288\n",
"You have heard 3.985454545454546e-15 percent of possible melodies.\n",
"```\n",
"\n",
"***Write your code in the block below.***"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"heading_collapsed": true,
"run_control": {
"frozen": true
}
},
"source": [
"#### Hint"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"hidden": true,
"run_control": {
"frozen": true
}
},
"source": [
"Take the number of different 10 tone melodies you have heard and divide by the number of possible melody-lines, then convert the number to a percentage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Edit Metadata",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}