Back to Article
PE100-02: Types, Variables, and Operators
Download Notebook

PE100-02: Types, Variables, and Operators

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”.

In [1]:
print (403.616)
403.616

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 [4]:
103.5

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…

In [6]:
print ("the quick brown fox")
In [7]:
print ('jumped over the lazy dogs')

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.

Operators

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.

In [10]:
2+2
In [11]:
2*8
In [4]:
6-4
2
In [12]:
7*6
In [13]:
16/3

Besides the “classic” operators, there are some handy extras:

In [15]:
16//3

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.

In [17]:
16%3

The % operator returns the remainder. This is also called “modulo”, and the above would be pronounced “sixteen mod 3”.

In [3]:
2**8
256
In [19]:
4**2.718281828459045

The ** operator does exponentiation. The arguments can be integers or they can be real numbers. Naturally, operators can be combined into arbitrarily long expressions.

In [21]:
3*4*5
In [22]:
6+4*5

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

In [24]:
print (2**3**4)
In [25]:
print (2**81)

Variables

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.

In [5]:
answer = 42
print(answer)
42

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.

In [28]:
print(answer)

The value stored in a variable can change. It can even change type:

In [30]:
weight = 60
weight = 70
print(weight)
weight = "not very much."
print(weight)

We can declare many variables, and we can “do things” with them just like we can when we type in numbers or strings.

In [32]:
volts = 120
amps = 4
watts = volts * amps
watts

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:

In [34]:
total = 22.41 + 19.45
average = total / 2
average

That’s the right answer. If we hadn’t done that, we would have gotten

In [36]:
22.41 + 19.45 / 2

which is utterly wrong. Beware of the order of operations… it is a frequent source of bugs in scientific programming.

Variable Naming Rules

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

Types

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]:
reading=7.2
print("reading:")
print(type(reading))
reading="rainbow" # changes type of reading to string
print(type(reading))
reading:
<class 'float'>
<class 'str'>
In [39]:
my_number = 42
my_string = "was that really the right answer?"

first_type = type(my_number)
second_type = type(my_string)
print(first_type, second_type)

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:

In [41]:
first_thing = 6
second_thing = "7"

print (first_thing + second_thing)

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.

In [9]:
print(first_thing + int(second_thing))
print(first_thing + float(second_thing))
13
13.0

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.

Continuation Character

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:

In [44]:
4+2
+3

…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.

In [10]:
4+2\
+3
9

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]:
print(1 + 3 + 5 * 4 / 2)
print(7 % 2 * 10)
big_num = 1 + 2 + 3 + 4 \
     + 5 + 6
print(big_num)

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:

The String Type

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.

In [50]:
print("I'll see you mañana, assuming I don't get irradiated to death.")

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]:
print("double quotes work")
print('single quotes also work')
print('but do not try to mix the two in one string!"

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]:
print("We told him \"Hexanitrohexaazaisowurtzitane and spectrometers don't mix, buddy\", but we're pretty sure he ignored us.")
We told him "Hexanitrohexaazaisowurtzitane and spectrometers don't mix, buddy", but we're pretty sure he ignored us.

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]:
gigantic = """This is the first line,
This is the second,
and this is the third and final line of my string."""
print(gigantic)

Triple quotes are also an easier way to embed mixed kinds of quotation marks into strings:

In [5]:
movie_opinion = """I know people who say "The Avengers" isn’t a good movie, but I don’t agree."""
print(movie_opinion)
I know people who say "The Avengers" isn’t a good movie, but I don’t agree.

Coming Up Next

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.