In this video, we’ll talk about more operators you can use, conditionals, and recursion.

We’ve talked about the basic operators in the previous videos.

Operators like plus, minus, multiply, and division. A very commonly used operator is modulus or also called modulo.

What’s modulus?

Modulus works on integers and finds the remainder when dividing two numbers.

The modulus operator is the percent sign (%).

For example,

1 2 |
remainder = 7 % 3 print(remainder) |

Think of dividing 7 by 3, but what gets returned is the remainder, which is 1.

Why would you need to know about the modulus operator?

The modulus operator is great for checking divisibility!

We can check if a number is an even number by using modulus.

Like:

1 2 |
remainder = 6 % 2 print(remainder) |

Whenever using modulus against 2, if the remainder is 0, we know that the number is even since modulus checks for divisibility!

Any number divisible by 2 is an even number.

Let’s talk about booleans. What are boolean expressions?

booleans are the term used for True or False values.

It’s our way of telling whether a statement is True or False.

For example, we can compare two values.

5 == 5 is True

Notice that we used two equal symbols.

Two equal operators means that we want to see if the statement is True or False.

5 == 5 returns True.

In the previous video, we used one equal, but that was for assignment.

We call these types of operators, comparison operators, because we’re comparing two values.

Putting an exclamation point before the equal sign means not equal like:

5 != 5 would be False

There is support for greater than, less than, greater than or equal, and less than or equal.

6 > 5 would be True

6 < 5 would be False

6 >= 6 would be True

4 <= 4 would be True

For greater than or equal or less than or equal, the equal symbol goes after the greater than or less than sign.

Let’s talk about conditionals. What are conditionals?

Conditionals derive from the word, condition. Only do something based on a requirement.

Conditionals trigger a certain behavior based on the requirement of the boolean values, True or False.

If statements are the most used conditional. They look like English.

1 2 |
If (True): print("Do something") |

Which can be translated to:

1 2 3 4 5 6 7 |
x = 5 if (x == 5): print("Do something") x = x + 1 print(x) |

x should be 6 since the if statement is True.

x is equal to 5, so we do the something, which is adding 1 to x, which makes x to 6.

1 2 3 4 5 |
if (x <; 7): print("Do something") x = x + 1 print(x) |

x should now equal to 7. Since x was 6, it is True that x is less than 7, so we do the thing, which was add 1 to x.

You can notice that the format of the if statements always follows:

1 2 |
if (condition): statement |

Like making a function definition, you start with the keyword. In this case, if.

Then, you space, put in the parentheses, the condition that compares values, then there’s a colon just like how defining functions works.

Most importantly, on the new lines, everything that you want the if statement to do, is tabbed once to the right, so that Python knows oh the conditional is true, do everything below the conditional that is tabbed once to the right.

The next thing that we’ll talk about are logical operators.

What are logical operators?

Logical operators is the fancy term for operators like and, or, and not for using multiple conditions or changing the condition’s meaning.

You can put and, or, and not in Python, and they have the same meaning as they do in English.

For example, think of a range of numbers. How do I check if a number is greater than 0 but less than 10? I can use the and operator! Because I need two conditions!

1 2 3 4 |
x = 5 if (x > 0 and x < 10): print("Do something") print("x is in between 0 and 10") |

If I use and I can check for multiple conditions simultaneously. The conditional will be true if and only if both conditions are true.

If x was = to 11, then the condition is False since and requires both conditions to be True.

1 |
x = 11 |

Nothing is printed out.

Let’s say that instead of wanting x to be between 0 and 10.

Let’s say that we wanted x to be outside of 0 and 10.

We can do this two ways.

First way, we can use the not operator:

1 2 3 4 5 |
x = -5 if (not (x > 0 and x < 10)): print("Do something") print("x is not in between 0 and 10") |

By adding a simple not, we can read the condition as it would read in English.

if (x is not greater than 0 and x not less than 10)

Well, -5 is not in between the range of 0 and 10, so the conditional is true.

The other way we could trigger the conditional when x is outside of the range of 0 to 10 is with the or operator:

1 2 3 4 |
x = -5 if (x < 0 or x > 10): print("Do something") print("x is not in between 0 and 10") |

We read the conditional like English. If x is less than 0 or x is greater than 10, then do the statements below the conditional.

With the or operator, if either of the conditions is true, then, the statements are triggered because like how or means in English or means the same thing in Python.

Whenever you want to combine conditions in a conditional, think of the logical operators, and, or, and not, and what they can change your conditional meaning if it were English.

We’ve talked about the if statement.

When the conditional is True do something, but when False, do nothing.

What if I want to do something else when the conditional is False?

There’s something called the else statement!

Trigger different behavior when the if statement is False.

The else statement depends on an if statement. The format is as follows:

1 2 3 4 |
if (True): print("Do something") else: print("Do something else") |

If the condition is true, do something. If the condition is not true, do something else.

Let’s say that we wanted to do something when x is an even number. And do something else when x is an odd number.

In the beginning of the video, we talked about the modulus operator that could check the remainder of a number divided by another number. Divisibility.

We can use the modulus operator with the comparison operator at the same time.

1 2 3 4 5 |
x = 1 if (x % 2 == 0): print(str(x) + " is even") else: print(str(x) + " is odd") |

We know that if there is no remainder when x cleanly divides by 2, then x has to be even because even numbers are always divisible by 2.

But if x does not cleanly divide by 2, then we know that it has to be an odd number.

Since x is equal to 1, that means that the conditional is False, so the else statement is triggered.

When the if statement is False, do the else statement, so

1 is odd is printed out.

Let’s move on to chained conditionals, which is a fancy term for putting multiple conditionals in between the if and else statements.

Earlier in the video, we talked about the and operator for multiple conditions in the same if statement. Sometimes, you want multiple conditions for completely separate actions.

Think of a multiple choice exam. Who want’s to be a millionaire?

You have choices, A, B, C, and D.

An and operator in an if statement won’t be enough.

You need multiple if statements.

Luckily, there’s the elif statement, which is short for else if. elif allows you to use multiple if statement statements.

For example,

1 2 3 4 5 6 7 8 9 10 |
choice = "C" if (choice == "A"): print("Choice A") elif (choice == "B"): print("Choice B") elif (Choice == "C"): print("Choice C") else: print("Choice D") |

You can put any number of elif statements.

If two of these if statements are true, the first one will run instead.

For example,

1 2 3 4 5 6 |
x = 5 if (x > 0): print("x is greater than 0") elif (x > 3): print("x is greater than 3") |

Both if statements are true, but only the first if statement gets executed, so we see “x is greater than 0”

You can put if statements underneath if statements.

We call putting conditionals like if statements underneath other if statements nested conditionals.

Nested conditionals is a fancy term for putting conditionals underneath other conditionals.

For example,

1 2 3 4 5 6 7 8 |
x = 10 if (x > 0): print("x is positive") if (x % 2 == 0): print("x is even") else: print("x is odd") |

x is positive because it’s greater than 0. Then, we encounter the if else statement underneath.

10 is cleanly divisible by 2 because it’s an even number, so x is even.

Notice how the we also tab once to the right from the relative positive underneath the if statement (x % 2 == 0)

Underneath (x % 2 == 0), we put one tab to the right from the relative position of the if statement.

Just remember that you have to put a tab underneath conditionals relative to where the conditional started.

Nested conditionals don’t always look the best way. Sometimes, instead of nested conditionals, you can use the logical operators like: and.

Our nested conditional example checks if x is greater than 0 and is even. We can put these two conditions in one line.

1 2 |
if (x > 0 and x % 2 == 0): print("x is positive and even") |

The main lesson that I want you to get out of conditionals is that they allow you control exactly what your program will do based on your set of requirements.

You need your program to do something based on requirements. Use conditionals.