×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION Roadmap
     ❯   

FLOW CONTROL

Conditionals

Understanding Flow Control in Python: Conditionals

Introduction

Flow control in Python allows you to control the execution of your code based on specific conditions. The most common form of flow control is through conditional statements, which let you execute different code blocks depending on the evaluation of certain conditions. Python provides several ways to implement conditionals, including if, elif, and else statements.

Conditional Statements in Python

Conditional statements are used to perform different actions based on different conditions. The basic syntax involves using the if statement to check a condition, followed by an optional elif (else if) and an else block.

1. The if Statement

The if statement is used to test a condition. If the condition is True, the block of code within the if statement is executed.

Example:

age = 18

if age >= 18:
    print("You are an adult.")

In this example, the condition age >= 18 is checked. Since the condition is True, the message "You are an adult." is printed.

2. The else Statement

The else statement follows an if statement and is executed when the if condition is False. It provides an alternative block of code that runs when the condition is not met.

Example:

age = 16

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

Here, the condition age >= 18 is False, so the code in the else block is executed, printing "You are a minor."

3. The elif Statement

The elif (short for "else if") statement allows you to check multiple conditions. If the first if condition is False, Python checks the next elif condition, and so on. If all conditions are False, the else block is executed (if provided).

Example:

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: F")

In this example, the first if condition score >= 90 is False, so the program moves to the elif condition score >= 80. Since this condition is True, "Grade: B" is printed.

Nesting Conditionals

Python allows you to nest conditionals inside other conditionals. This can be useful for complex decision-making processes where multiple conditions need to be checked at different levels.

Example:

age = 20
has_permission = True

if age >= 18:
    if has_permission:
        print("You can enter the club.")
    else:
        print("You need permission to enter.")
else:
    print("You are too young to enter.")

In this example, the first if statement checks if the age is 18 or more. If True, it then checks if the person has permission. Based on the nested if statement, it prints the appropriate message.

Using Boolean Operators

Boolean operators like and, or, and not can be used to combine multiple conditions within a single if statement.

Example:

age = 22
is_student = True

if age >= 18 and is_student:
    print("You qualify for a student discount.")

Here, both conditions age >= 18 and is_student must be True for the message "You qualify for a student discount." to be printed.

Conclusion

Conditionals are a fundamental aspect of flow control in Python. They allow you to make decisions in your code by executing different blocks of code based on specific conditions. Understanding how to use if, elif, and else statements effectively is essential for writing clear and logical programs.


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2024 Nischal Lamichhane. All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.