Previous Lesson: Lesson 5: Variables Inside Strings in Python
Learn simple conditionals which includes if, elif, and else statements in Python. Know what “elif” is. Also know why they are powerful to use in our code. First, make a new file. Then, name it ex4.py.
if
After having everything set, type this in. I’ll explain every line later:
So, as what we have done first in our code, we assigned a variable (which is age) to the value 18. On Line 3, we said that “if the value of the variable age is greater than (>) 18.” Line 4 is the continuation of our Line 3. It says that it will print “You are already an adult.”
To summarize all these lines, if the variable age has a value that is greater than 18, then the string, “You are already an adult” will get printed on the Terminal.
Note: After having an if statement, the next line should get indented. What happens if it isn’t indented? If it isn’t indented, you will most likely create a Python error. Python expects you to indent something after you end a line with a colon (:). But don’t worry. When you hit ENTER right after having the colon, the next line will get indented automatically. But, if it didn’t get indented automatically, just hit TAB or four SPACEs to indent.
Now let’s run this:
What happened? Nothing. That’s because the value of age is lower than 18. Still, if your age is lower than 18, nothing gets printed. But, let’s try having an age that’s greater than 18. We’ll have something like. . . 22:
Now, let’s see what happens:
That’s right! You are already an adult if your age is greater than 18. Furthermore, you can have multiple print() in the if-statement block:
Run it:
Nice.
else
Now let’s move on to this next conditional–else. We’ll change the value of age back to 16. And then we’ll add something on Line 7 to Line 9:
The else means that if age does not reach the requirement or condition of the if-statement block, then the code inside this block gets executed. In this case, the strings get printed.
Note: When you press ENTER after the if block, there is still an indentation. However, the else should not be indented, except the code under it. To avoid an error, just press BACKSPACE. And don’t forget about the colon (:)!
elif
The next condition can be really exciting yet mind-twisting. But, I’ll tell you that this will be just a piece of cake. We’ll add this under our if-statement. Make sure that your code looks exactly like this:
Note: elif means is a nickname for “else-if.” These conditions go between the if and else block. The elif narrows down into more specific requirements. You’ll realize later why.
But what does our code mean right now? It works like this: if the age is not greater than 18, the next block (elif block) executes. This time, elif says that if age is equal to (==) 16, then our strings inside this elif block gets printed out:
But if the age is neither equal to 16, nor is it greater than 18 (or none of the if and elif conditions does not achieve the requirements), then else block will execute.
Note:
- == means “equal to” in Python.
- One equal sign (=) is used for assigning a value for a variable. In addition, this is not used for comparing values.
Try this: Try removing one equal sign from the “equal to” symbol (==). See what happens.
more elif
On our next example, I’ve changed the value of age to 39. I replaced the less than symbol (<) on Line 3 with ==. And then, I added some elif statements. But again, type all these:
I know that this seems to be a lot for you. But I promise that after you’ve finished this course, nothing will scare you. You’ll get used to it. Now let’s see what happens after running all these:
You rock! Always remember, if you don’t get the same result, don’t feel miserable, if you know what I mean. You can do this! Apply some of my tips:
- Read each word and each character (including the () and ==) loud, from bottom to top. For example, when you encounter a (), pronounce “parentheses!” out loud. It’s what many programmers do to spot a bug on their code.
- Pronounce each word and character out loud to get familiar with the code.
- Type your code little by little, block by block and then you run it. For example, first type in the whole if block before moving in to the next condition. Then run it. If it successfully runs, then move on to the next block. If it doesn’t run, double check. This will relieve you from reading everything just to look for the error.
And that’s it with if, elif, and else statements in Python. In the next lesson, we’ll talk about more of those symbols like the ==, the +=, and the >=.