If/Else Statements
Python Basics • Last Updated: 03/12/2025
Python Basics • Last Updated: 03/12/2025
'If' statements in Python is really important because they are the sole reason as to how you can check for logic. As mentioned before, if-statements return booleans and can run certain code based on the required query or a certain group of situations where you'd want certain code to run.
When dealing with if-statements, you need to understand these three key words. if, elif, and else. The way that you should structure the statement is by starting with if, then checking something, then placing a colon (:) immediately after. Python will automatically create an indent for you in the editor, and then you run your code. Here's an example:
In case this isn't obvious, it is REALLY important that when checking for something (using ifs), then you use equal's twice (==), but when setting variable data, only a single equal's sign is required. This is extremely specific, so here's a simpler way of explaining this:
Well, what if you needed two conditions, where if the case you need isn't true, but something else needs to happen in that situation? Then the else tag is perfect for this situation. Here's how you can use it:
... essentially what's happening in the code above is that the code is setting the variable var to 2, and then checking: is var holding the value 2? Then, if it is, then it prints Case 1 to the output window. If var is not 2, then the code prints Case 2 to the output window instead. This can be adjusted accordingly to your needs.
There will be times when you need more than two conditions, but how do you do that? It's simple. All you have to do is use the elif command in between the if and else statements. That put in code looks like this:
... and that's literally just an example of how you can apply elif in your Python code. elif can be REALLY helpful at times because you can just save yourself from a lot of unnecessary if-statements. And the best part? You don't even need the else section -- you can just not put it entirely, and you're just left with a simple if-statement branch with no complications.
As stated before, these are completely optional, but when needed, worded like elif, in lowercase. Python keyword syntax is extremely case-sensitive. Here, below is an example of elif used in an if-statement without an else to finish it off. Even if there isn't a final else check at the end, it is still, not needed unless you would like to run specific code if something is not true.
... this is pretty much the same example as before, but edited to show that ending with elif is possible and also valid.
If/else statements can be used in many ways, and all of them include checking if a number or a string, or even a variable holds the value of another number, string, or variable. These statements are very versatile as you can have up to as many statements as you'd like, but please remember to add comments in between to help make it easier to read. There are three keywords to watch out for: if, elif, and else. Those three are key to helping form the statement.
If you have any questions, please head to the comments section below!
Share your opinions or ask questions.
-