Python · Conditionals

Complete Guide to Python Conditionals

Conditionals allow your Python programs to make decisions and execute different code paths based on conditions using if, elif, else, and logical expressions.

1. What Are Conditionals?

A conditional tests a condition and executes code only if the condition is true. Python Conditionals. Conditionals take an expression, which is code that evaluates to determine a value, and checks if it is True or False . If it's True , we can tell our program to do one thing — we can even account for False to do another.

Python evaluates the expression: if it is True, it runs the indented block; otherwise, it skips it.

2. The if Statement

A Python if statement executes a block of code if its condition is True . It can be extended with elif (else if) for additional conditions and else for a default case when no conditions are met. Conditions can be combined using logical operators like and and or .

if condition:
    # code executed if condition is True

Example:

3. The else Statement

The else statement acts as a fallback that executes when none of the preceding conditions are true. This makes it useful for error handling, validation, and providing default values.

4. The elif Statement

'Elif' stands for 'else if' and is used in Python programming to test multiple conditions. It is written following an if statement in Python to check an alternative condition if the first condition is false. The code block under the elif statement will be executed only if its condition is true.

5. Comparison Operators

Comparison operators are used inside conditions to compare values.

OperatorMeaningExample
==Equal tox == 5
!=Not equal tox != 5
>Greater thanx > 5
<Less thanx < 5
>=Greater than or equalx >= 5
<=Less than or equalx <= 5
x = 10
if x != 5:
    print("x is not 5")

6. Logical Operators

Use logical operators to combine multiple conditions.

and – both conditions must be true

or – at least one condition must be true

not – reverses the condition

7. Nested Conditionals

Nested if statements are if statements inside another if statement. These can be very useful to make complex decisions in your script. They can be several levels deep. It's better to avoid deep nesting as it may get confusing to read such deeply nested if statements.

8. Shorthand If and Ternary Operator

Shorthand if is a way to write if statements more concisely and readable. It's also known as the ternary operator or conditional expression. The expression is the condition that you want to test. The value_if_true is the value assigned to a variable if the expression is true.

8.1 One-line if

8.2 One-line if-else (Ternary Expression)

9. Using Conditionals with Strings

Conditional statements in Python are used to execute certain blocks of code based on specific conditions. These statements help control the flow of a program, making it behave differently in different situations.

Case-insensitive comparison:

10. Truthy and Falsy Values

In Python, some values are considered False when evaluated in a condition:

11. Checking Membership with in

In Python, you can use the in keyword to check if an element exists in a list. A list is a collection of items, like numbers, words, or other objects. Here, 'apple' is in the list, so Python returns True . 'grape' is not in the list, so it returns False .

12. match Statement (Python 3.10+)

Python 3.10 introduced structural pattern matching with match / case, similar to switch in other languages but more powerful. The Python match statement allows you to compare values against patterns. Its structure is similar to the switch-case statement in other programming languages.

13. Conditionals with Lists and Length

Conditionals with lists and length refers to using statements (or other conditional logic) together with the function to make decisions based on the size or contents of a list. • → returns the number of elements in a list. • Conditionals (, , ) → allow you to execute code only when certain conditions are true. • Combined, they let you check whether a list is empty, has a certain number of items, or meets specific criteria.

Shorter and more Pythonic:

14. Using Conditionals Inside Loops

In Python, loops ( and ) allow you to repeat actions, while conditionals (, , ) let you make decisions. When combined, conditionals inside loops enable you to control the flow of iteration — deciding what happens for each element or iteration.

15. Input-Based Conditionals

Input-based conditionals in Python refer to using user-provided input (via the function or other sources) together with conditional statements (, , ) to control program behavior. In other words, the program asks the user for data, then makes decisions depending on what the user enters.
Enter your age:

16. Real-World Example: ATM Withdrawal Logic

Enter amount to withdraw:

17. Common Mistakes with Conditionals

MistakeWrongCorrect
Using = instead of ==
if x = 5:
if x == 5:
Forgetting colon
if x > 0\n    print("Hi")
if x > 0:\n    print("Hi")
Wrong indentation
if True:\nprint("Hello")
if True:\n    print("Hello")

18. Summary

In this guide, you learned:

Conditionals are the foundation of decision-making in Python. Mastering them will help you write smarter, more dynamic programs.