When you're writing code in any programming language, you need to able to make decision 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 operates that Python offers.

Equality Operator

The most basic comparison operators 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 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 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 ran, 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 but 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!

If it isn't hot, which we defined as greater than 70 degrees, you will wear boots, which you did!

Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.