Lesson 1b: Variables, operators, and 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 course, 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.
Determining the type¶
First, we will use Python’s builtin type()
function to determine the type of some variables.
[1]:
type(2)
[1]:
int
[2]:
type(2.3)
[2]:
float
[3]:
type('Hello, world.')
[3]:
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 that we can also express float
s using scientific notation; \(4.5\times 10^{7}\) is expressed as 4.5e7
.
[4]:
type(4.5e7)
[4]:
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. The difference with triple quotes is that it allows a string to extend over multiple lines.
[5]:
# A multiline 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.
[6]:
# This is a SyntaxError
my_str = 'It was the best of times,
it was the worst of times...'
File "<ipythoninput62cc9f4015bb6>", line 2
my_str = 'It was the best of times,
^
SyntaxError: EOL while scanning string literal
Arithmetic 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 the course. Observe firey death if you use these inappropriately:
[7]:
10^200
[7]:
194
Instead of raising 10 to the 200th power, Python performed a bitwise XOR as illustrated below:
a 
Binary 
Decimal 

Input 


Input 


Output 


Note: if you want to see how a decimal number is represented in binary, you can use the following:
[8]:
'{0:b}'.format(194)
[8]:
'11000010'
Operations on integers¶
Let’s see how these operators work on integers.
[9]:
2 + 3
[9]:
5
[10]:
2  3
[10]:
1
[11]:
2 * 3
[11]:
6
[12]:
2 / 3
[12]:
0.6666666666666666
[13]:
2**3
[13]:
8
[14]:
2 % 3
[14]:
2
[15]:
2 // 3
[15]:
0
Operations on floats¶
Let’s try floats.
[16]:
2.1 + 3.2
[16]:
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.
[17]:
2.1  3.2
[17]:
1.1
[18]:
# Very very close to zero because of finite precision
5.3  (2.1 + 3.2)
[18]:
8.881784197001252e16
[19]:
2.1 * 3.2
[19]:
6.720000000000001
[20]:
2.1 / 3.2
[20]:
0.65625
[21]:
2.1**3.2
[21]:
10.74241047739471
[22]:
2.1 % 3.2
[22]:
2.1
[23]:
2.1 // 3.2
[23]:
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.
[24]:
2.1 / 0.0

ZeroDivisionError Traceback (most recent call last)
<ipythoninput242f8acfa4b142> in <module>
> 1 2.1 / 0.0
ZeroDivisionError: float division by zero
We can’t do it with int
s, either.
[25]:
2 / 0

ZeroDivisionError Traceback (most recent call last)
<ipythoninput258b4ac6d3a3e1> in <module>
> 1 2 / 0
ZeroDivisionError: division by zero
Operations on integers and floats¶
This proceeds as we think it should.
[ ]:
2.1 + 3
[ ]:
2.1  3
[26]:
2.1 * 3
[26]:
6.300000000000001
[27]:
2.1 / 3
[27]:
0.7000000000000001
[28]:
2.1**3
[28]:
9.261000000000001
[29]:
2.1 % 3
[29]:
2.1
[30]:
2.1**3
[30]:
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.
[31]:
'Hello, ' + 'world.'
[31]:
'Hello, world.'
Ah! Adding strings together concatenates them! This is also intuitive. How about subtracting strings?
[32]:
'Hello, '  'world'

TypeError Traceback (most recent call last)
<ipythoninput3275667588e15f> in <module>
> 1 'Hello, '  'world'
TypeError: unsupported operand type(s) for : 'str' and 'str'
That stands to reason. Subtracting strings does not make sense. Python 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?
[33]:
'Hello, world.' * 3
[33]:
'Hello, world.Hello, world.Hello, world.'
Yes, this makes sense! Multiplication by an integer is the same thing as just adding multiple times, so Python concatenates the string several times.
As a final note on operators with strings, watch out for this:
[34]:
'4' + '2'
[34]:
'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.
[35]:
1 + 4**2
[35]:
17
[36]:
1 + 4/2
[36]:
3.0
[37]:
1**3 + 2**3 + 3**3 + 4**3
[37]:
100
[38]:
(1 + 2 + 3 + 4)**2
[38]:
100
Interestingly, we also demonstrated that the sum of the first \(n\) cubes is equal to the sum of the first \(n\) integers squared. Fun!
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 tutorial, 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.
[39]:
a = 2
b = 3
a + b
[39]:
5
Great! We get what we expect! And we still have a
and b
.
[40]:
a, b
[40]:
(2, 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 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 computerspeak) 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.
[41]:
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.
[42]:
a = 2
a += 4.1
a
[42]:
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 for the associated increment operators, =
, *=
, /=
, //=
, %=
, and **=
.
[43]:
a = 2
a **= 3
a
[43]:
8
Relational operators¶
Suppose we want to compare the values of two numbers. We may want to know if they are equal for example. The operator used to test for equality is ==
, an example of a relational operator (also called a comparison operator).
The equality relational operator¶
Let’s test out the ==
to see how it works.
[44]:
5 == 5
[44]:
True
[45]:
5 == 4
[45]:
False
Notice that using the operator gives either True
or False
. These are important keywords in Python that indicate truth. True
and False
have a special type, called bool
, short for Boolean.
[46]:
type(True)
[46]:
bool
[47]:
type(False)
[47]:
bool
The equality operator, like all relational operators in Python, also works with variables, testing for equality of their values. Equality of the variables themselves uses identity operators, described below.
[48]:
a = 4
b = 5
c = 4
a == b
[48]:
False
[49]:
a==c
[49]:
True
Now, let’s try it out with some floats.
[50]:
5.3 == 5.3
[50]:
True
[51]:
2.1 + 3.2 == 5.3
[51]:
False
Yikes! Python is telling us that 2.1 + 3.2
is not 5.3
. This is floating point arithmetic haunting us. Note that floating point numbers that can be exactly represented with binary numbers do not have this problem.
[52]:
2.2 + 3.2 == 5.4
[52]:
True
This behavior is unpredictable, so here is a rule.
Never use the ==
operator with floats.
Other relational operators¶
As you might expect, there are other relational operators. The relational operators are
English 
Python 

is equal to 

is not equal to 

is greater than 

is less than 

is greater than or equal to 

is less than or equal to 

We can try some of them out!
[53]:
4 < 5
[53]:
True
[54]:
5.7 <= 3
[54]:
False
[55]:
'michael jordan' > 'lebron james'
[55]:
True
Whoa. What happened on that last one? The Python interpreter has weighed in on the debate about the greater basketball player of all time. It clearly thinks Michael Jordan is better than LeBron James, but that seems kind of subjective. To understand what the interpreter is doing, we need to understand how it compares strings.
A brief aside on Unicode¶
In Python, characters are encoded with Unicode. This is a standardized library of characters from many languages around the world that contains over 100,000 characters. Each character has a unique number associated with it. We can access what number is assigned to a character using Python’s builtin ord()
function.
[56]:
ord('a')
[56]:
97
[57]:
ord('λ')
[57]:
955
The relational operators on characters compare the values that the ord
function returns. So, using a relational operator on 'a'
and 'b'
means you are comparing ord('a')
and ord('b')
. When comparing strings, the interpreter first compares the first character of each string. If they are equal, it compares the second character, and so on. So, the reason that 'michael jordan' > 'lebron james'
gives a value of True
is because ord('m') > ord('l')
.
Note that a result of this scheme is that testing for equality of strings means that all characters must be equal. This is the most common use case for relational operators with strings.
[58]:
'lebron' == 'lebron james'
[58]:
False
[59]:
'lebron' == 'LeBron'
[59]:
False
[60]:
'LeBron James' == 'LeBron James'
[60]:
True
[61]:
'AGTCACAGTA' == 'AGTCACAGCA'
[61]:
False
Chaining relational operators¶
Python allow chaining of relational operators.
[62]:
4 < 6 < 6.1 < 9.3
[62]:
True
[63]:
4 < 6.1 < 6 < 9.3
[63]:
False
This is convenient do to. However, it is important not to do the following, even though it is legal.
[64]:
4 < 6.1 > 5
[64]:
True
In other words, do not mix the direction of the relational operators. You could run into trouble because, in this case, 5
and 4
are never compared. An expression with different relations among all three numbers also returns True
.
[65]:
4 < 6.1 > 3
[65]:
True
So, I issue a warning.
Do not mix the directions of chained relational operators.
The numerical values of True and False¶
As we move to conditionals, it is important to take a moment to evaluate the numerical values of the keywords True
and False
. They have numerical values of 1
and 0
, respectively.
[66]:
True == 1
[66]:
True
[67]:
False == 0
[67]:
True
You can do arithmetic on True
and False
, but you will get implicit type conversion.
[68]:
True + False
[68]:
1
[69]:
type(True + False)
[69]:
int
Identity operators¶
Identity operators check to see if two variables occupy the same space in memory; i.e., they are the same object. This is different that the equality relational operator, ==
, which checks to see if two variables have the same value. The two identity operators are in the table below.
English 
Python 

is the same object 
``is`` 
is not the same object 
``is not`` 
The operators are pretty much the same as English! Let’s see these operators in action and get at the difference between ==
and is
. Let’s use the ``is`` operator to investigate how Python stores variables in memory, starting with float
s.
[70]:
a = 5.6
b = 5.6
a == b, a is b
[70]:
(True, False)
Even though a
and b
have the same value, they are stored in different places in memory. They can occupy the same place in memory if we do a b = a
assignment.
[71]:
a = 5.6
b = a
a == b, a is b
[71]:
(True, True)
Because we assigned b = a
, they necessarily have the same (immutable) value. So, the two variables occupy the same place in memory for efficiency.
[72]:
a = 5.6
b = a
a = 6.1
a == b, a is b
[72]:
(False, False)
In the last two examples, we see that assigning b = a
, where a
is a float
in this case, means that a
and b
occupy the same memory. However, reassigning the value of a
resulted in the interpreter placing a
in a new space in memory. We can double check the values.
Integers sometimes do not behave the same way, however.
[73]:
a = 5
b = 5
a == b, a is b
[73]:
(True, True)
Even though we assigned a
and b
separately, they occupy the same place in memory. This is because Python employs integer caching for all integers between 5
and 256
. This caching does not happen for more negative or larger integers.
[74]:
a = 350
b = 350
a is b
[74]:
False
Now, let’s look at strings.
[75]:
a = 'Hello, world.'
b = 'Hello, world.'
a == b, a is b
[75]:
(True, False)
So, even though a
and b
have the same value, they do not occupy the same place in memory. If we do a b = a
assignment, we get similar results as with float
s.
[76]:
a = 'Hello, world.'
b = a
a == b, a is b
[76]:
(True, True)
Let’s try string assignment again with a different string.
[77]:
a = 'python'
b = 'python'
a == b, a is b
[77]:
(True, True)
Wait a minute! If we choose a string 'python'
, it occupies the same place in memory as another variable with the same value, but that was not the case for 'Hello, world.'
. This is a result of Python also doing string interning which allows for (sometimes much more) efficient string processing. Whether two strings occupy the same place in memory depends on what the strings are.
The caching and interning might be a problem, but you generally do not need to worry about it for immutable variables. Being immutable means that once the variables are created, their values cannot be changed. If we do change the value the variable gets a new place in memory. All variables we’ve encountered so far, int
s, float
s, and str
s, are immutable. We will encounter mutable data types in a moment, in which case it really does matter practically to you as a programmer
whether or not two variables are in the same location in memory.
Logical operators¶
Logical operators can be used to connect relational and identity operators. Python has three logical operators.
Logic 
Python 

AND 

OR 

NOT 

The and
operator means that if both operands are True
, return True
. The or
operator gives True
if either of the operands are True
. Finally, the not
operator negates the logical result.
That might be as clear as mud to you. It is easier to learn this, as usual, by example.
[78]:
True and True
[78]:
True
[79]:
True and False
[79]:
False
[80]:
True or False
[80]:
True
[81]:
True or True
[81]:
True
[82]:
not False and True
[82]:
True
[83]:
not(False and True)
[83]:
True
[84]:
not False or True
[84]:
True
[85]:
not (False or True)
[85]:
False
[86]:
7 == 7 or 7.6 == 9.1
[86]:
True
[87]:
7 == 7 and 7.6 == 9.1
[87]:
False
I think these examples will help you get the hang of it. Note that it is important to specify the ordering of your operations, particularly when using the not
operator.
Note also that
a < b < c
is equivalent to
(a < b) and (b < c)
With these new types of operators in hand, we can construct a more complete table of operator precedence.
precedence 
operators 

1 

2 

3 

4 

5 

6 

7 

8 

Operators we left out¶
We have left out a few operators in Python. Two that we left out are the membership operators, in
and not in
, which we will visit in a forthcoming lesson. The others we left out are bitwise operators and operators on sets, which we will not be directly covering.
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.
[88]:
my_str = '42'
my_int = int(my_str)
print(my_int, type(my_int))
42 <class 'int'>
Conversely, we can convert an int
back to a str
.
[89]:
str(my_int)
[89]:
'42'
When converting a float
to an int
, the interpreter does not round the result, but gives the floor.
[90]:
int(2.9)
[90]:
2
Also consider our string concatenation warning/example from above:
[91]:
print('4' + '2')
print(int('4') + int('2'))
42
6
Computing environment¶
[92]:
%load_ext watermark
%watermark v p jupyterlab
CPython 3.7.4
IPython 7.1.1
jupyterlab 1.1.4