Lesson 1b: Variables, operators & types#


Whether you are programming in Python or pretty much any other language, you will be working with variables. While the precise definition of a variable will vary from language to language, we’ll focus on Python variables here. Like many of the concepts in this class, though, the knowledge you gain about Python variables will translate to other languages.

We will talk more about objects later, but a variable, like everything in Python, is an object. For now, you can think of it this way. The following can be properties of a variable:

  1. The type of variable. E.g., is it an integer, like 2, or a string, like 'Hello, world.'?

  2. The value of the variable.

Depending on the type of the variable, you can do different things to it and other variables of similar type. This, as with most things, is best explored by example. We’ll go through some of the properties of variables and things you can do to them in this tutorial.

We will work through this and most other modules using Jupyter notebooks. Henceforth in the class, we will use Jupyter notebooks except where explicitly noted. So, launch and notebook, and we’ll get rolling!

Learning objectives#

Upon completing this module you will:

  • Understand the primary data types (i.e. integers, floats, strings).

  • Be able to apply different operations (i.e. arithmetic) with these variable types.

  • Know how to assign and evaluate variables.

  • Be able to coherce variables to different types (aka ‘type conversion’)

Determining the type#

Video 🎥:

First, we will use Python’s built-in type() function to determine the type of some variables.

type(2)
int
type(2.3)
float
type('Hello, world.')
str

The type function told us that 2 is an int (short for integer), 2.3 is a float (short for floating point number, basically a real number that is not an integer), and 'Hello, world.' is a str (short for string). Note that the single quotes around the characters indicate that it is a string. So, '1' is a string, but 1 is an integer.

Note

We can also express floats using scientific notation; \(4.5\times 10^{-7}\) is expressed as 4.5e-7.

type(4.5e-7)
float

A note on strings#

We just saw that strings can be enclosed in single quotes. In Python, we can equivalently enclose them in double quotes. E.g.,

'my string'

and

"my string"

are the same thing. We can also denote a string with triple quotes. So,

"""my string"""
'''my string'''
"my string"
'my string'

are all the same thing.

Tip

The difference with triple quotes is that it allows a string to extend over multiple lines.

# A multi-line string
my_str = """It was the best of times,
it was the worst of times..."""

print(my_str)
It was the best of times,
it was the worst of times...

Note, though, we cannot do this with single quotes.

# This is a SyntaxError
my_str = 'It was the best of times,
it was the worst of times...'
  Cell In[6], line 2
    my_str = 'It was the best of times,
             ^
SyntaxError: unterminated string literal (detected at line 2)

Operators#

Operators allow you to do things with variables, like add them. They are represented by special symbols, like + and *. For now, we will focus on arithmetic operators. Python’s arithmetic operators are

action

operator

addition

+

subtraction

-

multiplication

*

division

/

raise to power

**

modulo

%

floor division

//

Warning

Do not use the ^ operator to raise to a power. That is actually the operator for bitwise XOR, which we will not cover in this class.

Operations on integers#

Let’s see how these operators work on integers.

2 + 3
5
2 - 3
-1
2 * 3
6
2 / 3
0.6666666666666666
2 ** 3
8
2 % 3
2
2 // 3
0

This is what we would expect.

Note

An important note, though. If you are using Python 2, division of integers defaults to floor division. Some legacy code is written in Python 2, though it officially sunset on New Years Day 2020. Fingers crossed you should not be using Python 2 in a future role!

Operations on floats#

Let’s try floats.

2.1 + 3.2
5.300000000000001

Wait a minute! We know 2.1 + 3.2 = 5.3, but Python gives 5.300000000000001. This is due to the fact that floating point numbers are stored with a finite number of binary bits. There will always be some rounding errors. This means that as far as the computer is concerned, it cannot tell you that 2.1 + 3.2 and 5.3 are equal. This is important to remember when dealing with floats, as we will see in the next lesson.

2.1 - 3.2
-1.1
# Very very close to zero because of finite precision
5.3 - (2.1 + 3.2)
-8.881784197001252e-16
2.1 * 3.2
6.720000000000001
2.1 / 3.2
0.65625
2.1 ** 3.2
10.74241047739471
2.1 % 3.2
2.1
2.1 // 3.2
0.0

Aside from the floating point precision issue I already pointed out, everything is like we would expect. Note, though, that we cannot divide by zero.

2.1 / 0.0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/var/folders/8f/c06lv6q17tjbyjv2nkt0_s4s1sh0tg/T/ipykernel_14538/2929663160.py in <module>
----> 1 2.1 / 0.0

ZeroDivisionError: float division by zero

We can’t do it with ints, either.

2 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/var/folders/8f/c06lv6q17tjbyjv2nkt0_s4s1sh0tg/T/ipykernel_14538/2685369145.py in <module>
----> 1 2 / 0

ZeroDivisionError: division by zero

Operations on integers and floats#

This proceeds as we think it should.

2.1 + 3
5.1
2.1 - 3
-0.8999999999999999
2.1 * 3
6.300000000000001
2.1 / 3
0.7000000000000001
2.1 ** 3
9.261000000000001
2.1 % 3
2.1
2.1 ** 3
9.261000000000001

And again we have the rounding errors, but everything is otherwise intuitive.

Operations on strings#

Now let’s try some of these operations on strings. This idea of applying mathematical operations to strings seems strange, but let’s just mess around and see what we get.

'Hello, ' + 'world.'
'Hello, world.'

Ah! Adding strings together concatenates them! This is also intuitive. How about subtracting strings?

'Hello, ' - 'world'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/8f/c06lv6q17tjbyjv2nkt0_s4s1sh0tg/T/ipykernel_14538/3621933347.py in <module>
----> 1 'Hello, ' - 'world'

TypeError: unsupported operand type(s) for -: 'str' and 'str'

That stands to reason. Subtracting strings does not make sense. The Python interpreter was kind enough to give us a nice error message saying that we can’t have a str and a str operand type for the subtraction operation. It also makes sense that we can’t do multiplication, raising of power, etc., with two strings. How about multiplying a string by an integer?

'Hello, world.' * 3
'Hello, world.Hello, world.Hello, world.'

Yes, this makes sense! Multiplication by an integer is the same thing as just adding multiple times, so the Python interpreter concatenates the string several times.

As a final note on operators with strings, watch out for this:

'4' + '2'
'42'

The result is not 6, but it is a string containing the characters '4' and '2'.

Order of operations#

The order of operations is also as we would expect. Exponentiation comes first, followed by multiplication and division, floor division, and modulo. Next comes addition and subtraction. In order of precedence, our arithmetic operator table is

precedence

operators

1

**

2

*, /, //, %

3

+, -

You can also group operations with parentheses. Operations within parentheses is are always evaluated first. Let’s practice.

8 + 9 / 5 ** 2
8.36
8 + 9 / (5 ** 2)
8.36
8 + (9 / 5) ** 2
11.24
(8 + 9) / 5 ** 2
0.68

Knowledge check#

Questions:

  1. Compute one plus 4 squared.

  2. Compute one plus 4 and then square the output.

  3. Compute one cubed plus two cubed plus three cubed plus four cubed.

  4. Compute one plus two plus three plus four and then square the output.

Video 🎥:

Variables and assignment operators#

So far, we have essentially just used Python as an oversized desktop calculator. We would really like to be able to think about our computational problems symbolically. We mentioned variables at the beginning of the lesson, but in practice we were just using numbers and strings directly. We would like to say that a variable, a, represents an integer and another variable b represents another integer. Then, we could do things like add a and b. So, we see immediately that the variables have to have a type associated with them so the Python interpreter knows what to do when we use operators with them. A variable should also have a value associated with it, so the interpreter knows, e.g., what to add.

In order to create, or instantiate, a variable, we can use an assignment operator. This operator is the equals sign. So, let’s make variables a and b and add them.

a = 2
b = 3
a + b
5

Great! We get what we expect! And we still have a and b.

print('a =', a)
a = 2
print('b =', b)
b = 3

Now, we might be tempted to say, “a is two.” No. a is not two. a is a variable that has a value of 2. A variable in Python is not just its value. A variable also carries with it a type. It also has more associated with it under the hood of the interpreter that we will not get into. So, you can think about a variable as a map to an address in RAM (called a pointer in computer-speak) that stores information, including a type and a value.

Assignment/increment operators#

Now, let’s say we wanted to update the value of a by adding 4.1 to it. Python will do some magic for us.

print(type(a), a)

a = a + 4.1

print(type(a), a)
<class 'int'> 2
<class 'float'> 6.1

We see that a was initially an integer with a value of 2. But we added 4.1 to it, so the Python interpreter knew to change its type to a float and update its value.

This operation of updating a value can also be accomplished with an increment operator.

a = 2
a += 4.1
a
6.1

The += operator told the interpreter to take the value of a and add 4.1 to it, changing the type of a in the intuitive way if need be. The other six arithmetic operators have similar constructions, -=, *=, /=, //=, %=, and **=.

a = 2
a **= 3
a
8

Knowledge check#

Questions:

  1. Assign the values 1000, 5, and 0.05 to variables D, K, and h respectively.

  2. Compute \(2 \times D \times K\).

  3. Compute \(\frac{2 \times D \times K}{h}\).

  4. Now put this together to compute the Economic Order Quantity, which is \(\sqrt{\frac{2 \times D \times K}{h}}\). Save the output as Q.

  5. What is the type and value of Q?

  6. Execute del Q. What do you think this does?

Video 🎥:

Type conversion#

Suppose you have a variable of one type, and you want to convert it to another. For example, say you have a string, '42', and you want to convert it to an integer. This would happen if you were reading information from a text file, which by definition is full of strings, and you wanted to convert some string to a number. This is done as follows.

my_str = '42'
my_int = int(my_str)
my_int
42
type(my_int)
int

Conversely, we can convert an int back to a str.

str(my_int)
'42'

Note

Since we did not assign the output of str(my_int) to my_int, we did not actually change the variable my_int to a string. We simply evaluated the coersed ouput.

When converting a float to an int, the interpreter does not round the result, but gives the floor.

int(2.9)
2

Also consider our string concatenation warning/example from above:

print('4' + '2')
print(int('4') + int('2'))
42
6

Exercises#

Questions:

  1. Say you have a 12” pizza. Compute the area of the pizza and assign that value to the variable area. Now say the cost of the pizza was $8. Compute the cost per square inch and assign that value to a variable ppsi.

  2. Convert variable ppsi to an integer. Now what is the value of ppsi.

  3. Convert variable ppsi to a string. Now what is the value of ppsi.

Computing environment#

Hide code cell source
%load_ext watermark
%watermark -v -p jupyterlab
The watermark extension is already loaded. To reload it, use:
  %reload_ext watermark
Python implementation: CPython
Python version       : 3.9.4
IPython version      : 7.26.0

jupyterlab: 3.1.4