Page Content

Tutorials

What are the Conditional Statements In Python

Conditional Statements In Python

Conditional Statements In Python
Conditional Statements In Python

Conditional statements are essential programming structures that enable a computer to make decisions based on predetermined criteria and depart from a straightforward, linear execution path. They give programs the ability to monitor conditions and adjust their behaviour accordingly. These statements are essential for regulating how a program is executed.

Analysing circumstances and selecting a course of action are common tasks in programming. In Conditional Statements Conditions are the fundamental building block of conditional assertions. A condition is a True/False assertion. Special values of the bool (Boolean) type are represented by the values True and False.

You frequently employ comparison operators, sometimes called relational operators, to establish conditions. A Boolean value (True or False) is returned by these operators once they have completed tests.

These are examples of common comparison operators:

   `==` (is equal to)

   `!=` (is not equal to)

   `<` (is less than)

   `>` (is greater than)

   `<=` (is less than or equal to)

   `>=` (is greater than or equal to)

Numerous elements influence decisions. These use Boolean expressions and logical operators. Python supports and, or, and not logical operators.
If both criteria are true, and: Returns True. For Example , hour >= 9 and hour < 17. If any of the conditions is true, or: Returns True. The opposite Boolean value is returned by not`.

The `if` Statement

If is the most basic form of conditional statement. It only permits you to run a block of code in the event that a particular condition is satisfied.

syntax:

if condition:
    # Statements to execute if condition is True
    statement1
    statement2
    #

To specify the block of statements connected to the if statement in Python, indentation is essential. Four spaces are required for block assertions. If condition is True, the indented statements are performed. The program skips the indented block and continues with the code after the if block if the condition is False.

Example:

password = "secret"
if password == "secret": # Condition using equality operator
    print("Access Granted")
# This line executes regardless of the condition
print("Program ends")

The words “Access Granted” and “Program ends” are printed if password is set to “secret”. “Program ends” is printed if password is anything else.

The `if-else` Statement

Adding a else statement after a if statement is optional. Follow an alternative set of procedures if the initial if condition is False.

syntax:

if condition:
    # Statements to execute if condition is True
    if_block_statement1
    # 
else:
    # Statements to execute if condition is False
    else_block_statement1
    # 

Indent the else sentence at the same level as the if as it lacks a condition. If if is True, the if block executes. In the event that the if condition is False, the else block runs. Only one of the chunks of code will run.

Example:

number = 10
if number % 2 == 0: # Condition checking for evenness using modulo operator
    print(number, "is an even number.")
else:
    print(number, "is an odd number.")

If the condition number % 2 == 0 is true, the program outputs “10 is an even number.” If number is 7, the else block would execute with False, displaying “7 is an odd number.”

The `if-elif-else` Statement

The if-elif-else structure is used to verify many requirements simultaneously. Shorthand for “else if” is elif.

syntax:

if condition1:
    # Block 1
elif condition2:
    # Block 2
# You can have multiple elif clauses
elif condition3:
    # Block 3
else:
    # Block N (optional, executed if all above conditions are False)

Python verifies the condition1. Block 1 is executed if True, and the remainder of the if-elif-else structure is skipped. condition2is checked ifcondition1is False. The remainder is skipped and Block 2 is executed if condition2 is True. This keeps happening down the elif chain. The else block executes if all if an elif conditions are False`. The block linked to the first true condition is the only one that is executed.

Example:

score = 85
if score >= 90:
    print("Grade: A")
elif score >= 80: # Checked only if score is NOT >= 90
    print("Grade: B")
elif score >= 70: # Checked only if score is NOT >= 80
    print("Grade: C")
else: # Executed if score is NOT >= 70
    print("Grade: Below C")

False is the first condition (score >= 90) for score = 85. The remaining elif and else lines are skipped since the second condition (score >= 80) is True`, resulting in the printing of “Grade: B”. In many cases, this chained form is simpler than placing if statements within else clauses for multi-way decisions.

Nested Conditionals

You can put one conditional expression inside another. The fulfilment of the outer statement’s condition is necessary for the inner statement to execute. For this to indicate which statements are part of which block, careful indentation is needed.

Conditional Expressions

In Python, a conditional expression (ternary operator) is a simplified if-else statement that yields a value.

Statement if true if condition else expression if false is the syntax.

Example:

age = 17
ticket price = 20 if age >= 16 else 10 # Assigns 20 if age >= 16, else 10
print("Ticket price:", ticket price)
This is equivalent to a longer `if-else` statement:
if age >= 16:
    ticket price = 20
else:
    ticket price = 10
print("Ticket price:", ticket price)
Both examples would output "Ticket price: 20".


Writing programs that can handle a variety of scenarios and inputs and regulate the flow of execution to accomplish desired outputs requires practice with these numerous conditional statement forms and an awareness of the interplay between conditions, comparison operators, and logical operators.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.