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") |

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

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

1 2 |
if (condition): statement |

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

1 |
x = 11 |

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") |

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

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

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

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") |

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") |

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") |

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