From f0c1e995403e167deb16f5635bb34eb5d8d5f4fd Mon Sep 17 00:00:00 2001 From: Erik Scott Date: Tue, 4 Jun 2024 08:13:56 -0400 Subject: [PATCH] Added to a couple of the examples, fixed typos, clarified. --- theme1/PE100/PE100-01Introduction.ipynb | 22 +-- .../PE100/PE100-02TypesVarsAndOperators.ipynb | 141 +++++++++--------- theme1/PE100/PE100-03DecisionStructures.ipynb | 72 +++++---- theme1/PE100/PE100-04Repetition.ipynb | 22 +-- 4 files changed, 140 insertions(+), 117 deletions(-) diff --git a/theme1/PE100/PE100-01Introduction.ipynb b/theme1/PE100/PE100-01Introduction.ipynb index 91040d0..1eee1bd 100644 --- a/theme1/PE100/PE100-01Introduction.ipynb +++ b/theme1/PE100/PE100-01Introduction.ipynb @@ -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" diff --git a/theme1/PE100/PE100-02TypesVarsAndOperators.ipynb b/theme1/PE100/PE100-02TypesVarsAndOperators.ipynb index 0e3f634..3f0508a 100644 --- a/theme1/PE100/PE100-02TypesVarsAndOperators.ipynb +++ b/theme1/PE100/PE100-02TypesVarsAndOperators.ipynb @@ -7,33 +7,7 @@ "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" ] }, { @@ -41,11 +15,11 @@ "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\"." ] }, { @@ -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..." ] }, { @@ -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." ] }, @@ -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" ] @@ -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." ] }, @@ -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:" ] @@ -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", @@ -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", + "\n", + "\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))" ] }, { @@ -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:" ] }, { @@ -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." ] }, { @@ -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." ] }, @@ -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": { diff --git a/theme1/PE100/PE100-03DecisionStructures.ipynb b/theme1/PE100/PE100-03DecisionStructures.ipynb index 09a1826..783de2f 100644 --- a/theme1/PE100/PE100-03DecisionStructures.ipynb +++ b/theme1/PE100/PE100-03DecisionStructures.ipynb @@ -9,6 +9,8 @@ "\n", "In the first lesson, everything we did was sequential programming. Statements are executed one after the other in exactly the order they're written in. As long as there aren't any errors, every statement will be executed.\n", "\n", + "## The Simplest \"if\" Statement\n", + "\n", "In almost any real Jupyter notebook or standalone program we write, there will have to be places where different code paths are taken depending on what has happened leading up to there. Suppose we're looking at absorption at one specific wavelength and we know that some of our instruments are a little bit too sensitive to changes in humidity. Maybe the first spectrometer has some insulation that is just a little too porous and reads a bit high, but the second one is even worse. We have calibration constants we can apply, but we have to apply the right constant for each individual instrument." ] }, @@ -61,6 +63,8 @@ "we add 7.7% to the reading and save it in a variable called\n", "\"useful_result\".\n", "\n", + "## Else: the catch-all specialist\n", + "\n", "If that was all an ```if``` statement could do then it would be really\n", "useful. But that's not all it can do. We need to do something\n", "reasonable when we get readings from the second instrument. Such as:" @@ -107,8 +111,8 @@ "set useful_result to whatever is saved in \"reading\" plus 19%.\n", "\n", "So far, so good. But there's more! Suppose we need to handle several\n", - "of these cheap, unreliable spectrometers. How do you suppose we could deal\n", - "with that?" + "of these not-quite-top-quality spectrometers. How do you suppose we could deal\n", + "with that? We could resort to putting if-else statements inside if-else statements in sort of a brute force fashion..." ] }, { @@ -161,13 +165,14 @@ "three. This time it is, so the body of the if statement\n", "is executed. We set useful_reading equal to 92% of reading.\n", "\n", + "## Elif\n", + "\n", "This is fine if we only have three instruments, but what do we\n", - "do if we have 20 of them and all but the first two are perfectly\n", - "fine? We could, in principle, type in 60 lines of code, but that\n", + "do if we have 20 of them? We could, in principle, type in 60 lines of code, but that\n", "would be tedious, error prone, and would take a while to read and find\n", "any mistakes. *Of course* there's a better way.\n", "\n", - "Scientists and Engineers! We present... the \"elif\" statement!\n", + "That better way is the \"elif\" keyword.\n", "\n", "Let's see an example with 5 instruments...\n" ] @@ -205,7 +210,7 @@ " useful_result = reading * 1.26 \n", "else: \n", " useful_result = reading \n", - " print(\"Be careful! I only lnow how to correct the first 5 instruments!\") \n", + " print(\"Be careful!\") \n", " \n", "useful_result" ] @@ -216,14 +221,16 @@ "metadata": {}, "source": [ "The final ```else``` clause is the one that runs **if no other clauses\n", - "ran**. If no clause, whether it's the if clause or any of the elif\n", + "ran**. If no clause's conditional statement is true so no clause runs, whether it's the if clause or any of the elif\n", "clauses, then the else clause runs. It's really easy to spot\n", "```else``` clauses even from across the room - they're the ones that\n", "don't have a conditional test.\n", "\n", "Note that the ```if```, ```elif```, and ```else``` lines **must** end\n", - "with a colon. Don't feel bad if you forget one... I forget them about\n", - "half the time.\n", + "with a colon. True confession time: I forget the colons about\n", + "half the time. Python catches it as an error, I fix it, and life goes on.\n", + "\n", + "## Slightly More Complicated\n", "\n", "You can run more than one line of code in response to the tested\n", "conditions, but they have to be indented the same amount:" @@ -265,26 +272,30 @@ "id": "103aa905-d274-45a4-a00d-011e04815f38", "metadata": {}, "source": [ - "There are three interesting things going on here. First, we've\n", + "There are four interesting things going on here. The first and most important thing to notice is that we've got more than one line of code running in response to an \"if\", \"elif\", or \"else\" clause. A collection of lines that should be run together as a whole is called a *code block*. Unlike many languages that mark the start and end of code blocks with special words or characters, Python just does it by using indentation. Everything that is indented the same amount is considered to be in the same code block. We'll look at this in more detail in a few minutes.\n", + "\n", + "Secondly, we've\n", "added lines to set a variable named \"trustworthy\" to a value depending\n", "on whether we had to adjust the reading. Evidently, if we have to\n", "compensate for old, dry, cracking insulators then we don't really\n", "trust the instrument.\n", "\n", - "The second interesting thing is the values ```True``` and\n", + "The third interesting thing is the values ```True``` and\n", "```False```. These are \"Boolean\" values, and when we put them into the\n", "\"trustworthy\" variable then it takes on the Boolean type. There are\n", "only two values, ```True``` and ```False```. The capitalization is\n", "important.\n", "\n", - "The third thing to notice is that we're sending two values into the\n", + "The fourth thing to notice is that we're sending two values into the\n", "print statement and it's printing both of them. In general, we can\n", "give the print statement any number of *arguments*, separated by\n", "commas, and it will print all of them separated by one space.\n", "\n", + "## Conditional (aka Relational) Operators\n", + "\n", "The conditional test in each part of an ```if``` statement is an\n", "expression that results in a Boolean value. So far, the only\n", - "*relational operator* (or *conditional operator*) we've seen is\n", + "*conditional operator* (or *relational operator*) we've seen is\n", "```==```. There are others, though. For the sake of completeness, I'll\n", "include ```==``` here:\n", "\n", @@ -297,6 +308,10 @@ "|```<``` | less than|\n", "|```<=```| less than or equal|\n", "\n", + "\"Relational\" has at least two meanings in computing. Relational Operators have *nothing* to do with Releational Databases.\n", + "\n", + "#### Try This\n", + "\n", "For each of the following code cells, decide what the result is, run\n", "the cell, and see how you did:\n" ] @@ -467,6 +482,8 @@ "all. Punctuation is sprinkled around and the only way to know for sure\n", "is to look up \"ASCII Chart\".\n", "\n", + "## Code Blocks\n", + "\n", "Let's go back to that part about running several lines of code but\n", "they have to be indented the same amount. Python always runs \"blocks\"\n", "of code. That block might be as short as one line:\n" @@ -549,7 +566,7 @@ "single line of code running in a decision structure we can have as\n", "many lines as we want.\n", "\n", - "Take a look at the following code block. For the four possible\n", + "Take a look at the following example. For the four possible\n", "combinations of ```potentially_hazardous``` and ```explody```, decide\n", "what would be printed out. Then try out the combinations and make sure\n", "you know why each combination was handled the way it was." @@ -557,7 +574,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 2, "id": "dafda872-a797-43b1-a75b-9288d6824832", "metadata": {}, "outputs": [ @@ -565,7 +582,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Available oomph to ruin your day is 1511396.1762899999\n" + "Total Available Kaboom (TAK) to ruin your day is 1511396.1762899999\n" ] } ], @@ -592,8 +609,7 @@ " print(\"I'm guessing it's a vinegar-and-baking-soda volcano.\")\n", "else:\n", " print(\"As far as we know, the material in quesion is no more\")\n", - " print(\"dangerous than takeout pizza. How you want to handle it\")\n", - " print(\"is up to you. Choose wisely.\")\n" + " print(\"dangerous than takeout pizza.\")\n" ] }, { @@ -644,21 +660,23 @@ " print(\"No license at all. Run. Quickly.\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "cbff1e92-c650-48cd-9366-396d25254196", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "6a0b39c4-2ad1-4d22-b5ca-7d5e66466bad", "metadata": {}, "source": [ - "At this point, we've seen the most basic way to alter the *flow of control* in Python: the `if` statement. In the next notebook we're going to see how to expand on that idea and make our code do something over and over." + "## Coming Up Next: Loops\n", + "\n", + "At this point, we've seen the most basic way to alter the *flow of control* in Python: the `if` statement. We can write Python code to solve non-trivial problems now, but there are still some things we need in order to use Python as a truly general-purpose language. In the next notebook we're going to make our code do something over and over." ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4772f171-c5de-4c62-b59d-92572bb88232", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/theme1/PE100/PE100-04Repetition.ipynb b/theme1/PE100/PE100-04Repetition.ipynb index 6fe1dc1..bcd6145 100644 --- a/theme1/PE100/PE100-04Repetition.ipynb +++ b/theme1/PE100/PE100-04Repetition.ipynb @@ -7,12 +7,12 @@ "source": [ "# PE100-04: Repetition\n", "\n", - "We started off learning Python with a simple list of statements..." + "We started off learning Python with just simple lists of statements..." ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "id": "323c8155-7d1d-469d-939a-d41d0f3a33d1", "metadata": {}, "outputs": [ @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "61b3fe3b-b083-4ed2-9b05-5a6b6690eb28", "metadata": {}, "outputs": [ @@ -58,7 +58,7 @@ "source": [ "if temperature >= 100:\n", " print(\"Good chance it's boiling.\")\n", - "elif temperature > 2000:\n", + "elif temperature > 3000:\n", " print(\"Odds are it's plasma by now.\")\n", "else:\n", " print(\"You could probably run the experiment and it might even work.\")" @@ -79,9 +79,9 @@ "Python gives us two different ways to make our programs repeat things\n", "in a *loop*.\n", "\n", - "1. ```while``` loops run a block of code, repeatedly, until some\n", - "condition is ```False```.\n", - "1. ```for``` loops run a code block a specific number of times.\n", + "1. ```while``` loops run a block of code, repeatedly, until some conditional statement evaluates to false.\n", + "\n", + "2. ```for``` loops run a code block a specific number of times.\n", "\n", "Let's start with the ```while``` loop.\n", "\n", @@ -134,7 +134,7 @@ "Here's what the above code does. First, it creates a variable named\n", "\"instrument\" and sets it to 1. Then it goes into the while loop. The\n", "first time through, it checks to see if instrument is less than or\n", - "equal to 2. It is (we set it to 1 just a moment ago) so the while\n", + "equal to 2. It is (because we set it to 1 just a moment ago) so the while\n", "loop will execute the code block. This block prints out two lines and\n", "then it adds 1 to instrument. That means instrument now equals 2.\n", "\n", @@ -157,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "id": "b3084a25-da3f-4eed-a59d-19d6bfef25c2", "metadata": {}, "outputs": [ @@ -183,7 +183,7 @@ "power=0\n", "two_to_the_power = 2**power\n", "\n", - "while two_to_the_power < 928: # all Porsches aspire to this, btw.\n", + "while two_to_the_power < 928: # Totally not Porsche related.\n", " print(\"2 to the\", power,\"equals\",two_to_the_power)\n", " power = power+1\n", " two_to_the_power = 2**power\n", @@ -195,7 +195,7 @@ "id": "a5914a7c-74de-4a7b-b2ea-0a5baa2dfc25", "metadata": {}, "source": [ - "Did you notice I snuck something in there we haven't talked about yet? See the \"#\" character on the line with the `while` statement? That indicates the rest of the line is a comment. Python will totally ignore it. It's handy for leaving little notes to yourself, like \"why did I choose 928 there when I could have put 944?\" This is very, very important when writing full-fledged, standalone programs. If you don't leave some notes for yourself, you'll never remember what you were thinking when you go back to that code six months from now. Also, the next person who comes along and has to change something in your code will greatly appreciate the hints.\n", + "Did you notice I sneaked something in there we haven't talked about yet? See the \"#\" character on the line with the `while` statement? That indicates the rest of the line is a comment. Python will totally ignore it. It's handy for leaving little notes to yourself, like \"why did I choose 928 there when I could have put 944?\" This is very, very important when writing full-fledged, standalone programs. If you don't leave some notes for yourself, you'll never remember what you were thinking when you go back to that code six months from now. Also, the next person who comes along and has to change something in your code will greatly appreciate the hints.\n", "\n", "Leaving comments in the code isn't as big a deal in Jupyter notebooks... you can write rather substantial notes in a Markdown cell complete with **boldface**, *italics*, and whatever other fanciness you desire. On the other hand, it's also nice to be able to leave your comments in the just the right place in the code so it flows effortlessly through your comprehension as you read it. Let experience and personal opinion be your guide here.\n", "\n",