So far, we have covered the basics of how to write some simple code in sequential order. It is far more common, though, that you want your program to distinguish two or more alternatives. That’s when we need the so-called conditionals.
Just like in English, your condition starts with “if”, but unlike your normal phrases, this “if” needs to be followed by a variable and a comparative value, connected by an operator that describes the desired relationship. You may put the condition (without the if) into brackets. At the end, it is very important to put a colon.
Example for a condition:
in words: if the variable a is bigger than 10
in Python: if (a > 10):
In Python, it’s much shorter and thus more convenient.
Now comes the most difficult part: If you press enter, the curser automatically indents the following instructions. This is important. If you just copy something from somewhere else without indentation, Python won’t understand it and will display and error. Writing the instructions, i.e. what to do if the condition is true, is simple: It’s just your normal code.
Python also gives you the possibility to define instructions for the cases in which your conditions is not true. For this, you just write “else” at the same indentation level as your “if” and add a colon. You press enter and write your instruction in the next line. As “else” is the scenario in which your previous condition is not true, you don’t need to define any condition. Needless to say that you cannot use “else” if there is no previous “if”.
Some of you are probably now thinking, “That’s nice, but usually life is not black or white.” This is true, which is why Python has the option of adding one or more “elif” between “if” and “else”. What is “elif”? For once, this is not an actual English word, but a contraction between else and if. This explains also how it is used: It refers to a case in which your original condition (if) is not true (→ else), but you can define a new condition for your instruction (→ if). “elif” works just like “if”: you write your condition, possibly in brackets, and add the colon.
Before you try to write your first code with conditionals, one important piece of advice: Besides indention errors and missing brackets, most errors are due to users mistaking the assignation sign (=) for the equal sign (==). If your variable should be equal to a certain value, you must put ==. Otherwise Python won’t understand you.
Remember our favorite exercise on temperature conversion?

Now you can provide some additional information to the users: Let them know whether water boils at this temperature (under typical conditions).
Your code could look like this:

Of course, you could also leave out “elif” in this case, because we already have a validation since we define the user input as an integer. But as we’re still practicing, it’s nice to have all three options. #mastery11