B2. 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.
We will talk more about objects later, but a variable, like everything in Python, is an object. For now, you can think of it in the following 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 will go through some of the properties of variables and things you can do to them.
Determining the type
First, we will use Python’s built-in 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.5e-7
.
[4]:
type(4.5e-7)
[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 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.
[6]:
# 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 really use. 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 1 |
|
|
Input 2 |
|
|
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
This is what we would expect. An import 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.
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.881784197001252e-16
[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)
Cell In[24], line 1
----> 1 2.1 / 0.0
ZeroDivisionError: float division by zero
We cannot do it with int
s, either.
[25]:
2 / 0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[25], line 1
----> 1 2 / 0
ZeroDivisionError: division by zero
Operations on integers and floats
This proceeds as we think it should.
[26]:
2.1 + 3
[26]:
5.1
[27]:
2.1 - 3
[27]:
-0.8999999999999999
[28]:
2.1 * 3
[28]:
6.300000000000001
[29]:
2.1 / 3
[29]:
0.7000000000000001
[30]:
2.1 ** 3
[30]:
9.261000000000001
[31]:
2.1 % 3
[31]:
2.1
[32]:
2.1 ** 3
[32]:
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.
[33]:
'Hello, ' + 'world.'
[33]:
'Hello, world.'
Adding strings together concatenates them! This is also intuitive. How about subtracting strings?
[34]:
'Hello, ' - 'world'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[34], line 1
----> 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 cannot 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?
[35]:
'Hello, world.' * 3
[35]:
'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:
[36]:
'4' + '2'
[36]:
'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. As a watchout, do not use excessive parentheses. So often, we see students not trusting the order of operations and polluting their code with lots of parentheses, making it unreadable. This has been the source of countless bugs we have encountered in student code through the years.
Let’s practice order-of-operations.
[37]:
1 + 4**2
[37]:
17
[38]:
1 + 4/2
[38]:
3.0
[39]:
1**3 + 2**3 + 3**3 + 4**3
[39]:
100
[40]:
(1 + 2 + 3 + 4)**2
[40]:
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.
[41]:
a = 2
b = 3
a + b
[41]:
5
Great! We get what we expect! And we still have a
and b
.
[42]:
a, b
[42]:
(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 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.
[43]:
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.
[44]:
a = 2
a += 4.1
a
[44]:
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 **=
.
[45]:
a = 2
a **= 3
a
[45]:
8
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.
[46]:
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
.
[47]:
str(my_int)
[47]:
'42'
When converting a float
to an int
, the interpreter does not round the result, but gives the floor.
[48]:
int(2.9)
[48]:
2
Also consider our string concatenation warning/example from above:
[49]:
print('4' + '2')
print(int('4') + int('2'))
42
6
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.
[50]:
5 == 5
[50]:
True
[51]:
5 == 4
[51]:
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.
[52]:
type(True)
[52]:
bool
[53]:
type(False)
[53]:
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.
[54]:
a = 4
b = 5
c = 4
a == b
[54]:
False
[55]:
a == c
[55]:
True
Now, let’s try it out with some floats.
[56]:
5.3 == 5.3
[56]:
True
[57]:
2.1 + 3.2 == 5.3
[57]:
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.
[58]:
2.2 + 3.2 == 5.4
[58]:
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!
[59]:
4 < 5
[59]:
True
[60]:
5.7 <= 3
[60]:
False
[61]:
'michael jordan' > 'lebron james'
[61]:
True
Whoa. What happened on that last one? The Python interpreter has weighed in on the debate about the greatest 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.
[62]:
ord('a')
[62]:
97
[63]:
ord('λ')
[63]:
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.
[64]:
'lebron' == 'lebron james'
[64]:
False
[65]:
'lebron' == 'LeBron'
[65]:
False
[66]:
'LeBron James' == 'LeBron James'
[66]:
True
[67]:
'AGTCACAGTA' == 'AGTCACAGCA'
[67]:
False
Chaining relational operators
Python allow chaining of relational operators.
[68]:
4 < 6 < 6.1 < 9.3
[68]:
True
[69]:
4 < 6.1 < 6 < 9.3
[69]:
False
This is convenient do to. However, it is important not to do the following, even though it is legal.
[70]:
4 < 6.1 > 5
[70]:
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
.
[71]:
4 < 6.1 > 3
[71]:
True
So, I issue a warning.
Do not mix the directions of chained relational operators.
Identity operators
Identity operators check to see if two variables occupy the same space in memory; i.e., they are the same object (we’ll learn more about objects as we go along). 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`` |
That’s right. 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 stored variables in memory, starting with float
s.
[72]:
a = 5.6
b = 5.6
a == b, a is b
[72]:
(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.
[73]:
a = 5.6
b = a
a == b, a is b
[73]:
(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.
[74]:
a = 5.6
b = a
a = 6.1
a == b, a is b
[74]:
(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.
[75]:
a = 5
b = 5
a == b, a is b
[75]:
(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.
[76]:
a = 350
b = 350
a is b
[76]:
False
Now, let’s look at strings.
[77]:
a = 'Hello, world.'
b = 'Hello, world.'
a == b, a is b
[77]:
(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.
[78]:
a = 'Hello, world.'
b = a
a == b, a is b
[78]:
(True, True)
Let’s try string assignment again with a different string.
[79]:
a = 'python'
b = 'python'
a == b, a is b
[79]:
(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 very) 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 see encounter mutable data types in future lesson, 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.
[80]:
True and True
[80]:
True
[81]:
True and False
[81]:
False
[82]:
True or False
[82]:
True
[83]:
True or True
[83]:
True
[84]:
not False and True
[84]:
True
[85]:
not(False and True)
[85]:
True
[86]:
not False or True
[86]:
True
[87]:
not (False or True)
[87]:
False
[88]:
7 == 7 or 7.6 == 9.1
[88]:
True
[89]:
7 == 7 and 7.6 == 9.1
[89]:
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 forthcoming sections of this appendix. The others we left out are bitwise operators and operators on sets, which we will not be covering.
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.
[90]:
True == 1
[90]:
True
[91]:
False == 0
[91]:
True
You can do arithmetic on True
and False
, but you will get implicit type conversion.
[92]:
True + False
[92]:
1
[93]:
type(True + False)
[93]:
int
Conditionals
Conditionals are used to tell your computer to do a set of instructions depending on whether or not a Boolean is True
. In other words, we are telling the computer:
if something is true:
do task a
otherwise:
do task b
In fact, the syntax in Python is almost exactly the same. As an example, let’s ask whether or not a codon is the canonical start codon (AUG
).
[94]:
codon = 'AUG'
if codon == 'AUG':
print('This codon is the start codon.')
This codon is the start codon.
The syntax of the if
statement is apparent in the above example. The Boolean expression, codon == 'AUG'
, is called the condition. If it is True
, the indented statement below it is executed. This brings up a very important aspect of Python syntax.
Indentation matters.
Any lines with the same level of indentation will be evaluated together.
[95]:
codon = 'AUG'
if codon == 'AUG':
print('This codon is the start codon.')
print('Same level of intentation, so still printed!')
This codon is the start codon.
Same level of intentation, so still printed!
What happens if our codon is not the start codon?
[96]:
codon = 'AGG'
if codon == 'AUG':
print('This codon is the start codon.')
Nothing is printed. This is because we did not tell Python what to do if the Boolean expression codon == 'AUG'
evaluated False
. We can add that with an else
clause in the conditional.
[97]:
codon = 'AGG'
if codon == 'AUG':
print('This codon is the start codon.')
else:
print('This codon is not the start codon.')
This codon is not the start codon.
Great! Now, we have a construction that can choose which action to take depending on a value. So, if we’re zooming along an RNA sequence, we could pick out the start codon and infer where translation would start. Now, what if we want to know if we hit a canonical stop codon (UAA
, UAG
, or UGA
)? We can nest the conditionals!
[98]:
codon = 'UAG'
if codon == 'AUG':
print('This codon is the start codon.')
else:
if codon == 'UAA' or codon == 'UAG' or codon == 'UGA':
print('This codon is a stop codon.')
else:
print('This codon is neither a start nor stop codon.')
This codon is a stop codon.
Notice that the indentation defines which clause the statement belongs to. E.g., the second if
statement is executed as part of the first else
clause.
While this nesting is very nice, we can be more concise by using an elif
clause.
[99]:
codon = 'UGG'
if codon == 'AUG':
print('This codon is the start codon.')
elif codon == 'UAA' or codon == 'UAG' or codon == 'UGA':
print('This codon is a stop codon.')
else:
print('This codon is neither a start nor stop codon.')
This codon is neither a start nor stop codon.
Computing environment
[100]:
%load_ext watermark
%watermark -v -p jupyterlab
Python implementation: CPython
Python version : 3.10.9
IPython version : 8.10.0
jupyterlab: 3.5.3