---
jupyter:
  jupytext:
    text_representation:
      extension: .Rmd
      format_name: rmarkdown
      format_version: '1.0'
      jupytext_version: 0.8.1
  kernelspec:
    display_name: Python
    language: python
    name: python3
  language_info:
    codemirror_mode:
      name: ipython
      version: 3
    file_extension: .py
    mimetype: text/x-python
    name: python
    nbconvert_exporter: python
    pygments_lexer: ipython3
    version: 3.5.2
---

# Comparisons

A *Boolean* value is a value that can only be True or False.  It is called
"Boolean" after the mathematician and logician [George
Boole](https://en.wikipedia.org/wiki/George_Boole)

Boolean values most often arise from comparison operators. Python includes a
variety of operators that compare values. For example, `3` is larger than `1`:

```{python}
3 > 1
```

`>` is the *greater than* comparison operator.  It takes the value to the left and asks if it is greater than the value to right.   The answer can only be `True` or `False`.

The value `True` indicates that the comparison is valid; Python has confirmed
this simple fact about the relationship between `3` and `1`.

Python uses the special `bool` type for values that can only be `True` or `False`.

```{python}
type(3 > 1)
```

Here we confirm to ourselves that `3` is greater than the result of the expression `1 + 1`:

```{python}
3 > 1 + 1
```

The value `True` indicates that the comparison is valid; Python has confirmed
this simple fact about the relationship between `3` and `1+1`. The full set of
common comparison operators are listed below.

| Comparison         | Operator | True example | False Example |
|--------------------|----------|--------------|---------------|
| Less than          | <        | 2 < 3        | 2 < 2         |
| Greater than       | >        | 3>2          | 3>3           |
| Less than or equal | <=       | 2 <= 2       | 3 <= 2        |
| Greater or equal   | >=       | 3 >= 3       | 2 >= 3        |
| Equal              | ==       | 3 == 3       | 3 == 2        |
| Not equal          | !=       | 3 != 2       | 2 != 2        |

Here are some more examples:

```{python}
4 < 5
```

```{python}
4 < 3
```

```{python}
4 <= 4
```

Notice the `==` in the table above.

The double equals ``==`` is different from the single `=` that we saw before
in assignment.   Here is assignment.

```{python}
a = 4
```

Notice it does not display a value, because it is an assignment, and not an expression.

``==`` is different - it's a comparison operator like `<` or `>`.  It checks
whether two values are equal, and returns True or False:

```{python}
a == 4
```

This is an expression, because `==` is an operator, to say how values should
be combined, like `+` or `*`.

Strings can also be compared, and their order is alphabetical. A shorter
string is less than a longer string that begins with the shorter string.

```{python}
"Dog" > "Catastrophe"
```

```{python}
"Catastrophe" > "Cat"
```

{ucb-page}`Comparison`
