Conditionals with Python
Conditionals with Python
Conditionals are used in programming to control the flow of a program based on different True of False conditions. Python traditionally provides if statements to serve this purpose. Moreover, with Version 3.10, Python has provided us a switch-case equivalent which is the match statement. In this article, let’s talk about implementation of these two statements with a student grade evaluating program example.
if Statement
if statements generally use comparison operations to evaluate conditions. Additionally, we can use boolean operations to combine conditions.
Let’s implement a simple program to decide student grade based on their marks. A mark from 75 to 100 would be evaluated for an ‘A’ grade. We use the and operation to combine the two conditions.
if 100 >= marks and marks >= 75:
print('A')
However, Python provides a more succinct way of combining comparison conditions which is known as comparison operator chaining. Let’s use this approach in our program.
if 100 >= marks >= 75:
print('A')
A mark below 45 would be evaluated for an ‘F’ grade. We have to use the elif statement with this condition so that Python interprets this condition only when the previous conditions have been evaluated as False.
elif 45 > marks >= 0:
print('F')
At the end of our if-elif statements we can add an else statement to provide any default value. Let’s use this to add a simple validation code to our program so that any value below 0 and above 100 would be given an “Invalid Score” output.
else:
print("Invalid Score")
Student Grade Evaluation Program
Now let’s write the complete code for our program. We will get the user input and cast it to int type. We will then write if-elif statements for each condition to generate grades for scores from 0 to 100. Any score not in this range will be evaluated as an invalid score with the else statement.
marks = int(input("Enter marks: "))
if 100 >= marks >= 75:
print('A')
elif 75 > marks >= 65:
print('B')
elif 65 > marks >= 55:
print('C')
elif 55 > marks >= 45:
print('D')
elif 45 > marks >= 0:
print('F')
else:
print("Invalid Score")
match Statement
match statement is used to compare a value against several patterns and control the flow of the program accordingly.
Let’s write a program to decide the student’s Pass or Fail status based on their grade. We use the match statement to compare the grade variable with our grading system values. Students with ‘A’ or ‘B’ or ‘C’ or ‘D’ grades are evaluated as Pass. We have to use the bitwise operator ‘|’ to match multiple patterns. The default case ‘case _’ outputs an error message of “Invalid Grade”. Unlike switch-case in other languages, we don’t need break statements to stop the execution of mismatching case statements in Python. Python only executes the code written in the first matching pattern.
grade = input("Enter grade: ")
match grade:
case 'A' | 'B' | 'C' | 'D':
print("Pass")
case 'F':
print("Fail")
case _:
print("Invalid Grade")
Note:
ifvs.match
You might have noticed we can write this program withef-elif-elserather than usingmatch. Yes, when we write simple programs we can stick withifstetaments. Python has introducedmatchfor more complex and advanced pattern matching tasks.
Student Grade & Status Evaluation Program
Now, let’s write a complete program to evaluate the grade based on marks and assign Pass of Fail status based on the grade. We have combined the previous two programs to assign a grade value and a status value.
marks = int(input("Enter marks: "))
# evaluate grade
if 100 >= marks >= 75:
grade = 'A'
elif 75 > marks >= 65:
grade = 'B'
elif 65 > marks >= 55:
grade = 'C'
elif 55 > marks >= 45:
grade = 'D'
elif 45 > marks >= 0:
grade = 'F'
else:
grade = "Invalid Score"
# evaluate status
match grade:
case 'A' | 'B' | 'C' | 'D':
status = "Pass"
case 'F':
status = "Fail"
case _:
status = "Invalid Score"
# print result
print(f"Grade: {grade} Status: {status}")
Comments
Post a Comment