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:
The type of variable. E.g., is it an integer, like
2
, or a string, like'Hello, world.'
?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 built-in type()
function to determine the type of some variables.
[2]:
type(2)
[2]:
int
[3]:
type(2.3)
[3]:
float
[4]:
type("Hello, world.")
[4]:
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.5e-7
.
[5]:
type(4.5e-7)
[5]:
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.
[6]:
# 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.
[7]:
# This is a SyntaxError
my_str = 'It was the best of times,
it was the worst of times...'
File "<ipython-input-7-2cc9f4015bb6>", 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:
[8]:
10^200
[8]:
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:
[9]:
"{0:b}".format(194)
[9]:
'11000010'
Operations on integers
Let’s see how these operators work on integers.
[10]:
2 + 3
[10]:
5
[11]:
2 - 3
[11]:
-1
[12]:
2 * 3
[12]:
6
[13]:
2 / 3
[13]:
0.6666666666666666
[14]:
2**3
[14]:
8
[15]:
2 % 3
[15]:
2
[16]:
2 // 3
[16]:
0
Operations on floats
Let’s try floats.
[17]:
2.1 + 3.2
[17]:
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.
[18]:
2.1 - 3.2
[18]:
-1.1
[19]:
# Very very close to zero because of finite precision
5.3 - (2.1 + 3.2)
[19]:
-8.881784197001252e-16
[20]:
2.1 * 3.2
[20]:
6.720000000000001
[21]:
2.1 / 3.2
[21]:
0.65625
[22]:
2.1**3.2
[22]:
10.74241047739471
[23]:
2.1 % 3.2
[23]:
2.1
[24]:
2.1 // 3.2
[24]:
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.
[25]:
2.1 / 0.0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-25-2f8acfa4b142> in <module>
----> 1 2.1 / 0.0
ZeroDivisionError: float division by zero
We can’t do it with int
s, either.
[26]:
2 / 0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-26-8b4ac6d3a3e1> in <module>
----> 1 2 / 0
ZeroDivisionError: division by zero
Operations on integers and floats
This proceeds as we think it should.
[27]:
2.1 + 3
[27]:
5.1
[28]:
2.1 - 3
[28]:
-0.8999999999999999
[29]:
2.1 * 3
[29]:
6.300000000000001
[30]:
2.1 / 3
[30]:
0.7000000000000001
[31]:
2.1**3
[31]:
9.261000000000001
[32]:
2.1 % 3
[32]:
2.1
[33]:
2.1**3
[33]:
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.
[34]:
'Hello, ' + 'world.'
[34]:
'Hello, world.'
Ah! Adding strings together concatenates them! This is also intuitive. How about subtracting strings?
[35]:
'Hello, ' - 'world'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-75667588e15f> 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?
[36]:
'Hello, world.' * 3
[36]:
'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:
[37]:
'4' + '2'
[37]:
'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.
[38]:
1 + 4**2
[38]:
17
[39]:
1 + 4/2
[39]:
3.0
[40]:
1**3 + 2**3 + 3**3 + 4**3
[40]:
100
[41]:
(1 + 2 + 3 + 4)**2
[41]:
100
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.
[42]:
a = 2
b = 3
a + b
[42]:
5
Great! We get what we expect! And we still have a
and b
.
[43]:
a, b
[43]:
(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 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.
[44]:
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.
[45]:
a = 2
a += 4.1
a
[45]:
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 **=
.
[46]:
a = 2
a **= 3
a
[46]:
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.
[47]:
5 == 5
[47]:
True
[48]:
5 == 4
[48]:
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.
[49]:
type(True)
[49]:
bool
[50]:
type(False)
[50]:
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.
[51]:
a = 4
b = 5
c = 4
a == b
[51]:
False
[52]:
a==c
[52]:
True
Now, let’s try it out with some floats.
[53]:
5.3 == 5.3
[53]:
True
[54]:
2.1 + 3.2 == 5.3
[54]:
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.
[55]:
2.2 + 3.2 == 5.4
[55]:
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!
[56]:
4 < 5
[56]:
True
[57]:
5.7 <= 3
[57]:
False
[58]:
'michael jordan' > 'lebron james'
[58]:
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 built-in ord()
function.
[59]:
ord('a')
[59]:
97
[60]:
ord('λ')
[60]:
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.
[61]:
'lebron' == 'lebron james'
[61]:
False
[62]:
'lebron' == 'LeBron'
[62]:
False
[63]:
'LeBron James' == 'LeBron James'
[63]:
True
[64]:
'AGTCACAGTA' == 'AGTCACAGCA'
[64]:
False
Chaining relational operators
Python allow chaining of relational operators.
[65]:
4 < 6 < 6.1 < 9.3
[65]:
True
[66]:
4 < 6.1 < 6 < 9.3
[66]:
False
This is convenient do to. However, it is important not to do the following, even though it is legal.
[67]:
4 < 6.1 > 5
[67]:
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
.
[68]:
4 < 6.1 > 3
[68]:
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.
[69]:
True == 1
[69]:
True
[70]:
False == 0
[70]:
True
You can do arithmetic on True
and False
, but you will get implicit type conversion.
[71]:
True + False
[71]:
1
[72]:
type(True + False)
[72]:
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 not the same object |
|
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.
[73]:
a = 5.6
b = 5.6
a == b, a is b
[73]:
(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.
[74]:
a = 5.6
b = a
a == b, a is b
[74]:
(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.
[75]:
a = 5.6
b = a
a = 6.1
a == b, a is b
[75]:
(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.
[76]:
a = 5
b = 5
a == b, a is b
[76]:
(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.
[77]:
a = 350
b = 350
a is b
[77]:
False
Now, let’s look at strings.
[78]:
a = 'Hello, world.'
b = 'Hello, world.'
a == b, a is b
[78]:
(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.
[79]:
a = 'Hello, world.'
b = a
a == b, a is b
[79]:
(True, True)
Let’s try string assignment again with a different string.
[80]:
a = 'python'
b = 'python'
a == b, a is b
[80]:
(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.
[81]:
True and True
[81]:
True
[82]:
True and False
[82]:
False
[83]:
True or False
[83]:
True
[84]:
True or True
[84]:
True
[85]:
not False and True
[85]:
True
[86]:
not(False and True)
[86]:
True
[87]:
not False or True
[87]:
True
[88]:
not (False or True)
[88]:
False
[89]:
7 == 7 or 7.6 == 9.1
[89]:
True
[90]:
7 == 7 and 7.6 == 9.1
[90]:
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.
[91]:
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
.
[92]:
str(my_int)
[92]:
'42'
When converting a float
to an int
, the interpreter does not round the result, but gives the floor.
[93]:
int(2.9)
[93]:
2
Also consider our string concatenation warning/example from above:
[94]:
print('4' + '2')
print(int('4') + int('2'))
42
6
Computing environment
[95]:
%load_ext watermark
%watermark -v -p jupyterlab
Python implementation: CPython
Python version : 3.8.11
IPython version : 7.26.0
jupyterlab: 3.1.7