# Ifs, Elifs, Elses

Updated on Dec 22, 2021 by [Alan Morel](/content/alanmorel/index.html)

## Table of Contents

1. [Comparison Operators](/content/classes/python/logic-conditionals#comparison-operators/index.html)
2. [Conditionals](/content/classes/python/logic-conditionals#conditionals/index.html)
3. [Logical Operators](/content/classes/python/logic-conditionals#logical-operators/index.html)

When you're writing code in any programming language, you need to able to make decisions based on the current state of the program. You'll want to be able to compare values to decide what path you want to take next. This is called **logic**.

## Comparison Operators

To use logic in our program, we'll need to learn about the comparison operators that [Python](/content/classes/python/index.html) offers.

### Equality Operator

The most basic comparison operator we have is the **equality** operator. This simply checks if two values are equivalent or not. If they are the same, the result will be `true`, and if they are not equal, the result will be `false`.

To use the equality operator, simply use **two equal signs**.

```python
number1 = 3
number2 = 3
print(number1 == number2)
```

```bash
True
```

```python
number1 = 3
number2 = 6
print(number1 == number2)
```

```bash
False
```

### Inequality Operator

The **inequality operator** functions exactly like the equality operator we just saw but in reverse. If the two values are the same, it will result in `false`, and if they are different, it will result in `true`.

To use this operator, put an **exclamation point** in front of the equal sign, like so:

```python
number1 = 3
number2 = 3
print(number1 != number2)
```

```bash
False
```

```python
number1 = 3
number2 = 6
print(number1 != number2)
```

```bash
True
```

### Greater Than and Less Than Operators

You can compare the values of two variables using the **greater than** and **less than** operators. Here is how to use them:

```python
number1 = 3
number2 = 6
print(number1 > number2) # greater than
```

```bash
False
```

```python
number1 = 3
number2 = 6
print(number1 < number2) # less than
```

```bash
True
```

### Or Equal To Operators

In the case where you need to check if a value is **either** less than/greater than or equal to another value, Python also has an operator for that. It looks like a combination of the operators we saw before:

```python
print(3 <= 5)
print(3 >= 5)
print(7 <= 8)
print(7 >= 9)
```

```bash
True
False
True
False
```

## Conditionals

Now that we are able to compare two values, we can now do something with that result. Using **conditionals** we can now take different paths of code depending on if the value inside it is `true` or `false`.

### If

Using the **if** keyword is very straightforward. If whatever you are checking is `true`, then whatever is inside the next block of code will run.

Let's see an example of this:

```python
burritos = 6

if (burritos > 5):
    print("You ordered too many burritos.")
```

```bash
You ordered too many burritos.
```

Now let's order a correct number of burritos and see what happens:

```python
burritos = 3

if (burritos > 5):
    print("You ordered too many burritos.")
```

```bash
```

Nothing happened! The code block did not execute because the value of `burrito` was not greater than `5`.

### Else

You're probably thinking, well, I want to output something even if the correct number of burritos were ordered. Well, that's where **else** comes into play.

We can define another block of code to run whenever the `if` conditional was false.

```python
burritos = 3

if (burritos > 5):
    print("You ordered too many burritos.")
else:
    print("You ordered a correct number of burritos!")
```

```bash
You ordered a correct number of burritos!
```

### Else If

That's cool, but what if we wanted more than 2 code paths? That is where **else if** comes into play, or **elif**.

```python
burritos = 2

if (burritos > 5):
    print("You ordered too many burritos.")
elif (burritos == 2):
    print("You ordered the PERFECT number of burritos!")
else:
    print("You ordered a correct number of burritos!")
```

```bash
You ordered the PERFECT number of burritos!
```

Since code executes from top to bottom, it will first do the first conditional. Since the value was not greater than `5`, it moved on to the second conditional. Since the conditional there was `true`, that code block was executed instead of the one below it.

## Logical Operators

So far we've seen how logic works when we only have a single conditional to work with. Eventually, you will need to use multiple conditionals to finally make the decision about what path of code you want to take. When you want to use multiple conditionals at once, you use a **logical operator**.

### And Operator

By using the **and** operator, the block of code will only execute if both sides of the operator are true:

```python
isHungry = True
foodAvailable = True

if (isHungry and foodAvailable):
    print("Since I am hungry and there is food, I shall eat.")
```

```bash
Since I am hungry and there is food, I shall eat.
```

Since in this case both variables were `true`, the resulting code block was run, and the print statement was displayed.

### Or Operator

While using the `and` operator, both conditionals needed to be `true`, but with the `or` operator, only a **single one** needs to be `true`.

Consider this example:

```python
sunny = False
bored = True

if (sunny or bored):
    print("Since it is either sunny outside or I'm bored, I will go play basketball.")
```

```bash
Since it is either sunny outside or I'm bored, I will go play basketball.
```

Even though it is not sunny outside, because you are bored, you decided to play basketball anyway.

### Not Operator

In our final logical operator, you can invert the results of a conditional entirely by using the **not** operator.

Let's look at an example:

```python
temperature = 50
hot = temperature > 70

if not (hot):
    print("Since it isn't hot, I will wear boots today!")
```

```bash
Since it isn't hot, I will wear boots today!
```
