In this article we will explore the If-else and elif statements in Python. These statements exist to execute code if certain conditional statements are satisfied. They can be very helpful for specific needs or tasks. For example while working on an online form, which requires the user to enter a username. The user has strongly entered a number and we need to process it is an error if the entered data doesn’t match according to the requirements, or stores the right entered data. Another example might be when asking user to enter the age we would want to specify the data to be a number, with a certain range limit.
Working with if-else and elif statements in Python
To handle such problems like those mentioned earlier, most languages have conditional statements or keywords, Python similarly offers it’s own approach that operates on the same behavior.
It is a simple conditional statement which checks if a condition is true and executes a statement.The if statement can be nested to create huge conditional statements.
Syntax: if boolean_expression: statement(s)
In the following example, we have a condition that will evaluate to true and statement (s) if the block is executed.
a = 2 b = 4 if a < b: print(" B is greater than A")
Output: B is greater than A
If the result of the assessment of the if condition is false and we also want to influence the result, then we include an else statement.
Syntax: if condition: statement_1 else: statement_2
Let us look at an example password program,
password = 'Mission' if len(password) < 6: print('password too weak - should be at least 6 characters') else: print('your password was accepted')
When a program needs to handle more than two cases, we need to use multiple else and if statements together. The keyword elif means otherwise if.
Syntax: if condition: statement_1 elif: statement_2 else: statement_3
For example, we have a program that needs to determine the type of a triangle based on 3 integer inputs.
- A scaling triangle is a triangle that has different lengths on all three sides.
- An isosceles triangle has two sides of the same length.
- An equilateral triangle is a triangle in which all sides are equal.
a = 5 b = 5 c = 5 if a != b and b != c and a != c: print('This is a scalene triangle') elif a == b and b == c: print('This is an equilateral triangle') else: print('This is an isosceles triangle')
Output: This is an equilateral triangle.
Nesting conditional statements
All the above mentioned conditional statements can be included within each other to form as part of a larger program statement provided that they meet the right syntax format. Such a process of utilizing or linking multiple conditional statements together is referred to as Nesting. Let us look at an example program.
Output: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # if Block is end here # if Block is end here
In this program. we accept an input number from the user and according to the value given we display the output of the kind of value that it is. Negative for numbers less than zero, an output for zero and Positive for numbers greater than zero.
#Program to display whether the associated user input is Positive, Negative or Zero n=int(input("Enter a number:")) if n: if n > 0: print("Positive") elif n<0: print("Negative") else: print("zero")
Output: Enter a number:5 Positive
In the above program it checks if a value is defined/assigned to n before proceeding. Let us look at another example:
a=15 if a>10: if a>20: print("a is greater than 20") else: print("a is greater than 10")
Hence, we have gone through the concepts of if-else and elif statements in Python. In the next article we will look at Even-Odd logic in Python.