Comparisons and Logic

Every day, we make thousands of tiny judgements that lead to bigger actions. Based on the information available, we will make what we judge to be the right decision. The programs that you will write will be exactly the same – we will feed it information, it will evaluate it and proceed accordingly.

On the pitch, do I take a shot or lay it off for a better chance elsewhere? In the manager’s office, does he sign a new goalkeeper, or show faith in the academy? Decisions are made on the information available – let’s take a look at how this is done in Python.

Comparison Operators

You may be familiar with comparison operators from other programming languages or even in Excel. These are used to ask the relationship between two values. Is one bigger than another? Are they the same?

They will return with either ‘True’ or ‘False’. Eventually, we will write programs that make decisions based on these evaluations.

Let’s use some simple examples with numbers:

In [1]:
```# Is four greater than three?
4 > 3
```
Out[1]:
`True`
In [2]:
```# Is four less than three?
4 < 3
```
Out[2]:
`False`
In [3]:
```# Is 20 less than or equal to 21?
20 <= 21
```
Out[3]:
`True`
In [4]:
```# Does 44 equal 44? We use 2 equals signs here!
44 == 44
```
Out[4]:
`True`

Remember our variables section? Let’s assign a skill ranking to some players and evaluate them.

In [5]:
```# Our skill ranking system assigns two players a ranking, Who is better?

Garrerd = 99
Landard = 98

Garrerd > Landard
```
Out[5]:
`True`

Logic Operators

Logic operators allow us to use multiple comparisons together. This is really useful as we can quickly ask more complex questions from our programs.

Let’s take the example above. Why don’t we check if we can afford to purchase the better player, assuming they both cost the same?

In [6]:
```# Garrerd = 99 and Landard = 98 as in the last example
# Check that Garrerd is better AND that can we afford them?

Budget = 5000000
PlayerCost = 4000000

(Garrerd > Landard) and (Budget >= PlayerCost)
```
Out[6]:
`True`

Sign him up!

By using the ‘and’ keyword, we test if both things are true. If just one has to be true, we can use the ‘or’ keyword instead.

In this new example, our star striker gets a £1,000,000 bonus if she scores more than ten goals or makes at least 15 assists. Let’s test to see if she did it.

In [7]:
```HerderGoals = 9
HerderAssists = 16

(HerderGoals > 10) or (HerderAssists >= 15)
```
Out[7]:
`True`

Lucky Herder!

Herder gets her bonus! Her teammate Merta, however, is a bit of a loose cannon. Her bonus is given if she scores more than 5 goals, or makes 5 assists AND receives fewer than 3 red cards. Let’s test this.

In [8]:
```MertaGoals = 5
MertaAssists = 10
MertaRedCards = 3

(MertaGoals > 5) or ((MertaAssists>=5) and (MertaRedCards<3))
```
Out[8]:
`False`

Looks like Merta’s temper has cost her here. While she easily made the number of assists, her red card record let her down.

Pay close attention to how the brackets show Python what the clauses are in our ‘or’ and ‘and’ statements!

Summary

In this piece, we have seen how Python assesses numbers. We can use our operators (<,>, etc.) to ask true or false questions. We can make these even more complex with and/or operators too.

In our basic examples, we used it to quickly check player ratings and affordability. We also used it to check if our players earned their bonuses.

For next steps, we need to tell Python to do something based on these actions. We can do this with if statements in the next article!