The powerful way your code makes decisions
Written by PIRATE KING
Conditional statements are what give your code brains. They let the program choose what to do based on certain conditions. In Python, you define if and else statements using a simple syntax that relies on indentation to group code blocks. The if statement executes a block of code only if its condition is True. An optional else block executes if the condition is False. Here's a pseudo-code that illustrates the basics of conditional statements:
if to start the condition and else for the fallback case.:): Every conditional statement must end with a colon.IndentationError.Let's build a simple example: say we're creating a grading app.
Here's what's happening:
f"Your score is {score}" runs no matter what.score >= 50 is a Boolean expression — it either becomes True or FalseTrue, the two indented print statements run.Here are two important things that beginners often make when writing if statements:
: after the condition. Otherwise, it's a syntax error and the code will fail to run.if block. In our example, Great job! and You passed! belong to the if block.If you set score to 60, all the lines under the if block will be executed. In the example above, you get Your score is 60, Great job!, and You passed!. On the other hand, if you set score to 40, only the first print statement will run because the if statement evaluates to False, NOT executing the two print statements inside the if block.
See? Only Your score is 40 is printed. But what if you want to print something else when the score is under 50? For example, what if you want to say that you failed? That's where the else keyword helps. By putting else in the same vertical line as the if, you tell Python what to do when the first condition doesn't happen. Let's print Oh no!, You failed... in this case.
There you go. Since the score is 40, Python skips the if statement and runs all the lines indented under the else statement instead.
Now, sometimes you've got multiple conditions. That's where elif, short for else if, comes in. Let's imagine you're building a score grading app.
A score of 97 prints A. In an if-elif structure, Python stops evaluating as soon as it finds a match. Because 97 satisfies score >= 90, the remaining elif blocks are ignored entirely. If the score changes to 87, Python evaluates the first condition as False and moves to the next. Since 87 >= 80 is True, it prints B and exits the chain. Similarly, a score of 77 yields C, and 67 yields D.
Notice how all the print statements in the if and elif blocks are indented consistently. You can use as many elif statements as you want, but what if none of the conditions are true? That's where else statement comes in. What you have in the else block will be executed. Let's add this.
If you set score to 50 and run it, you get an F. Nice and flexible! Since the score is 50, it skips the first four and runs the else block.
Imagine we have a code that checks if a student passed the exam. Instead of writing:
You can use a neat shortcut for such simple if-else decisions — it's called the ternary operator.
Basically, the commented lines # and result = "Pass" if score >= 50 else "Fail" are identical. This one line does exactly what a multi-line if-else would - very clean for quick decisions. This demonstrates one of Python's core strengths; it reads naturally: result equals "Pass" if score is greater than or equal to 50 else "Fail".
Here's where it gets interesting. Python's logical operators - and and or - use something called short-circuit evaluation, meaning it stops checking conditions as soon as the result is known. Example time. I've got three functions here: check_1, check_2, and check_3. Now, don't worry about what function definitions are or how to define a new function yet; we'll get to them later. Just know when we call them, they print a message and return either True or False. For this example, I set them all to True, initially. Let's run this.
When you run this, you see all the Checking messages plus All passed at the end — because all three returned True, so Python evaluated every function. Now, what if the first check is False, like this?
Here, Python calls check_1() and sees it returns False. Since the and operator requires all to be True, the if statement stops instantly — it doesn't call check_2 nor check_3 because we already know the statement evaluates to False. That's short-circuiting with and.
Now, with the or operator, it's the opposite. Python stops as soon as it finds a True. Let me show you. Let's keep everything the same but replace the and operators with or's.
This prints Checking 1 and Checking 2 and then stops — it never calls check_3() because check_2() returned True and with the or operators, that's enough.
Flip the returned values in these checks (e.g. True to False or False to True) and watch how Python skips calls you might expect it to make. This will help you understand the efficiency short-circuiting brings to your code.
and: stops evaluation as soon as something is False.or: stops evaluation as soon as something is True.That's short-circuiting in action; it prevents unnecessary checks and keeps programs efficient.
Here's a powerful technique for writing clean code — chaining comparisons. Want to check if a value falls within a range? Python lets you do this in a natural, readable way — just like in math. Let me show you. Instead of writing:
You can chain comparisons like this:
The results are the same, but the latter mathematical syntax is cleaner and easier to read.
1. What is the output of the following code?
x = 8
if x > 5:
print("A")
elif x > 0:
print("B")
else:
print("C")2. Which of the following statements about Python's elif and else are true? (Select all that apply)
3. Suppose score = 50. What is the result of the following ternary operator? print("Pass") if score >= 50 else print("Fail")
4. Write Python code to:
age and set it to 17. Use an if statement to check if age is greater than or equal to 18.Adult. If false, print Minor.Thank you after the conditional block.Expected output:
Minor Thank you
5. Write Python code to:
a = False, b = True, c = TrueAll true if all three variables are true and At least one true if at least one of them is true.Expected output:
At least one true