Conditional statements are used to execute specific commands only if a certain condition is met. In Python we use the key words “if”, “elif”, and “else” to guide the script through the different potential conditions.
These statements allow us to create more sophisticated scripts with code that is more flexible to different use-cases. By implementing conditional statements, we are able to save time as our code single segment of code will be able to make the appropriate decision based on the situation.
5th Python Script
The fifth python script created is:
These 3 statements demonstrate the ability to apply conditions to simple and complex requests. It is possible to use conditional statements that take multiple forms of input and can provide a relatively short piece of code that will run multiple checks against an input.
Statement 1
The first statement provides a simple method of outputting a string if a condition is met. This is achieved with the use of the “if” keyword
Statement 2
The second statement uses “elif” and “else” to expand on statement 1. When comparing two integers, there are three potential outcomes:
· x is bigger than y
· x is smaller than y
· x is the same as y
elif is the keyord use for “else if”, this means that as the condition of the previous if statement has not been met, another if statement is provided. Once all of the elif statements have been checked, if the condition has not been met, the “else” keyword is used as a catch-all statement to mean “if none of the above if statements are correct, then do this”
Statement 3
The third statement shows that we are able to use conditional statements as part of a function. As with all functions, we are able to pass parameter values. It is therefore possible to create functions with varying levels of complexity that, in this example, can show the ability to take input and work through the script until a condition is met.
The examples we have here show that we can pass both string and integer values and determine whether an exam has been passed or not. It is important to note that when creating conditional statements, it is important to create an if or elif action for each valid condition and to use else to catch any outlier.