print (403.616)
403.616
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. Data Structures are how information is stored in a computer, and algorithms are the instructions the computer applies to transform that data.
To run the code in a cell, first click in the cell to select it. Then you can either: 1. Go to the “Run” menu and choose “Run Selected Cells”, or 1. Just press Shift + Enter.
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”.
When it ran, it printed “403.616” on a line by itself. That was the output from the print.
Click in the next cell (where it says “103.5”) to select that cell. Then hold down the Shift key while you press Enter.
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.
Take a look at a string literal. Run each of the next two cells…
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.
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.
Besides the “classic” operators, there are some handy extras:
What happened there? The //
operator does integer division - it returns the whole number part of the answer, just like when we learned division in elementary school.
The %
operator returns the remainder. This is also called “modulo”, and the above would be pronounced “sixteen mod 3”.
The **
operator does exponentiation. The arguments can be integers or they can be real numbers. Naturally, operators can be combined into arbitrarily long expressions.
Notice what happens when we use different operators. They are applied in the “My Dear Aunt Sally” order of precendence (multiplication, division, addition, subtraction).
Order of operations: * Exponentiation: **
* Multiplication, Division, Remainder: * / // %
* Addition and Subtraction: + -
Within the same level, operators are applied left-to-right. 8-5+2 is evaluated as 3+2 and yields 5. The exception is exponentiation: 2 ** 3 ** 4 is treated as 2 ** 81 and yeilds an annoyingly large number
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.
We just created a variable named answer
and gave it the value 42. Variables are long-lived - later we’ll talk about just how long when we start writing our own functions, but until then our variables last as long as Python (or in our case, Jupyter) is running. Take a look - answer is still there.
The value stored in a variable can change. It can even change type:
We can declare many variables, and we can “do things” with them just like we can when we type in numbers or strings.
In the last line, we just put watts
because Jupyter automatically prints what the last line evaluates to.
We can use variables to change the order of operations. Let’s see the average price of two people’s meals:
That’s the right answer. If we hadn’t done that, we would have gotten
which is utterly wrong. Beware of the order of operations… it is a frequent source of bugs in scientific programming.
For the most part, you can pick whatever name makes sense for a variable, but there are some rules. When choosing a name: 1. No keywords (False
won’t work.) 1. No spaces (sample thickness
is invalid) 1. The first character must be one of * a-z, or A-Z, or _. (the underscore character) * As a result, no numbers (3rd_sample_holder is invalid) 1. After the first character, you can then have numbers (sample_holder_3 is perfectly valid) 1. No other symbols are allowed (exploded&destroyed_spectrometers is invalid, and probably suggests it’s time to review lab safety procedures).
Note: Uppercase vs. Lowercase matters! Bevatron
is not the same variable as bevatron
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:
In [2]:
In [39]:
The type of a variable matters. Let’s create a variable with an integer in it and another with a string. Then let’s do some math:
How do we handle situations like that, where second_thing
held a string representing a seven, but because it was a string variable it couldn’t be used as an integer? Python provides a few functions to convert values from one type to another. The str()
function takes a variable and converts it to a string. The float()
and int()
functions convert their arguments to floating-point and to integer numbers, respectively.
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.
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:
…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.
Time for an exercise! Try to predict what will be printed when you run the next cell. Then, run the next cell and see how you did. If you miss one, make sure you figure out what happened before you go. I know, we’re professionals, I shouldn’t have to say that…
In [47]:
Now write an expression to average three numbers (12, 14, and 66), divide the result by three, and square it. You can use the code cell right below here:
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.
Some, probably most, languages contain strings inside “double quotes”, "
, which is shift+apostrophe on US English keyboards. Other languages (SQL and Pascal are the only two I can think of) use single quotes: '
. Python lets you use either one. You do have to be consistent in each string, but it can vary from one string to the next:
In [52]:
Because we can use either type of quotation mark, we can exploit that to let us put quotation marks into strings:
In [3]:
print("Don't put explosive mixtures in the spectrometer, please.")
print('Of course he was warned... "Do not turn the spectrometer into a bomb, please" but I am sure he ignored that.')
Don't put explosive mixtures in the spectrometer, please.
Of course he was warned... "Do not turn the spectrometer into a bomb, please" but I am sure he ignored that.
That lets us embed whichever kind of quotation mark we need into a string.
But what if we need to embed both kinds of quotes into one string? We’re in luck: we can use the backslash character again to “quote” our quotation mark. In fact, we can quote any character with it if we need to.
In [2]:
That sentence contains three things, inside the string itself: 1. Double Quotes to surround a direct quotation 2. A single quote, also called an apostrophe depending on how it’s used, to make a contraction, and 3. A totally awesome/terrifying molecule you have to google to believe.
OK, I’ll save you the trouble. Prepare to lose most of a day’s productivity. You’re welcome.
(Derek has written gobs of articles on fun substances. Here are some more. )
There is one last kind of string literal. Sometimes you need a string that is several lines long. The “triple quote” is a way to do it. You have to use three double-quotes in a row:
In [57]:
Triple quotes are also an easier way to embed mixed kinds of quotation marks into strings:
In [5]:
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.