Conditional Statements in Python are used to decide the flow of execution of a program or code in Python. It’s like telling your program, “Hey, do this particular thing if it meets this condition, or if it doesn’t”. In Python, conditional statements are handled with “IF”. In this article, I am going to walk you through the different IF statements in Python, using a practical approach.
Here’s what I mean; I am not just going to give long definitions of the different types of IF statements in Python. We will see how they all work by building an online grading system for test scores with Python. It is important to note that IF Statements in Python works together with Python’s logical operators: AND, OR.
Let’s get right to it. Remember, we are going to be learning these IF statements while building an online grading system for students’ test scores.
The first thing to do is to launch your IDE. I am using the Jupyter notebook and I really recommend it. Jupyter notebook is something you get automatically when you download anaconda. Now, to writing code.
testscore = input("Please input your testscore:") testscoreint = int(testscore)
The above code is to present an interactive environment on the front-end so that the user/student can enter his/her score. Running the code above will return the image below:
Now that we have an interactive environment for the students, let us move on to applying the different types of IF statements to our mock grading system.
One-way IF statement (IF)
The one-way IF statement in Python is used to make single one-way decisions in your program. It is basically saying “if this is the situation, then do that.”. Let’s apply it to our mock online grading system to see how it works.
if testscoreint>=70 and testscoreint<=100: print("Congratulations, you scored 'A'")
The code above is basically telling the program that if a student enters a score that is between 70 and 100, then the system should display the text “Congratulations, you scored an A”. Let’s put it to test.
I tested it with a score of 87 because it is within the range of numbers specified in the condition. In a 1-way statement, there won’t be an output for values that do not meet the specified condition.
Two-way IF Statement (ELSE)
The 2-way IF statement leaves room for one alternative decision. “If it meets the requirements, do this. If it doesn’t, do this instead.” This is only possible with the addition of “ELSE”. Let me show you how it works. Here is our code:
testscore = input("Please input your testscore:") testscoreint = int(testscore) if testscoreint>=70 and testscoreint<=100: print("Congratulations, you scored 'A'") else: print("Nice Work!")
This is following our first example, but this time there is an option for those whose test score is not between 70 and 100. This code is telling Python, “Hey, if it’s not between 70 and 100, print Nice Work.” This means that any score between 0 to 69 is “Nice Work”, which is not good enough for a standard grading system. I mean, 10 out of 100 is definitely not “Nice Work””
Multi-way IF Statement (ELIF)
The multi-way IF statement is the most efficient of them all, and of course, it’s a bit more complicated. In the 2-way statement, an alternative decision is given. But, in the multi-way statement, 1 or more alternative conditions are provided for the program before a final alternative decision with “ELSE”. This is made possible with the addition of “ELIF”, a combination of ELSE and IF. Let’s get to the code so you can understand exactly what I mean
testscore = input("Please input your test score:") testscoreint = int(testscore) if testscoreint>=70 and testscoreint<=100: print("Congratulations, you scored 'A'") elif testscoreint>=60 and testscoreint<=69: print("Nice Work, That's a 'B'") elif testscoreint>=50 and testscoreint<=59: print("Averagely good! It's a 'C'") elif testscoreint>=40 and testscoreint<=49: print("You have a 'D'. Try harder") else: print("You failed, it's an 'F'")
The code above is saying the following things:
- If the score is between 70 and 100, display “Congratulations, you scored A” to the student.
- If it doesn’t meet the first condition, check if it meets the next one, if it’s between 60 to 69; or 50 to 59, etc.
- If it doesn’t meet any of the conditions specified, then go ahead and display the final alternate decision.
Let’s see how the code works in the following examples:
That’s pretty much how an online grading system with python works. You can try the codes out with different values, switch up the conditions as you please, and so much more.
You have questions? Leave them in the comments section.