Understanding if, else, and Ternary Expressions in Python

python if else statement

One of the first things you learn in Python is how to make your program think.
Not think like a genius philosopher — just make simple decisions:

“If this is true, do that… otherwise do something else.”

That’s exactly what if/else statements are for.

To keep things fun, let’s walk through a simple example:

meaning = 42
print('')

if meaning > 10:
print('Right on!')
else:
print('Not today')
# Ternary Operator
print('Right on!') if meaning > 10 else print('Not today')

Yep. Just a few lines, but they teach you almost everything you need to know.

The Setup: A Simple Variable

Before we jump into the conditions, here’s our starting point:

meaning = 42

We’re storing the number 42 in a variable called meaning — and honestly, if any number deserves that name, it’s 42.

Then we print a blank line:

print('')

Nothing magical — just keeping the output clean.

The Classic if/else Statement

Now the fun begins:

if meaning > 10:
print('Right on!')
else:
print('Not today')

Here’s what Python does:

  1. It checks the condition:
    Is meaning greater than 10?
  2. If yes, it runs this line:
Right on!

3. If not, it runs the other line:

Not today

Since meaning is 42, which is obviously greater than 10, we get:

Right on!

This is Python’s way of saying, “Yep, that condition is true, let’s go!”

What’s Really Going On?

Think of it like a tiny branching path:

Is it true? → Go this way  
Is it false? → Go that way

Your job as the programmer is just deciding what you want to happen in each case.

And the cool part?
if statements work with any condition you can think of:

  • age >= 18
  • password == “admin”
  • score != 100
  • len(name) > 3
  • is_logged_in == True

If it can be true or false, Python can handle it.

The Ternary Operator

Sometimes an entire if/else block feels too long—especially when all you want to do is print something simple.

Python has a shortcut for that:

print('Right on!') if meaning > 10 else print('Not today')

This line does the exact same thing as the full if/else above.
But now it fits into a single line.

Think of it as:

action_if_true  if condition  else action_if_false

It’s perfect when:

  • you want short, clean code
  • you’re returning a value
  • you’re making quick decisions
  • you don’t want to write 5 lines for a 1-line idea

Just don’t abuse it — ternaries are great for simple logic, but not for complicated if-chains.

Final Output

If you run the whole script, you’ll see:

Right on!
Right on!

One from the normal if/else
One from the ternary expression
Both based on the same condition.

Why?

Because every Python program — big or small — uses decision-making.

Whether you’re:

  • validating user input
  • checking login credentials
  • comparing numbers
  • handling errors
  • building game logic
  • filtering data
  • reacting to user choices

…you’ll be using if, elif, and else all the time.

And when you want cleaner code, ternary expressions are your best friend.

Conclusion

If/else statements are like the traffic lights of your program — they decide where the flow goes.
Ternary expressions are the clever shortcuts for when you already know the route.

Once these click in your mind, you’ll start writing cleaner, smarter, more flexible Python code.

For more visite this repo

Happy coding!!


Understanding if, else, and Ternary Expressions in Python was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.

This post first appeared on Read More