Learn Python: Conditional Statements (if/elif/else)
Conditional statements let your program decide what to do based on certain conditions. You can check whether a value meets certain criteria and execute different code blocks accordingly. This lesson focuses on using if, elif, and else to implement decisions in Python. In Visual Studio Code (VS Code), you can write these conditionals in a script and run it in the integrated terminal to see how the logic unfolds.
What will be covered
• Using if statements to run code only if a condition is True.
• Expanding with elif for multiple conditions and else for a default action.
• Examples of checking numeric ranges or user input and responding differently.
• Proper indentation to define each code block in Python.
• Basic nested if statements, if needed.
Basic if Statement
An if statement checks a condition. If it’s True, Python runs the indented block beneath it. Otherwise, it skips that block.
number = 10
if number > 0:
print("The number is positive.")
If number > 0, the line “The number is positive.” prints. Otherwise, Python does nothing in response to this if statement. Indentation determines which lines belong to the if block, so be consistent.
if...else
Adding an else gives the program something to do if the condition is False. This is useful when you want exactly one of two outcomes to occur.
number = -5
if number >= 0:
print("The number is non-negative.")
else:
print("The number is negative.")
Here, if number >= 0 is True, Python prints “The number is non-negative.” Otherwise, it prints “The number is negative.”
if...elif...else
Sometimes you have multiple conditions to check in sequence. You can chain as many elif statements as you want to handle different scenarios. If none match, else acts as a fallback.
score = 75
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
Python checks each condition in order until it finds one that’s True. If none are True, it executes the else block. In this example, score=75 matches the elif score >= 70 branch, printing “Grade: C.”
Indentation and Block Structure
Python groups code by indentation. The code under an if or elif must be indented the same amount. If you have another if inside, that inner code must be further indented. Keep your spacing consistent (for instance, 4 spaces for each level).
Practice Exercises
- Write a program that asks the user for a number (use input(), then convert to int). Print whether it’s positive, negative, or zero with an if...elif...else chain. Print something like “The number is positive” or “The number is negative” or “Zero”.
- Simulate a simple password check. Store a password in a variable, ask the user to input a guess, then if it matches, print “Login successful,” else print “Incorrect password.” (Be sure to only use the concepts covered so far.)
- Write a program that converts a numeric test score (0-100) into a letter grade: A for 90 and above, B for 80-89, C for 70-79, D for 60-69, and F otherwise. Test it with a few scores to see which message prints.
- Optional: nest an if check inside another if. Ask for a numeric input, then first check if it’s non-negative. If it’s non-negative, check if it’s even or odd. If it’s negative, just print “Negative number.” This can help you see how indentation structures nested blocks.
Summary
Conditional statements give your program the power to make decisions. By using if, elif, and else, you can handle different inputs or situations and produce different outputs. This is essential for creating interactive or dynamic scripts. Properly indent your code to define the blocks under each condition. These conditionals will become the core of many more advanced Python features, such as loops and functions, where you’ll integrate boolean checks and branching logic in more elaborate ways.