Lesson 4: More operators and conditionals

This tutorial was generated from a Jupyter notebook. You can download the notebook here.

In this tutorial, we will examine more operators beyond the arithmetic and assignment operators we have already encountered. We'll look at relational operators, identity operators, and logical operators. We'll use these operators in conditional statements, which help a program decide what to do in certain situations. We will also start using the Spyder's editor to create .py files that we will then run.

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 a ==, 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.

In [61]:
5 == 5
Out[61]:
True
In [62]:
5 == 4
Out[62]:
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.

In [63]:
type(True)
Out[63]:
bool
In [64]:
type(False)
Out[64]:
bool

Now, let's try it out with some floats.

In [65]:
5.3 == 5.3
Out[65]:
True
In [66]:
2.1 + 3.2 == 5.3
Out[66]:
False

Yikes! Python is telling us that 2.1 + 3.2 is not 5.3. This is floating point arithmetic coming back to haunt us! Note that some floating point numbers that can be exactly represented with binary numbers do not have this problem.

In [67]:
2.2 + 3.2 == 5.4
Out[67]:
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 thanor equal to <=

We can try some of them out!

In [68]:
4 < 5
Out[68]:
True
In [69]:
5.7 <= 3
Out[69]:
False
In [70]:
'steph curry' > 'lebron james'
Out[70]:
True

Whoa. What happened on that last one? The Python interpreter clearly thinks Steph Curry is better than LeBron James, but that seems kind of subjective. To understand what the interpeter 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.

In [71]:
ord('a')
Out[71]:
97
In [72]:
ord('λ')
Out[72]:
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 intepreter first compares the first character of each string. If they are equal, it compares the second character, and so on. So, the reason that 'steph curry' > 'lebron james' gives a value of True is because ord('s') > ord('l').

Note that a result of this sceme 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.

In [4]:
'lebron' == 'lebron james'
Out[4]:
False
In [74]:
'lebron' == 'LeBron'
Out[74]:
False
In [75]:
'LeBron James' == 'LeBron James'
Out[75]:
True
In [76]:
'AGTCACAGTA' == 'AGTCACAGCA'
Out[76]:
False

Chaining relational operators

Python allow chaining of relational operators.

In [77]:
4 < 6 < 6.1 < 9.3
Out[77]:
True
In [78]:
4 < 6.1 < 6 < 9.3
Out[78]:
False

This is convenient do to. However, it is important not to do the following, even though it is legal.

In [79]:
4 < 6.1 > 5
Out[79]:
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.

In [80]:
4 < 6.1 > 3
Out[80]:
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 later). 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.

In [1]:
str_1 = 'Hello, world.'
str_2 = 'Hello, world.'

str_1 == str_2, str_1 is str_2
Out[1]:
(True, False)

So, even though str_1 and str_2 have the same value, they do not occupy the same place in memory. Here are a few more examples.

In [2]:
str_1 = 'Hello, world.'
str_2 = str_1

str_1 == str_2, str_1 is str_2
Out[2]:
(True, True)
In [3]:
a = 5.6
b = 5.6

a == b, a is b
Out[3]:
(True, False)
In [4]:
a = 5.6
b = a

a == b, a is b
Out[4]:
(True, True)
In [5]:
a = 5.6
b = a
a = 6.1

a == b, a is b
Out[5]:
(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.

In [84]:
a, b
Out[84]:
(6.1, 5.6)

This automatic reassigning happens with immutable variables. This means that once the variables are created, their values cannot be changed. If we do change the value, as we did by setting a = 6.1, the variable gets a new place in memory. All variables we've encountered so far, ints, floats, and strs, are immutable. We will see encounter mutable data types in future lessons.

Logical operators

Logical operators can be used to connect relational and identity operators. Python has three logical operators.

Logic Python
AND and
OR or
NOT 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.

In [85]:
True and True
Out[85]:
True
In [86]:
True and False
Out[86]:
False
In [87]:
True or False
Out[87]:
True
In [5]:
True or True
Out[5]:
True
In [88]:
not False and True
Out[88]:
True
In [89]:
not(False and True)
Out[89]:
True
In [90]:
not False or True
Out[90]:
True
In [91]:
not (False or True)
Out[91]:
False
In [92]:
7 == 7 or 7.6 == 9.1
Out[92]:
True
In [93]:
7 == 7 and 7.6 == 9.1
Out[93]:
False

I think these example 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 type of operators in hand, we can construct a more complete table of operator precedence.

precendence operators
1 **
2 *, /, //, %
3 +, -
4 <, >, <=, >=
5 ==, !=
6 =, +=, -=, *=, /=, **=, %=, //=
7 is, is not
8 and, or, not

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 covering in the bootcamp.

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.

In [7]:
True == 1
Out[7]:
True
In [10]:
False == 0
Out[10]:
True

You can do arithmetic on True and False, but you will get implicit type conversion.

In [102]:
True + False
Out[102]:
1
In [103]:
type(True + False)
Out[103]:
int

.py files

We now move out of one-liners that we can just enter at a Python or IPython prompt and evaluate. We will be preparing multi-line sets of instructions and then running them. We store the code in Python source files. These are standard text files with a .py suffix. You can create them using Spyder's editor. To run the contents of the file (after saving it), you can click the green arrow on Spyder's toolbar. If I have a file that I saved as

/Users/Justin/bootcamp.py

clicking the green arrow pastes

runfile('/Users/Justin/Desktop/test.py', wdir='/Users/Justin/Desktop')

into the Spyder Python or IPython console. The runfile() function is a built-in function that execute the contents of a .py file in interactive mode.

Unless you are using Python as a fancy desktop calculator, you will invariably be writing .py files.

For our first .py file, we will write a simple conditional. We will then run our .py file in the Spyder Python or IPython console. We will be learning coding in this way for the rest of the bootcamp, as our expressions become more and more sophisticated.

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

In [11]:
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.

In [12]:
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?

In [15]:
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.

In [14]:
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!

In [16]:
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.

In [17]:
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.