Mastering Python : Day 6
If-Else-Elif Statements in Python
In Python, if-else-elif statements are used to control the flow of a program based on conditions. Here's a breakdown of how they work:
If Statement
The if statement is used to execute a block of code if a certain condition is true.
if condition:
2 # code to be executed if condition is true
Example:
x = 5
2if x > 10:
3 print("x is greater than 10")
In this example, the code inside the if block will not be executed because the condition x > 10
is false.
If-Else Statement
The if-else statement is used to execute a block of code if a certain condition is true, and another block of code if the condition is false.
if condition:
2 # code to be executed if condition is true
3else:
4 # code to be executed if condition is false
Example:
x = 5
2if x > 10:
3 print("x is greater than 10")
4else:
5 print("x is less than or equal to 10")
In this example, the code inside the else block will be executed because the condition x > 10
is false.
If-Elif-Else Statement
The if-elif-else statement is used to execute a block of code if a certain condition is true, another block of code if the first condition is false and a second condition is true, and another block of code if both conditions are false.
if condition1:
2 # code to be executed if condition1 is true
3elif condition2:
4 # code to be executed if condition1 is false and condition2 is true
5else:
6 # code to be executed if both conditions are false
Example:
x = 5
2if x > 10:
3 print("x is greater than 10")
4elif x == 5:
5 print("x is equal to 5")
6else:
7 print("x is less than 5")
In this example, the code inside the elif block will be executed because the condition x > 10
is false, and the condition x == 5
is true.
Nested If-Else Statements
If-else statements can be nested inside each other to create more complex conditional logic.
condition1:
2 if condition2:
3 # code to be executed if both conditions are true
4 else:
5 # code to be executed if condition1 is true and condition2 is false
6else:
7 # code to be executed if condition1 is false
Example:
x = 5
2y = 10
3if x > 5:
4 if y > 10:
5 print("x is greater than 5 and y is greater than 10")
6 else:
7 print("x is greater than 5 but y is less than or equal to 10")
8else:
9 print("x is less than or equal to 5")
In this example, the code inside the inner if block will not be executed because the condition x > 5
is false.
I hope this helps! Let me know if you have any questions or need further clarification.
Question: Write a Python program to determine the type of a triangle based on the lengths of its sides. The criteria are as follows:
If all sides are equal, the triangle is equilateral
If two sides are equal, the triangle is isosceles
If no sides are equal, the triangle is scalene
If the sum of the lengths of any two sides is less than or equal to the length of the third side, the triangle is not valid
Task: Write a Python program using if-else-elif statements to determine the type of a triangle based on the lengths of its sides.
Input: The lengths of the three sides of the triangle (three integer values)
Output: The type of the triangle (equilateral, isosceles, scalene, or not valid)
Go ahead and try to solve it!