DEV Community

Akash Singh
Akash Singh

Posted on

Basic Python: 6. Python Conditional Statements

Conditional statements are an essential part of programming that allow you to control the flow of your code based on certain conditions. In Python, you can achieve this using if, elif, and else statements. These statements help your program make decisions and execute different blocks of code based on whether specific conditions are true or false.

Table of Contents

  1. The if Statement
  2. The if-else Statement
  3. The if-elif-else Statement
  4. Nested Conditional Statements
  5. Ternary Operator 6 Combining Conditions
  6. Using Logical Operators

1. The if Statement

The if statement is used to execute a block of code only if a certain condition is true.

Syntax:

if condition:
    # Code to be executed if the condition is true
Enter fullscreen mode Exit fullscreen mode

Here's a simple example that checks if a number is positive:

num = 10

if num > 0:
    print("The number is positive")
Enter fullscreen mode Exit fullscreen mode

2. The if-else Statement

The if-else statement allows you to execute one block of code when a condition is true, and another block when the condition is false.

Syntax:

if condition:
    # Code to be executed if the condition is true
else:
    # Code to be executed if the condition is false
Enter fullscreen mode Exit fullscreen mode

Let's see an example that checks whether a number is even or odd:

num = 7

if num % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")
Enter fullscreen mode Exit fullscreen mode

3. The if-elif-else Statement

The if-elif-else statement is used when you have multiple conditions to check. elif stands for "else if" and allows you to test multiple conditions in sequence.

Syntax:

if condition1:
    # Code to be executed if condition1 is true
elif condition2:
    # Code to be executed if condition2 is true
else:
    # Code to be executed if none of the conditions are true
Enter fullscreen mode Exit fullscreen mode

Here's an example that categorizes a student's score into different grades:

score = 85

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
else:
    print("F")
Enter fullscreen mode Exit fullscreen mode

4. Nested Conditional Statements

You can also nest conditional statements within each other to handle more complex scenarios. Just be mindful of proper indentation to maintain code readability. Here's a basic example:

x = 10
y = 5

if x > 0:
    if y > 0:
        print("Both x and y are positive")
    else:
        print("Only x is positive")
else:
    print("Neither x nor y is positive")
Enter fullscreen mode Exit fullscreen mode

5. Ternary Operator

The ternary operator is a concise way to write simple if-else statements in a single line.
The ternary operator (expression if condition else expression) is a concise way to write conditional expressions.

is_sunny = True
weather = "It's sunny!" if is_sunny else "It's cloudy."
print(weather)
Enter fullscreen mode Exit fullscreen mode

6. Combining Conditions

You can combine conditions using logical operators and, or, and not.

temperature = 25
is_raining = False
if temperature > 20 and not is_raining:
    print("Perfect weather for a picnic!")
Enter fullscreen mode Exit fullscreen mode

7. Using Logical Operators

Logical operators help you build complex conditions by combining simpler ones.

x = 10
y = 5
if x > 0 and y > 0:
    print("Both x and y are positive.")
elif x > 0 or y > 0:
    print("At least one of x or y is positive.")
else:
    print("Neither x nor y is positive.")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)