Back to Python
2026-01-266 min read

JS If Conditions (Python Programming)

Learn JS If Conditions (Python Programming) step by step with clear examples and exercises.

Title: Mastering Decision Making in Python Code - Understanding If Conditions

Why This Matters

In programming, if conditions are a fundamental concept that allows us to make decisions within our code. They enable us to create dynamic and flexible programs that can handle various scenarios based on the input data. Understanding and mastering Python's if conditions is crucial for solving real-world problems, acing coding interviews, and debugging complex issues in your projects.

Prerequisites

Before diving into Python's if conditions, you should have a good understanding of:

  1. Basic Python syntax (variables, data types, operators)
  2. Loops (for and while loops)
  3. Functions
  4. Error handling (try-except blocks)
  5. Understanding the difference between == and = in Python
  6. Familiarity with control flow statements like while, for, and if

Core Concept

If Statements

In Python, the if statement is used to test a condition and execute code based on its result. The general structure of an if statement is as follows:

if condition:

Code block executed if condition is True


Let's break down this example:

1. `condition` - A boolean expression that evaluates to either `True` or `False`.
2. `# Code block executed if condition is True` - The code block that will be executed if the condition is true.

Here's a simple example:

x = 10

if x > 5:

print("x is greater than 5")


In this case, since `x` is equal to 10 and 10 is indeed greater than 5, the message "x is greater than 5" will be printed.

### Else Statements

The else statement can be used to specify a block of code that should be executed when the if condition is False:

if condition:

Code block executed if condition is True

else:

Code block executed if condition is False


Let's add an else statement to our previous example:

x = 10

if x > 5:

print("x is greater than 5")

else:

print("x is not greater than 5")


Now, if `x` were less than or equal to 5, the message "x is not greater than 5" would be printed instead.

### Elif Statements

The elif statement can be used to add additional conditions to an if statement:

if condition1:

Code block executed if condition1 is True

elif condition2:

Code block executed if condition1 is False and condition2 is True

else:

Code block executed if neither condition1 nor condition2 is True


In this structure, the first condition `condition1` is checked. If it's True, the corresponding code block is executed, and the rest of the conditions are ignored. If `condition1` is False, then the next condition `condition2` is checked, and so on. This allows for more complex decision-making in your code.

### Nested If Statements

Nesting if statements means placing one if statement inside another:

if condition1:

Code block 1

if condition2:

Code block 2

else:

Code block 3

else:

Code block 4


In this example, the first condition `condition1` is checked. If it's True, then `condition2` is checked, and the corresponding code blocks are executed accordingly. If `condition1` is False, then the entire nested if statement is skipped, and the code execution continues with the next line after the else block.

Worked Example

Let's create a simple program that prompts the user to enter their age and provides feedback based on their age group:

age = int(input("Enter your age: "))

if age < 13:
print("You are a child.")
elif 13 <= age < 18:
print("You are a teenager.")
elif 18 <= age < 65:
print("You are an adult.")
else:
print("You are a senior citizen.")

Common Mistakes

  1. **Forgetting to use colons (:) after the if, elif, and else statements.**
  2. Misusing equal (==) instead of assignment (=).
  3. Not accounting for all possible conditions when using multiple elif statements.
  4. Using indentation inconsistently within nested if statements.
  5. Forgetting to convert input data to the appropriate type before comparing it in the condition.
  6. Not handling edge cases, such as age being 0 or a negative number.
  7. Not considering the order of conditions when using multiple elif statements.
  8. Using and and or operators incorrectly within if conditions.

Practice Questions

  1. Write a program that checks whether a number is even or odd.
  2. Write a program that calculates the grade of a student based on their marks (passing grade: 40).
  3. Write a program that asks for two numbers and determines which one is greater.
  4. Write a program that validates a password input, requiring it to be at least 8 characters long and contain both uppercase and lowercase letters, as well as a digit and a special character.
  5. Write a program that calculates the area of a triangle given its base and height.
  6. Write a program that determines whether a number is prime or not.
  7. Write a program that checks if a given year is a leap year.
  8. Write a program that asks for two dates (day, month, year) and compares them to determine which one is earlier.
  9. Write a program that asks for a person's name and their age, then prints a personalized greeting based on the time of day (good morning, good afternoon, good evening).
  10. Write a program that calculates the factorial of a given number.

FAQ

  1. What happens if there are no conditions to be checked in an if statement?
  • If there are no conditions to check, the code block inside the if statement will not be executed. It's equivalent to having an empty if statement.
  1. Can I use multiple variables in a single if condition using AND (&&) or OR (||)?
  • Yes, you can combine multiple conditions using AND (and) and OR (or). For example:
x = 10
y = 20
if x > 5 and y < 30:
print("Both conditions are true.")
  1. What is the difference between == and = in Python?
  • == is used to compare values, while = is used for assignment (to set a variable's value). For example:
x = 10
y = 20
if x == y: # Compares the values of x and y
print("x and y have the same value.")
else:
print("x and y have different values.")
  1. What is the purpose of the pass statement in Python?
  • The pass statement is a placeholder for code that will be added later or serves as a no-op (no operation) when you want to include syntax but don't need any functionality at the moment.
  1. Can I use multiple lines within an if, elif, or else block?
  • Yes, you can write multiple lines of code within an if, elif, or else block, as long as they are properly indented.
  1. What is the difference between if, elif, and else in Python?
  • The if statement checks a condition and executes the corresponding code block if the condition is True. The elif (short for "else if") statement serves as an additional condition that gets checked only when the previous conditions are False. The else statement is used as a catch-all for any unchecked conditions or cases.
  1. Can I use multiple if, elif, and else statements in a single line?
  • No, you cannot use multiple if, elif, or else statements on the same line. Each condition should be placed on separate lines with proper indentation for readability and maintainability.
  1. What is the maximum number of levels for nested if statements in Python?
  • There is no limit to the number of levels you can nest if statements in Python, but it's generally recommended to avoid deep nesting to maintain code readability and simplicity.
JS If Conditions (Python Programming) | Python | XQA Learn