Skip to content

Commit c9c612b

Browse files
committed
Added exercises in Lecture 5
1 parent 6136a79 commit c9c612b

17 files changed

+1992
-1462
lines changed
File renamed without changes.

Diff for: EXERCISES/Data/Lab_Data.xlsx

6.28 KB
Binary file not shown.

Diff for: EXERCISES/Data/Replica_1.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
x_3 , y_3
2+
0.01 , 20.235
3+
0.272632 , 25.5319
4+
0.535263 , 34.473
5+
0.797895 , 32.0662
6+
1.06053 , 43.7387
7+
1.32316 , 42.0162
8+
1.58579 , 60.352
9+
1.84842 , 83.2927
10+
2.11105 , 90.7923
11+
2.37368 , 76.4588
12+
2.63632 , 173.847
13+
2.89895 , 143.544
14+
3.16158 , 168.587
15+
3.42421 , 183.321
16+
3.68684 , 259.777
17+
3.94947 , 233.873
18+
4.21211 , 322.399
19+
4.47474 , 556.162
20+
4.73737 , 613.347
21+
5.0 , 648.405

Diff for: EXERCISES/Data/Replica_2.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
x_3 , y_3
2+
0.01 , 20.1566
3+
0.272632 , 30.359
4+
0.535263 , 30.7784
5+
0.797895 , 39.5152
6+
1.06053 , 41.0238
7+
1.32316 , 40.9458
8+
1.58579 , 40.3884
9+
1.84842 , 101.613
10+
2.11105 , 65.9388
11+
2.37368 , 157.715
12+
2.63632 , 126.18
13+
2.89895 , 166.295
14+
3.16158 , 211.261
15+
3.42421 , 225.927
16+
3.68684 , 193.993
17+
3.94947 , 284.291
18+
4.21211 , 316.048
19+
4.47474 , 513.524
20+
4.73737 , 649.256
21+
5.0 , 613.421

Diff for: EXERCISES/Data/Replica_3.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
x_3 , y_3
2+
0.01 , 20.2485
3+
0.272632 , 25.1977
4+
0.535263 , 30.0595
5+
0.797895 , 38.4903
6+
1.06053 , 32.3859
7+
1.32316 , 54.6471
8+
1.58579 , 68.9805
9+
1.84842 , 62.2803
10+
2.11105 , 114.592
11+
2.37368 , 90.5158
12+
2.63632 , 139.98
13+
2.89895 , 129.452
14+
3.16158 , 179.73
15+
3.42421 , 208.27
16+
3.68684 , 276.969
17+
3.94947 , 304.246
18+
4.21211 , 372.051
19+
4.47474 , 429.018
20+
4.73737 , 557.862
21+
5.0 , 723.059

Diff for: EXERCISES/Exercises_Lecture_02.ipynb

+55-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
{
1212
"cell_type": "code",
13-
"execution_count": 6,
13+
"execution_count": 1,
1414
"metadata": {},
1515
"outputs": [],
1616
"source": [
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"cell_type": "code",
76-
"execution_count": 8,
76+
"execution_count": 2,
7777
"metadata": {},
7878
"outputs": [
7979
{
@@ -83,13 +83,13 @@
8383
"<IPython.core.display.Image object>"
8484
]
8585
},
86-
"execution_count": 8,
86+
"execution_count": 2,
8787
"metadata": {},
8888
"output_type": "execute_result"
8989
}
9090
],
9191
"source": [
92-
"Image(filename='PYTHON.png')"
92+
"Image(filename='Figures/PYTHON.png')"
9393
]
9494
},
9595
{
@@ -101,7 +101,7 @@
101101
},
102102
{
103103
"cell_type": "code",
104-
"execution_count": 12,
104+
"execution_count": 3,
105105
"metadata": {},
106106
"outputs": [
107107
{
@@ -111,13 +111,13 @@
111111
"<IPython.core.display.Image object>"
112112
]
113113
},
114-
"execution_count": 12,
114+
"execution_count": 3,
115115
"metadata": {},
116116
"output_type": "execute_result"
117117
}
118118
],
119119
"source": [
120-
"Image(filename='triangle.png')"
120+
"Image(filename='Figures/triangle.png')"
121121
]
122122
},
123123
{
@@ -249,10 +249,10 @@
249249
" multi = False\n",
250250
" for j in i:\n",
251251
" if multi:\n",
252-
" new_value *= j\n",
252+
" new_value = new_value*j\n",
253253
" multi = False\n",
254254
" else:\n",
255-
" new_value += j\n",
255+
" new_value = new_value+j\n",
256256
" multi = True\n",
257257
" new_list.append(new_value)\n",
258258
"print(new_list)"
@@ -276,6 +276,27 @@
276276
" * Create the follwing matrix: [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]"
277277
]
278278
},
279+
{
280+
"cell_type": "code",
281+
"execution_count": 1,
282+
"metadata": {},
283+
"outputs": [
284+
{
285+
"name": "stdout",
286+
"output_type": "stream",
287+
"text": [
288+
"[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196, 203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294, 301, 308, 315, 322, 329, 336, 343, 350, 357, 364, 371, 378, 385, 392, 399, 406, 413, 420, 427, 434, 441, 448, 455, 462, 469, 476, 483, 490, 497, 504, 511, 518, 525, 532, 539, 546, 553, 560, 567, 574, 581, 588, 595, 602, 609, 616, 623, 630, 637, 644, 651, 658, 665, 672, 679, 686, 693, 700, 707, 714, 721, 728, 735, 742, 749, 756, 763, 770, 777, 784, 791, 798, 805, 812, 819, 826, 833, 840, 847, 854, 861, 868, 875, 882, 889, 896, 903, 910, 917, 924, 931, 938, 945, 952, 959, 966, 973, 980, 987, 994]\n"
289+
]
290+
}
291+
],
292+
"source": [
293+
"l=[]\n",
294+
"for i in range(1,1001):\n",
295+
" if i%7==0:\n",
296+
" l.append(i)\n",
297+
"print(l)"
298+
]
299+
},
279300
{
280301
"cell_type": "code",
281302
"execution_count": 4,
@@ -290,10 +311,34 @@
290311
}
291312
],
292313
"source": [
293-
"l=[i for i in range(1,1001) if i%7==0]\n",
314+
"l=[2*i+7 for i in range(1,1001) if i%7==0]\n",
294315
"print(l)"
295316
]
296317
},
318+
{
319+
"cell_type": "code",
320+
"execution_count": 2,
321+
"metadata": {},
322+
"outputs": [
323+
{
324+
"data": {
325+
"text/plain": [
326+
"'8'"
327+
]
328+
},
329+
"execution_count": 2,
330+
"metadata": {},
331+
"output_type": "execute_result"
332+
}
333+
],
334+
"source": [
335+
"a=89765\n",
336+
"l=str(a)\n",
337+
"l=['8', '9', '7', '6']\n",
338+
"\n",
339+
"'7' in l"
340+
]
341+
},
297342
{
298343
"cell_type": "code",
299344
"execution_count": 5,

Diff for: EXERCISES/Exercises_Lecture_03.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@
395395
"source": [
396396
"import pandas as pd\n",
397397
"\n",
398-
"d_in = pd.read_excel(\"Data.xlsx\").to_dict(orient='list')\n",
398+
"d_in = pd.read_excel(\"./Data/Data.xlsx\").to_dict(orient='list')\n",
399399
"d_out = {'username':[], 'name':[], 'surnames':[], 'email':[]}\n",
400400
"\n",
401401
"for name_surnames, mail in zip(d_in['Nombres y Apellidos'], d_in['Correo']):\n",
@@ -794,7 +794,7 @@
794794
"\n",
795795
"**Last Red Pill revelation**: It turns out that the function $\\frac{1}{\\sqrt{1-x^2}}$ is very problematic as we approach the edges in $x=-1$ and 1, since it gets towards infinity. We can avoid this problem by noting the following. Take an equilateral triangle with the three sides equal 1. If you fit three equilateral triangles of side 1, into a trapezoid as in the image (as a partial \"triforce\"), you will see that their shared edges that have length one, can be thought of as the radious of a half circumference. Since the three triangles are the same, this proves **their shared edges divide the half circumference in three equal arcs** (each will have length $\\pi/3$. Finally, since the height of the equilateral triangle cuts the opposite edge in two segments of same length $1/2$, we see that the $x$ coordinate at which the half circumference is cut in three is $x=1/2$. This proves that the **length of the circumference from $x=-1/2$ to $x=1/2$ is exactly** one third of the circumference, that is, **$\\pi/3$**.\n",
796796
"\n",
797-
"<img src=\"https://raw.githubusercontent.com/LLACorp/Python-Course-for-Scientific-Programming/master/EXERCISES/proof.png\" alt=\"Drawing\" style=\"width: 800px;\"/>\n",
797+
"<img src=\"https://raw.githubusercontent.com/LLACorp/Python-Course-for-Scientific-Programming/master/EXERCISES/Figures/proof.png\" alt=\"Drawing\" style=\"width: 800px;\"/>\n",
798798
"\n",
799799
"Therefore, we can also compute $\\pi$ avoiding the problems of the function geting arbitrarily large, using the integral only in $x\\in(-0.5, 0.5)$:\n",
800800
"$$\n",

Diff for: EXERCISES/Exercises_Lecture_05.ipynb

+313-3
Large diffs are not rendered by default.
File renamed without changes.

Diff for: EXERCISES/Figures/from_excel.png

112 KB
Loading

Diff for: EXERCISES/Figures/machine.png

48.5 KB
Loading
File renamed without changes.
File renamed without changes.

Diff for: LECTURES/Lecture_05.ipynb

+22-13
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
"\n",
1414
"Jan Scarabelli Calopa: Jan.Scarabelli@e-campus.uab.cat \n",
1515
"\n",
16-
"\n",
17-
"All the course material can be found at: https://llacorp.github.io/Python-Course-for-Scientific-Programming/ \n",
18-
"\n",
1916
"---"
2017
]
2118
},
@@ -811,17 +808,19 @@
811808
"metadata": {},
812809
"source": [
813810
"## Saving and Loading a Dictionary: the JSON format\n",
814-
"There is a standard format for data structures akin to Python dictionaries, called `JSON`, which is used in many data bases for example. Thus, many times data you download from a data base will be in this format. It is useful to know how to import those files and how to export your dictionaries back to files you can share, since it is just a line!\n",
811+
"In general all the functions that read or write to files that you will find in Python libraries (e.g. the one we saw to import Excel files), use the `open()` function and file handlers.\n",
812+
"\n",
813+
"Another example is the `json`library. There is a standard format for data structures akin to Python dictionaries, called `JSON`, which is used in many data bases for example. Thus, many times data you download from a data-base will be in this format. It is useful to know how to import those files and how to export your dictionaries back to files you can share. You could create a function that reads it using `open()` and do all the parsing, but somebody has already doen that for you.\n",
815814
"\n",
816-
"We will explain it more deeply in coming lectures, but will need to first write `import json` (you import a set of functions related with it), then there are two functions you will use inside:\n",
815+
"We will need to first write `import json` (you import a set of functions related with it), then there are two functions you will use inside:\n",
817816
"\n",
818817
"#### `json.dump(<dictionary>, <file_handler_to_save>)` \n",
819-
"We place as first argument the dictionary and then the file jandler of the new file to write (and if you wish the full or relative path, but do not worry, we will explain what this means in a future lecture). If you only place the name of the file (the standard way is to put it the `.json` extension), the file will be generated in the same place as where the notebook is."
818+
"We place as first argument the dictionary and then the file handler of the new file to write. If you only place the name of the file (the standard way is to put it the `.json` extension), the file will be generated in the same place as where the notebook is."
820819
]
821820
},
822821
{
823822
"cell_type": "code",
824-
"execution_count": null,
823+
"execution_count": 1,
825824
"metadata": {},
826825
"outputs": [],
827826
"source": [
@@ -834,7 +833,7 @@
834833
},
835834
{
836835
"cell_type": "code",
837-
"execution_count": null,
836+
"execution_count": 2,
838837
"metadata": {},
839838
"outputs": [],
840839
"source": [
@@ -851,16 +850,25 @@
851850
"source": [
852851
"#### `json.load(<file_handler_to_load>)`\n",
853852
"\n",
854-
"We place the "
853+
"We place the file handler as the argument of the function and it will return a dictionary. As simple as that."
855854
]
856855
},
857856
{
858857
"cell_type": "code",
859-
"execution_count": null,
858+
"execution_count": 3,
860859
"metadata": {},
861-
"outputs": [],
860+
"outputs": [
861+
{
862+
"name": "stdout",
863+
"output_type": "stream",
864+
"text": [
865+
"{'People': ['Arnau', 'Gerard', 'Artemis', 'Jan', 'Xabier'], 'Surnames': ['Parrilla', 'Navarro', 'Llabrés', 'Scarabelli', 'Oianguren'], 'Favourite Number': [1, 2, 3, 4, 5], 'Counter': 1000}\n"
866+
]
867+
}
868+
],
862869
"source": [
863-
"D = json.load( open(\"MyDictionary.json\", \"r\"))\n",
870+
"with open(\"MyDictionary.json\", \"r\") as handler: \n",
871+
" D = json.load( handler )\n",
864872
"print(D)"
865873
]
866874
},
@@ -2045,7 +2053,8 @@
20452053
"os.remove(\"New.txt\")\n",
20462054
"os.remove(\"New2.txt\")\n",
20472055
"os.remove(\"npyfile.npy\")\n",
2048-
"os.remove(\"npzfile.npz\")"
2056+
"os.remove(\"npzfile.npz\")\n",
2057+
"os.remove(\"MyDictionary.json\")"
20492058
]
20502059
},
20512060
{

0 commit comments

Comments
 (0)