Hello everybody,
It’s Michael, and today’s lesson will be on conditional statements in Python. I covered Java conditional statements in Java Lesson 4: IF-ELSE statements & Logical/Comparison Operators, and in case you’re wondering, “conditional statements” is a more technical way of saying “if-else statements” (or “if statements” and “else statements”, for that matter). I will also cover user input in Python.
Just like Java, Python has if, else, and else if statements, except in Python, else if is referred to as elif. The logic behind conditional statements in Python is the same as in Java. By that I mean in any group of conditional statements, if will always come first, followed by any elif statements and an else statement at the end of the group. Here’s an example program with conditional statements (since I wrote this for explanatory purposes, I won’t have any sample output):
if (score >= 300) and (score <= 579); print (“POOR”)
elif (score >= 580) and (score <= 679); print (“FAIR”)
elif (score >= 680) and (score <= 739); print (“GOOD”)
elif (score >= 740) and (score <= 799); print (“VERY GOOD”)
else; print (“EXCEPTIONAL”)
In this program, there is a variable called score (a reference to FICO Credit Score) and based on the value of that variable, one of five things will be displayed-POOR, FAIR, GOOD, VERY GOOD, and EXCEPTIONAL. If the if statement is not true, each of the elif statements will be analyzed from the top down to see which one is true; once a true statement is found, any code on that statement will be executed and the compiler will stop analyzing the group of conditional statements. If neither the if statement nor any of the elif statements are true, then the else statement will execute.
Now. everything I just said will hold true in most cases, provided score falls between 300 and 849 inclusive. But what if the value of score was outside that range? You would simply write another elif statement that goes something like elif (score >= 800) and (score <= 850); print ("EXCEPTIONAL"). You would also have to revise the else statement so that it reads something like else; print ("NOT POSSIBLE"). By writing the new elif statement, the compiler will know to print out ERROR if score isn’t between 300 and 850 inclusive.
Now, let’s demonstrate conditional statements. Here’s a simple example:
rt = 78
if (rt >= 60) and (rt <= 100):
print(“fresh movie”)
else:
print(“rotten movie”)
And here’s the output:
fresh movie
Using Rotten Tomatoes’ movie scoring system as an example (the rt refers to Rotten Tomatoes score), the compiler will print out “fresh movie” if rt is between 60 and 100 and “rotten movie” if rt is below 60. Since rt is 78 (that’s the current score for the movie Good Boys), “fresh movie” will be displayed.
Now, here’s a more complex conditional statements example, with several elif statements:
IGN = 6.6
if (IGN == 10):
print(“MASTERPIECE”)
elif (IGN >= 9) and (IGN <= 9.9):
print(“AMAZING”)
elif (IGN >= 8) and (IGN <= 8.9):
print(“GREAT”)
elif (IGN >= 7) and (IGN <= 7.9):
print(“GOOD”)
elif (IGN >= 6) and (IGN <= 6.9):
print(“OKAY”)
elif (IGN >= 5) and (IGN <= 5.9):
print(“MEDIOCRE”)
elif (IGN >= 4) and (IGN <= 4.9):
print(“BAD”)
elif (IGN >= 3) and (IGN <= 3.9):
print(“AWFUL”)
elif (IGN >= 2) and (IGN <= 2.9):
print(“PAINFUL”)
elif (IGN >= 1) and (IGN <= 1.9):
print(“UNBEARABLE”)
else:
print(“DISASTER”)
And here’s the output:
OKAY
Using IGN’s gaming review scoring system in this example, the compiler will look at the value of IGN (referring to a game’s IGN score) and based on that value, will first analyze the if statement, then all of the elif statements from the top down, and finally the else statement (which will only be analyzed if neither the if statement nor the elif statements are true). Remember that the compilers will stop analyzing once it finds a true statement, which in this case would be elif (IGN >= 6) and (IGN <= 6.9) (which would print out OKAY).
- Also remember that a colon would come after each conditional statement (in other words, right before the block of code that’s supposed to be executed with a particular statement). Unlike Java, there’s no need to place the code that goes with conditional statements in brackets (and you’ll likely get an error if you do so).
Now, one thing I would like to mention is that you can sometimes write conditional statements in a handy little shorthand.
Let’s use the if-else Rotten Tomatoes score example. Here’s the shorthand way of writing that program:
print(“fresh movie”) if (rt >= 60) and (rt <= 100) else print(“rotten movie”)
Even though you will need to assign a value to rt on a separate line, you can still write the if–else statement on the same line. However, if you’ve got multiple elif statements (like the IGN example), then this approach will not work.
Now, what if the value of a certain variable wasn’t already set. What if you needed to set the value of a certain variable?
This brings me to the next topic I will cover in this post-user input in Python. Just like Java, Python also allows for user input. Using the IGN scoring program from a previous example, here’s how to handle user input in Python:
IGN = float(input(“Give me an IGN score: “))
if (IGN == 10):
print(“MASTERPIECE”)
elif (IGN >= 9) and (IGN <= 9.9):
print(“AMAZING”)
elif (IGN >= 8) and (IGN <= 8.9):
print(“GREAT”)
elif (IGN >= 7) and (IGN <= 7.9):
print(“GOOD”)
elif (IGN >= 6) and (IGN <= 6.9):
print(“OKAY”)
elif (IGN >= 5) and (IGN <= 5.9):
print(“MEDIOCRE”)
elif (IGN >= 4) and (IGN <= 4.9):
print(“BAD”)
elif (IGN >= 3) and (IGN <= 3.9):
print(“AWFUL”)
elif (IGN >= 2) and (IGN <= 2.9):
print(“PAINFUL”)
elif (IGN >= 1) and (IGN <= 1.9):
print(“UNBEARABLE”)
else:
print(“DISASTER”)
And here’s the output if I use 5.1 as the value of IGN:
Give me an IGN score: 5.1
MEDIOCRE
In Python, user input is much simpler than it is in Java. In Java, you have to import a Scanner class for user input AND create a separate Scanner variable (along with familiarizing yourself with all the methods in the Scanner class). With regards to user input, Python is much simpler to use, as it has a built-in user input function-input()-so there is no need to import anything. As a result, it only take one line of code to handle user input in Python, as opposed to the three or four lines of code it can take in Java.
I have three important tips regarding user input in Python, which include:
- You don’t technically need put in any parameters for the
input()method, but it’s suggested that you do so, as I did withinput(Give me an IGN score: ). - You can set the value of certain variable to the user’s input, as I did with the value of
IGN. - The default type of user input in Python is String, so if you want to set the user input to another type, write something like this
other type(input(text for input)). In my IGN example, I wanted input of typefloat, so I wroteIGN = float(input(Give me an IGN score: )). By writing this, my input forIGNwould be read asfloatand notString.
Thanks for reading,
Michael
