3.7 KiB
4 - If/Else and Logical Operators
What's this if/else nonsense?
Sometimes, our programs need to make decisions, we need to do one thing if some condition is true, and something else if it's not. We use if/else statements to ask questions in our program so we can make decisions about what should happen. The format of an if statement starts off simple.
if (some condition):
(DO STUFF IF CONDITION IS TRUE)
else:
(DO STUFF IF CONDITION IS FALSE)
We can check different things to make different choices, neat. But what do we do if we need to ask a bunch of related questions in our program? That's were elif comes in.
if (some condition): (DO STUFF IF CONDITION IS TRUE) elif (some other condition): (DO STUFF IF SOME OTHER CONDITION IS TRUE) else: (DO STUFF IF ALL CONDITIONS (QUESTIONS) ARE FALSE)
elif allows use to specify another condition and some code to go with it to add another question to ask if the questions that came before it were false. This gives us more options to determine what we're going to do next.
Do I need to use if/elif/else all the time?
The smallest form of if is this
if (some condition): (DO STUFF IF CONDITION IS TRUE)
You don't need else, you don't need elif, unless you want them and it makes sense for what you're trying to do in your program
Does the indentation matter?
YES! Python is ULTRA PICKY about indentation. In general, when you see a colon (:) in Python, that means that some portion of the code underneath it should be indented one tab more than the current level of indentation. Here's a very bland example (ignore the for loop for the moment, we'll talk about that next lesson)
for x in range(10): if x == 5 or x == 7: print("Potato") elif x == 2: print("Grape") else: print("You get nothing")
The identation determines something called scope. Scope is essentially defining blocks of code that "live in the same space". Scope can be a complicated topic, so we're going to gloss over it. W3Schools does a really good job explaining scope relatively quickly. You can even try the code for yourself right on the webpage!
So how do I write a condition?
Depends on what you're trying to do. There's lots of different options, but the most common should be relatively familiar to you
- == - Is something equal to something else
- This is not a typo, it has to be two equals signs to check if two values are the same or Python gets confused
- != - Is something not equal to something else
- < - Is the left value less than the right value
- <= - Is the left value less than or equal to the right value
- > - Is the left value greater than the right value
- >= - Is the left value greater than or equal to the right value
Sometimes conditions are as simple as, is this less than that, or is this equal to that. Sometimes you need to check multiple things! This is where AND and OR come into play. These are logical operators that can "combine" the results of different questions (like is equal to) into a single true false value.
AND will result in a True value only when both the left and the right side of the AND are True.
Example: intValue < 100 and intValue > 50 is only True when intValue is any number between 51 and 99
The Truth Table for AND looks like this
Left Side | Right Side | Result |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |