Skip to content

Commit

Permalink
Merge pull request #50 from RENCI-NRIG/escott
Browse files Browse the repository at this point in the history
Added to a couple of the examples, fixed typos, clarified.
  • Loading branch information
erikscott authored Jun 4, 2024
2 parents 2cdda60 + f0c1e99 commit 0fc30ef
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 117 deletions.
22 changes: 11 additions & 11 deletions theme1/PE100/PE100-01Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
"source": [
"# Programming Essentials - Python Programming and Jupyter Notebooks (PE100)\n",
"\n",
"Welcome to the **Programming Essentials - Python Programming and Jupyter Notebooks** component of the X-CITE training materials. The intent behind this unit is to teach new CHESS users how to write programs in Python for experimental data analysis.\n",
"Welcome to the **Programming Essentials - Python Programming and Jupyter Notebooks** component of the X-CITE training materials. The intent behind this unit is to show new CHESS users how to write programs in Python for experimental data analysis.\n",
"\n",
"Prerequisites? None.\n",
"\n",
"The training materials before you are designed both for scientists who may not have any programming experience whatsoever and for those who have at least some basic programming capability but in a language other than Python.\n",
"\n",
"Python's adoption has exploded in the last decade. Much of its success can be attributed to productivity. Many programming languages force the programmer to deal with very small details to do even the simplest things. Python's attitude is to just take care of all the minutae so we don't have to. On top of that, Python's popularity has resulted in hundreds of thousands of collections of useful code for specific tasks. If there is something you need to write a program for, it's almost definite someone else has had the same problem. There's a good chance at least one of those people neatly packaged up some of their code and made it available in one of the repositories on the internet. There's no reason for you to reinvent the proverbial wheel again - take advantage of their work (don't forget to cite it!) and get back to doing actual science that much sooner.\n",
"Python's adoption has exploded in the last decade. Much of its success can be attributed to productivity. Many programming languages force the programmer to deal with very small details to do even the simplest things. Python's attitude is to just take care of all the minutae so we don't have to. On top of that, Python's popularity has resulted in hundreds of thousands of packages of useful code for specific tasks. If there is something you need to write a program for, it's almost definite that someone else has had the same problem. There's a good chance at least one of those people neatly wrapped up some of their code and made it available in one of the repositories on the internet. There's no reason for you to reinvent the proverbial wheel again - take advantage of their work (don't forget to cite it!) and get back to doing actual science that much sooner.\n",
"\n",
"You're currently looking at [PE100-01 Introduction](PE100-01Introduction.ipynb). If you are new to Python and especially if you're new to programming, you should work through each of the modules in order. More experienced programmers might benefit from skipping directly to topics that interest them. Select one of the following:\n",
"\n",
"- [PE100-02 Types, Variables, and Operators](PE100-02TypesVarsAndOperators.ipynb)\n",
"- [PE100-03 Decision Structures](PE100-03DecisionStructures.ipynb)\n",
"- [PE100-04 Repetition](PE100-04Repetition.ipynb)\n",
"- [PE100-05 Functions](PE100-05Functions.ipynb)\n",
"- [PE100-06 Files](PE100-06Files.ipynb)\n",
"- [PE100-07 Exceptions](PE100-07Exceptions.ipynb)\n",
"- [PE100-08 Lists](PE100-08Lists.ipynb)\n",
"- [PE100-09 Strings](PE100-09Strings.ipynb)\n",
"- [PE100-10 Dictionaries](PE100-10Dictionaries.ipynb)\n",
"- [PE100-02 Types, Variables, and Operators](PE100-02TypesVarsAndOperators.ipynb) - the heart of any programming language.\n",
"- [PE100-03 Decision Structures](PE100-03DecisionStructures.ipynb) - conditional statements (\"if\" statements\") change the program flow.\n",
"- [PE100-04 Repetition](PE100-04Repetition.ipynb) - \"while\" and \"for\" loops let us do things over and over.\n",
"- [PE100-05 Functions](PE100-05Functions.ipynb) - Python comes with a lot of functions, but we can write even more.\n",
"- [PE100-06 Files](PE100-06Files.ipynb) - Reading input from and storing your results to disk.\n",
"- [PE100-07 Exceptions](PE100-07Exceptions.ipynb) - Dealing with unexpected contingencies.\n",
"- [PE100-08 Lists](PE100-08Lists.ipynb) - Another kind of variable, and the key to structuring data storage.\n",
"- [PE100-09 Strings](PE100-09Strings.ipynb) - More details on working with text.\n",
"- [PE100-10 Dictionaries](PE100-10Dictionaries.ipynb) - Like a simple database, look up information quickly.\n",
"\n",
"\n",
"\n"
Expand Down
141 changes: 73 additions & 68 deletions theme1/PE100/PE100-02TypesVarsAndOperators.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,19 @@
"source": [
"# PE100-02: Types, Variables, and Operators\n",
"\n",
"Niklaus Wirth was one of the founding giants of Computer Science. He wrote an introductory textbook whose title neatly summed up the act and art of programming: [Algorithms + Data Structures = Programs](https://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs). Data Structures are how information is stored in a computer, and algorithms are the instructions the computer applies to transform that data.\n",
"\n",
"Let's begin our exploration of Python by looking at a few basic kinds of data. Watch the video in the next cell (right below this one, even though it might not be obvious it's a second cell) and it will guide you through some experiments."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7895dbfe-1868-47b4-a23e-0536a47abd91",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e4b56db7-f7c7-4416-bbad-c1e9649fbcc2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "a122c50c-a867-4e93-9cca-6019c73fedbb",
"metadata": {},
"source": [
"*Video for the next four code cells (ints, reals, strings, and printing) goes here*"
"Niklaus Wirth was one of the founding giants of Computer Science. He wrote an introductory textbook whose title neatly summed up the act and art of programming: [Algorithms + Data Structures = Programs](https://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs). Data Structures are how information is stored in a computer, and algorithms are the instructions the computer applies to transform that data.\n"
]
},
{
"cell_type": "markdown",
"id": "2f37dae2-3f6f-4331-8394-b2d141123de3",
"metadata": {},
"source": [
"To run the code in a cell, first click in the cell to select it. Then you can either\n",
"To run the code in a cell, first click in the cell to select it. Then you can **either**:\n",
"1. Go to the \"Run\" menu and choose \"Run Selected Cells\", or\n",
"1. Just press Shift + Enter.\n",
"\n",
"Let's do this now: click right below where it says \"print (403.616\"), then Go to the \"Run\" menu and choose \"Run Selected Cells\"."
"Let's do this now: click just below where it says \"print (403.616\"), then Go to the \"Run\" menu and choose \"Run Selected Cells\"."
]
},
{
Expand Down Expand Up @@ -91,7 +65,9 @@
"id": "81da8b1a-f21b-49bb-a7ca-a7f2420285ca",
"metadata": {},
"source": [
"In Python, and in Jupyter notebooks, if the last line evaluates to some value then it will be printed out. That's how \"103.5\" got printed - a literal number evaluates to that number when it's run. A \"literal number\" means you look at it in your code and you _literally see a number_."
"In Python, and in Jupyter notebooks, if the last (or only!) line evaluates to some value then it will be printed out. That's how \"103.5\" got printed - a literal number evaluates to that number when it's run. A \"literal number\" means you look at it in your code and you _literally see a number_.\n",
"\n",
"Take a look at a _string literal_. Run each of the next two cells..."
]
},
{
Expand Down Expand Up @@ -120,23 +96,16 @@
"metadata": {},
"source": [
"At this point, we can use Python and Jupyter Lab as a scientific calculator. We have some literals of different types (int, real, and string, so far) and we can print them out with the ```print()``` *function*. If we don't explicitly print anything at the end of a cell, Python will show us the last value that was computed.\n",
"\n",
"\n",
"\n",
"Take a look at the next video."
"\n"
]
},
{
"cell_type": "markdown",
"id": "8c570718-4ec3-45a7-8fd7-5d1d4dd39d41",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "c20f366f-062b-435b-9d03-bd7f0d6624e7",
"metadata": {},
"source": [
"## Operators\n",
"\n",
"Like any programming language, Python lets you \"do math\" and lots of other things. Let's take a look at some of the basic \"operators\". In all of the code-containing cells through this course, try to predict what will happen first, and then run the code."
]
},
Expand Down Expand Up @@ -247,10 +216,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "1177f56b-554c-4c41-b85a-af6a2bc73a6a",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"256"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2**8"
]
Expand Down Expand Up @@ -334,6 +314,8 @@
"id": "21530437-db65-4639-a928-13808271cc0e",
"metadata": {},
"source": [
"## Variables\n",
"\n",
"Unless we just use Jupyter as a big, expensive scientific calculator, we need a way to store data. *Variables* were invented for just that purpose, and virtually every language has them. Think of them as a place to store data of some kind, and that place has a name. They behave in Python just like you'd expect."
]
},
Expand Down Expand Up @@ -422,7 +404,7 @@
"id": "e60eb2cf-ffdd-42a6-aa53-3cb8ed98e673",
"metadata": {},
"source": [
"In the last line, we just put ```watts``` because Jupyter automatically prints what the last line evaluates to (unless there has been a print statement before that).\n",
"In the last line, we just put ```watts``` because Jupyter automatically prints what the last line evaluates to.\n",
"\n",
"We can use variables to change the *order of operations*. Let's see the average price of two people's meals:"
]
Expand Down Expand Up @@ -464,6 +446,8 @@
"source": [
"which is utterly wrong. Beware of the order of operations... it is a frequent source of bugs in scientific programming.\n",
"\n",
"#### Variable Naming Rules\n",
"\n",
"For the most part, you can pick whatever name makes sense for a variable, but there *are* some rules. When choosing a name:\n",
"1. No keywords (```False``` won't work.)\n",
"1. No spaces (```sample thickness``` is invalid)\n",
Expand All @@ -481,15 +465,33 @@
"id": "58272db5-10c0-4c33-afef-354cd9c83dfb",
"metadata": {},
"source": [
"## Types\n",
"\n",
"We've hinted that variables have a \"type\", and that the type can change if it needs to. The way it works is that variables keep track of what *type* they are (integer, real number, or string) and what their \"value\" is. We can even interrogate a variable as see what type it is:"
]
},
{
"cell_type": "markdown",
"id": "56a0db35-2a45-45ca-9b27-631b08f3f91a",
"cell_type": "code",
"execution_count": 2,
"id": "fc59a93c-4ce8-4f62-a804-34d8ea496926",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"reading:\n",
"<class 'float'>\n",
"<class 'str'>\n"
]
}
],
"source": [
"Lines of code in any language can get very complicated (see \"scientific programming\", above). What happens when you run out of horizontal space and you need to go to the next line? Let's see:"
"reading=7.2\n",
"print(\"reading:\")\n",
"print(type(reading))\n",
"reading=\"rainbow\" # changes type of reading to string\n",
"print(type(reading))"
]
},
{
Expand Down Expand Up @@ -561,7 +563,11 @@
"id": "8fc293e2-f0f6-4b3c-a9ed-f34c07a5f905",
"metadata": {},
"source": [
"Sometimes the expressions we need to evaluate can be very long. It would be nice if we could split up a long expression and spread it out over a few lines. As a small example, we'll take a look at 4+2+3. Manyb programming languages will let us just split an expression anywhere we want, such as:"
"Being able to convert values from one type to another is often called *type coercion*. These conversions are extremely important for situation where you need to get input from a user, even more so if you need to do it repetitively.\n",
"\n",
"## Continuation Character\n",
"\n",
"Sometimes the expressions we need to evaluate can be very long. It would be nice if we could split up a long expression and spread it out over a few lines. As a small example, we'll take a look at 4+2+3. Many programming languages will let us split an expression anywhere we want, such as:"
]
},
{
Expand All @@ -580,7 +586,7 @@
"id": "9552bdf3-e316-4151-859e-2428672a4e9d",
"metadata": {},
"source": [
"...but that result isn't right. The last line, `+3`, was evaluated and printed as the result of running that cell. In Python,it turns out, if we need to continue an expression on the next line we just end the current line with a backslash `\\` and press enter. It has to be a backslash, by the way, and **cannot** be the forward slash like we use for division."
"...but that result isn't right in Python. The last line, `+3`, was evaluated and printed as the result of running that cell. In Python,it turns out, if we need to continue an expression on the next line we must end the current line with a backslash `\\` and press enter. It has to be a backslash, by the way, and **cannot** be the forward slash like we use for division."
]
},
{
Expand Down Expand Up @@ -648,6 +654,8 @@
"id": "3f85735b-86f2-456c-9886-e93f8ee310d0",
"metadata": {},
"source": [
"## The String Type\n",
"\n",
"At the beginning of this notebook, we casually mentioned \"strings\" without saying what they are. They're just \"sequences of characters\". And these can be any kind of characters - the English alphabet, the Hungarian alphabet, hiragana... it doesn't matter."
]
},
Expand Down Expand Up @@ -783,35 +791,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "2652937d-4643-4c76-9f70-a364b9dc7cf2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I know people who say \"The Avengers\" isn’t a good movie, but I don’t agree.\n"
]
}
],
"source": [
"\"\"\"I know people who say \"The Avengers\" isn’t a good movie, but I don’t agree.\"\"\"\n"
"movie_opinion = \"\"\"I know people who say \"The Avengers\" isn’t a good movie, but I don’t agree.\"\"\"\n",
"print(movie_opinion)"
]
},
{
"cell_type": "markdown",
"id": "bc86c6dd-6b54-4803-b91a-11bae88c33b9",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3cb7279-f0ee-4e8b-ad40-0b5190dcdbcd",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a13c02dc-f8aa-4c1c-9948-c55ddcd9016c",
"metadata": {},
"outputs": [],
"source": []
"source": [
"## Coming Up Next\n",
"\n",
"We just looked at enough of Python and Jupyter notebooks to use it as a basic calculator, but so far we can't do any real, general-purpose programming with it. The \"flow of control\" sob far as been a straight line from top to bottom and we can't change what we're doing in response to different inputs. That's about to change. In the next section we'll look at the ```if``` statement and how to use it."
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 0fc30ef

Please sign in to comment.