Skip to content

Commit 2894b37

Browse files
committed
Update lesson for 2020
1 parent 7bb9cda commit 2894b37

File tree

1 file changed

+51
-136
lines changed

1 file changed

+51
-136
lines changed

L1/a-taste-of-python.ipynb

Lines changed: 51 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"source": [
1010
"# A taste of Python\n",
1111
"\n",
12+
"University of Helsinki students are encouraged to use the CSC Notebooks platform.<br/>\n",
13+
"<a href=\"https://notebooks.csc.fi/#/blueprint/7e62ac3bddf74483b7ac7333721630e2\"><img alt=\"CSC badge\" src=\"https://img.shields.io/badge/launch-CSC%20notebook-blue.svg\" style=\"vertical-align:text-bottom\"></a>\n",
14+
"\n",
1215
"We will start our Python lesson by learning a bit of the basic operations you can perform using Python.\n",
1316
"\n",
1417
"## General information\n",
@@ -23,7 +26,7 @@
2326
"\n",
2427
"### Getting started\n",
2528
"\n",
26-
"Presumably if you have made it this far, you have already opened this Jupyter Notebook using Binder, the CSC notebooks server, or on your own computer. If not, you can launch [Jupyter Lab](http://jupyterlab.readthedocs.io/en/stable/) by clicking on either of the launch buttons at the top of this document or by typing the following in a terminal window.\n",
29+
"Presumably if you have made it this far, you have already opened this Jupyter Notebook using Binder, the CSC notebooks server, or on your own computer. If not, you can launch [Jupyter Lab](http://jupyterlab.readthedocs.io/en/stable/) by clicking on the launch button at the top of this document, using the launch Binder icon at the top of the page, or by typing the following in a terminal window.\n",
2730
"\n",
2831
"```bash\n",
2932
"$ jupyter lab\n",
@@ -83,7 +86,7 @@
8386
"editable": true
8487
},
8588
"source": [
86-
"If you want to edit and re-run some code, simply make changes to the cell and press **Shift-Enter** to execute the revised code."
89+
"If you want to edit and re-run some code, simply make changes to the cell and press **Shift-Enter** to execute the modified code."
8790
]
8891
},
8992
{
@@ -95,9 +98,9 @@
9598
"source": [
9699
"### Functions\n",
97100
"\n",
98-
"You can use Python for more advanced math by using functions. Functions are pieces of code that perform a single action such as printing information to the screen (e.g., the ``print()`` function). Functions exist for a huge number of operations in Python.\n",
101+
"You can use Python for more advanced math by using a *function (funktio)*. Functions are pieces of code that perform a single action such as printing information to the screen (e.g., the `print()` function). Functions exist for a huge number of operations in Python.\n",
99102
"\n",
100-
"Let's try out a few simple examples using functions to find the sine or square root of a value. You can type ``sin(3)`` or ``sqrt(4)`` into the cells below to test this out."
103+
"Let's try out a few simple examples using functions to find the sine or square root of a value. You can type `sin(3)` or `sqrt(4)` into the cells below to test this out."
101104
]
102105
},
103106
{
@@ -109,7 +112,10 @@
109112
"editable": true,
110113
"jupyter": {
111114
"outputs_hidden": false
112-
}
115+
},
116+
"tags": [
117+
"raises-exception"
118+
]
113119
},
114120
"outputs": [],
115121
"source": []
@@ -123,7 +129,10 @@
123129
"editable": true,
124130
"jupyter": {
125131
"outputs_hidden": false
126-
}
132+
},
133+
"tags": [
134+
"raises-exception"
135+
]
127136
},
128137
"outputs": [],
129138
"source": []
@@ -157,7 +166,7 @@
157166
"| Division | `/` | `4 / 2` | `2` |\n",
158167
"| Exponentiation | `**` | `2**3` | `8` |\n",
159168
"\n",
160-
"For anything more advanced, we need to load a *module*. For math operations, this module is called *math* and it can be loaded by typing ``import math``. Try that below."
169+
"For anything more advanced, we need to load a *module (moduuli)* or *library (ohjelmakirjasto)*. For math operations, this module is called *math* and it can be loaded by typing `import math`. Try that below."
161170
]
162171
},
163172
{
@@ -319,7 +328,7 @@
319328
"source": [
320329
"### Variables\n",
321330
"\n",
322-
"*Variables* can be used to store values calculated in expressions and used for other calculations. Assigning value to variables is straightforward. To assign a value, you simply type ``variable_name = value``, where ``variable_name`` is the name of the variable you wish to define. In the cell below, define a variable called ``temp_celsius``, assign it a value of '10.0', and then print that variable value using the ``print()`` function. Note that you need to do this on two separate lines."
331+
"A *variable (muuttuja)* can be used to store values calculated in expressions and used for other calculations. Assigning value to variables is straightforward. To assign a value, you simply type ``variable_name = value``, where ``variable_name`` is the name of the variable you wish to define. In the cell below, define a variable called ``temp_celsius``, assign it a value of '10.0', and then print that variable value using the ``print()`` function. Note that you should do this on two separate lines."
323332
]
324333
},
325334
{
@@ -427,13 +436,7 @@
427436
"editable": true
428437
},
429438
"source": [
430-
"<div class=\"alert alert-warning\">\n",
431-
"\n",
432-
"**Warning**\n",
433-
"\n",
434-
"If you try to run some code that accesses a variable that has not yet been defined you will get a `NameError` message. Try printing out the value of the variable ``tempFahrenheit`` using the ``print()`` function in the cell below.\n",
435-
"\n",
436-
"</div>"
439+
"**Warning**: If you try to run some code that accesses a variable that has not yet been defined you will get a `NameError` message. Try printing out the value of the variable `tempFahrenheit` using the `print()` function in the cell below."
437440
]
438441
},
439442
{
@@ -445,7 +448,10 @@
445448
"editable": true,
446449
"jupyter": {
447450
"outputs_hidden": false
448-
}
451+
},
452+
"tags": [
453+
"raises-exception"
454+
]
449455
},
450456
"outputs": [],
451457
"source": []
@@ -457,14 +463,9 @@
457463
"editable": true
458464
},
459465
"source": [
460-
"<div class=\"alert alert-info\">\n",
461-
"\n",
462-
"**Note**\n",
463-
"\n",
464-
"One of the interesting things here is that if we define the undefined variable in a cell lower down in the notebook and execute that cell, we can return to the earlier cell and the code should now work. That was a bit of a complicated sentence, so let's test this all out. First, let's define a variable called ``tempFahrenheit`` in the cell below and assign it to be equal to ``9/5 * temp_celsius + 32``, the conversion factor from temperatures in Celsius to Fahrenheit. Then, return to the cell above this text and run that cell again. See how the error message has gone away? ``tempFahrenheit`` has now been defined and thus the cell above no longer generates a ``NameError`` when the code is executed.\n",
466+
"**Note**: One of the interesting things here is that if we define the undefined variable in a cell lower down in the notebook and execute that cell, we can return to the earlier cell and the code should now work. That was a bit of a complicated sentence, so let's test this all out. First, let's define a variable called `tempFahrenheit` in the cell below and assign it to be equal to `9/5 * temp_celsius + 32`, the conversion factor from temperatures in Celsius to Fahrenheit. Then, return to the cell above this text and run that cell again. See how the error message has gone away? `tempFahrenheit` has now been defined and thus the cell above no longer generates a `NameError` when the code is executed.\n",
465467
"\n",
466-
"Also, the number beside the cell, for example ``In [2]``, tells you the order in which the Python cells have been executed. This way you can see a history of the order in which you have run the cells.\n",
467-
"</div>"
468+
"Also, the number beside the cell, for example `In [2]`, tells you the order in which the Python cells have been executed. This way you can see a history of the order in which you have run the cells."
468469
]
469470
},
470471
{
@@ -533,7 +534,8 @@
533534
"source": [
534535
"### Data types\n",
535536
"\n",
536-
"There are 4 basic *data types* in Python as shown in the table below.\n",
537+
"A *data type (tietotyyppi)* determines the characteristics of data in a program.\n",
538+
"There are 4 basic data types in Python as shown in the table below.\n",
537539
"\n",
538540
"| Data type name | Data type | Example |\n",
539541
"| -------------- | -------------------- | ---------- |\n",
@@ -578,7 +580,10 @@
578580
"editable": true,
579581
"jupyter": {
580582
"outputs_hidden": false
581-
}
583+
},
584+
"tags": [
585+
"raises-exception"
586+
]
582587
},
583588
"outputs": [],
584589
"source": []
@@ -636,15 +641,11 @@
636641
"cell_type": "markdown",
637642
"metadata": {},
638643
"source": [
639-
"<div class=\"alert alert-warning\">\n",
640-
"\n",
641-
"**Warning**\n",
642-
"\n",
643-
"Jupyter Notebooks might sometimes get stuck when using the ``input()`` function. If this happens, restart the kernel and run the cell again (**Kernel** -> **Restart Kernel...**).\n",
644+
"```{warning}\n",
645+
"Jupyter Notebooks might sometimes get stuck when using the `input()` function. If this happens, restart the kernel and run the cell again (**Kernel** -> **Restart Kernel...**).\n",
644646
"\n",
645647
"Also, we realize these cells **will not render properly on the course website**, but should work just fine in Binder or using the CSC notebooks. Sorry.\n",
646-
"\n",
647-
"</div>"
648+
"```"
648649
]
649650
},
650651
{
@@ -656,7 +657,10 @@
656657
"editable": true,
657658
"jupyter": {
658659
"outputs_hidden": false
659-
}
660+
},
661+
"tags": [
662+
"raises-exception"
663+
]
660664
},
661665
"outputs": [],
662666
"source": []
@@ -677,7 +681,10 @@
677681
"editable": true,
678682
"jupyter": {
679683
"outputs_hidden": false
680-
}
684+
},
685+
"tags": [
686+
"raises-exception"
687+
]
681688
},
682689
"outputs": [],
683690
"source": []
@@ -697,7 +704,10 @@
697704
"execution_count": null,
698705
"metadata": {
699706
"deletable": true,
700-
"editable": true
707+
"editable": true,
708+
"tags": [
709+
"raises-exception"
710+
]
701711
},
702712
"outputs": [],
703713
"source": []
@@ -712,108 +722,13 @@
712722
{
713723
"cell_type": "code",
714724
"execution_count": null,
715-
"metadata": {},
725+
"metadata": {
726+
"tags": [
727+
"raises-exception"
728+
]
729+
},
716730
"outputs": [],
717731
"source": []
718-
},
719-
{
720-
"cell_type": "markdown",
721-
"metadata": {},
722-
"source": [
723-
"## Good coding practices - Selecting \"good\" variable names\n",
724-
"\n",
725-
"This course aims to introduce you to programming in Python, but also to *good programming practices*. These comprise practical tips based on practices used by professional programmers that help them write better programs that are easier to understand and share with others. To say it differently, there are many ways to learn to program, and we want to help you learn how to program the right way!\n",
726-
"\n",
727-
"This week our good programming practice is about selecting \"good\" variable names. \n",
728-
"\n",
729-
"### Some \"not-so-good\" variable names\n",
730-
"\n",
731-
"To illustrate the point, consider a few not-so-good examples below."
732-
]
733-
},
734-
{
735-
"cell_type": "code",
736-
"execution_count": null,
737-
"metadata": {},
738-
"outputs": [],
739-
"source": [
740-
"s = \"101533\""
741-
]
742-
},
743-
{
744-
"cell_type": "markdown",
745-
"metadata": {},
746-
"source": [
747-
"or"
748-
]
749-
},
750-
{
751-
"cell_type": "code",
752-
"execution_count": null,
753-
"metadata": {},
754-
"outputs": [],
755-
"source": [
756-
"sid = \"101533\""
757-
]
758-
},
759-
{
760-
"cell_type": "markdown",
761-
"metadata": {},
762-
"source": [
763-
"Any idea what these variables are for? Of course not, the variables ``s`` and ``sid`` are too short and cannot communicate what they should be used for in the code. You might think you know what they are for now, but imagine not looking at the code for a few months. Would you know then? What about if you share the code with a friend? Would they know? Probably not.\n",
764-
"\n",
765-
"Let's look at another example."
766-
]
767-
},
768-
{
769-
"cell_type": "code",
770-
"execution_count": null,
771-
"metadata": {},
772-
"outputs": [],
773-
"source": [
774-
"finnishmeteorologicalinstituteobservationstationidentificationnumber = \"101533\""
775-
]
776-
},
777-
{
778-
"cell_type": "markdown",
779-
"metadata": {},
780-
"source": [
781-
"OK, so now we have another issue. The variable name ``finnishmeteorologicalinstituteobservationstationidentificationnumber`` potentially provides more information about what the variable represents (the identification number of an FMI observation station), but it does so in a format that is not easy to read, nor something you're likely to want to type more than once. The previous example was too short, and now we have a variable name that is too long (and hard to read as a result)."
782-
]
783-
},
784-
{
785-
"cell_type": "markdown",
786-
"metadata": {},
787-
"source": [
788-
"### Selecting \"good\" variable names\n",
789-
"\n",
790-
"A good variable name should:\n",
791-
"\n",
792-
"1. Be clear and concise. \n",
793-
"\n",
794-
"2. Be written in English. A general coding practice is to write code with variable names in English, as that is the most likely common language between programmers. Thus, variable names such as ``muuttuja`` (which is also not a good name on other levels) should be avoided.\n",
795-
"\n",
796-
"3. Not contain special characters. Python supports use of special characters by way of various encoding options that can be given in a program. That said, it is better to avoid variables such as ``lämpötila`` because encoding issues can arise in some cases. Better to stick to the [standard printable ASCII character set](https://en.wikipedia.org/wiki/ASCII#Printable_characters) to be safe.\n",
797-
"\n",
798-
"4. Not conflict with any [Python keywords](https://www.pythonforbeginners.com/basics/keywords-in-python), such as ``for``, ``True``, ``False``, ``and``, ``if``, or ``else``. These are reserved for speical operations in Python and cannot be used as variable names.\n",
799-
"\n",
800-
"With this in mind, let's now look at a few better options for variable names."
801-
]
802-
},
803-
{
804-
"cell_type": "markdown",
805-
"metadata": {},
806-
"source": [
807-
"### Formatting \"good\" variable names\n",
808-
"\n",
809-
"There are several possibilities for \"good\" variable name formats, of which we'll consider two:\n",
810-
"\n",
811-
"1. *pothole_case_naming* uses lowercase words separated by underscores ``_``. This is our suggested format as the underscores make it easy to read the variable, and don't add too much to the length of the variable name. As an example, consider the variable ``temp_celsius``. In the context of the examples above, we might rename our variable ``fmi_station_id``, which conveys all of the essential information we need, while remaining easy to read.\n",
812-
"\n",
813-
"2. *camelCase* or *CamelCase* uses capitalization of the first letter of words in a variable name to make it easier to read. In some cases the first letter of the variable may be capitalized. The variable ``tempFahrenheit`` was one example of camelCase. Again, if we consider the examples from the previous section, we might consider the variable name ``fmiStationID`` or simply ``stationID`` if we elect to use camelCase.\n",
814-
"\n",
815-
"As mentioned in point 1, pothole_case_naming is preferred, mostly because we feel it is the easiest to read and seems to be most common amongst Python programmers. We are happy with you using either option, as long as you are consistent in the use. It might take an extra second of thought, but could make a big difference in the ease of use of your programs!"
816-
]
817732
}
818733
],
819734
"metadata": {
@@ -833,7 +748,7 @@
833748
"name": "python",
834749
"nbconvert_exporter": "python",
835750
"pygments_lexer": "ipython3",
836-
"version": "3.7.3"
751+
"version": "3.7.4"
837752
}
838753
},
839754
"nbformat": 4,

0 commit comments

Comments
 (0)