Conditional Statements
A conditional statement is a type of programming statement that allows a program to make decisions based on whether certain conditions are true or false. It is used to control the flow of execution in a program based on certain criteria.
The if statement
The if statement is the most basic conditional statement in Python. It allows us to execute a block of code only if a certain condition is true. Here's a basic example:
x = 10
if x > 5:
print("x is greater than 5")
In this example, the if statement checks whether the variable x is greater than 5. Since the condition is true (since x is equal to 10), the code inside the if block is executed, and the program prints "x is greater than 5".
The if-else statement
The if-else statement is used when we want to execute one block of code if a certain condition is true, and another block of code if the condition is false. Here's an example:
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
In this example, the if statement checks whether the variable x is greater than 5. Since the condition is false (since x is equal to 3), the code inside the else block is executed, and the program prints "x is less than or equal to 5".
The if-elif-else statement
The if-elif-else statement is used when we have multiple conditions to check. The elif keyword stands for "else if", and allows us to check additional conditions if the previous condition(s) are false. Here's an example:
x = 7
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but less than or equal to 10")
else:
print("x is less than or equal to 5")
In this example, the if statement checks whether the variable x is greater than 10. Since the condition is false, the program moves on to the next condition, which checks whether x is greater than 5. Since this condition is true (since x is equal to 7), the code inside the elif block is executed, and the program prints "x is greater than 5 but less than or equal to 10".
Logical operators
Python provides several logical operators that can be used to combine multiple conditions. These operators include and, or, and not. Here's an example:
x = 7
y = 3
if x > 5 and y > 5:
print("Both x and y are greater than 5")
elif x > 5 or y > 5:
print("Either x or y is greater than 5 (or both)")
else:
print("Neither x nor y is greater than 5")
In this example, the if statement checks whether both x and y are greater than 5 using the and operator. Since this condition is false (since y is less than 5), the program moves on to the elif statement, which checks whether either x or y is greater than 5 (or both) using the or operator. Since this condition is true (since x is equal to 7), the code inside the elif block is executed, and the program continues to print "Either x or y is greater than 5 (or both)".
Nested if statements
We can also nest if statements inside other if statements to create more complex conditional statements. Here's an example:
x = 10
y = 5
if x > 5:
if y > 5:
print("Both x and y are greater than 5")
else:
print("x is greater than 5 but y is less than or equal to 5")
else:
print("x is less than or equal to 5")
In this example, the outer if statement checks whether x is greater than 5. If this condition is true, the program moves on to the inner if statement, which checks whether y is greater than 5. If this condition is also true, the program prints "Both x and y are greater than 5". If y is less than or equal to 5, the program prints "x is greater than 5 but y is less than or equal to 5". If x is less than or equal to 5, the program prints "x is less than or equal to 5".
Conclusion
Conditional statements are an essential part of programming in Python. They allow us to execute different blocks of code based on whether certain conditions are true or false. In this lecture, we have covered the basic if statement, the if-else statement, the if-elif-else statement, logical operators, and nested if statements. By understanding these concepts, you should be able to create more complex programs that can make decisions based on different conditions.
Basic Topics
- Introduction to Python
- Basic Syntax and Data Types
- Variables and Operators
- Input
- Conditional statements
- Loops
- Functions
- List
- Tuples
- Sets
- Dictionary
- Modules
- Packages
- Exception Handling
- Read/Write Files