LEARN Python - 2.1 If Statements When Python Starts Thinking for Itself- zerotopyhero.com

Learn Python – 2.1: If Statements: When Python Starts Thinking for Itself

This is the moment where Python officially stops being a robot that follows orders blindly.

Up until now, every line of code ran.
No choices.
No questions.
No hesitation.

Now we change that.

An if statement lets Python pause for half a second and ask:

“Should I run this… or should I skip it?”

And surprisingly, that one little question changes everything.

The Simplest If Statement Possible

Let’s start with the smallest working example.

				
					age = 18

if age >= 18:
    print("You are allowed in.")

				
			

Read it slowly, like a sentence:

If age is greater than or equal to 18, then print “You are allowed in.”

If the condition is true, Python runs the indented code.
If it’s false, Python skips it completely.

No error.
No drama.
It just moves on.

The Most Important Rule: Indentation

This part matters more than it looks.

Anything indented under the if statement only runs if the condition is true.

				
					if condition:
    this_runs_if_true
    this_also_runs_if_true

this_runs_no_matter_what

				
			

Python uses indentation to understand structure.
It doesn’t use curly braces.
It doesn’t guess.

Indentation is how Python knows what belongs together.

If you forget it, Python will complain loudly.
And that’s okay, everyone forgets it at first.

Conditions Are Just Yes-or-No Questions

An if statement always asks a question that can be answered with:

  • True

  • False

Some common examples:

				
					5 > 3
10 == 10
age >= 18
name == "Alex"
				
			

Each one answers a simple question.

Python doesn’t care how you feel about the answer.
It only cares whether it’s true.

Your First Interactive If Statement

Now let’s use something you already know: input().

				
					age = int(input("How old are you? "))

if age >= 18:
    print("Welcome! You're old enough.")

				
			

What happens here?

  1. Python asks for input

  2. The user types something

  3. The age is converted to a number

  4. Python checks the condition

  5. The message runs only if the condition is true

If the user types 15?
Nothing happens.

And that’s intentional.

When Nothing Happens, That’s Still a Result

This is important.

If the condition is false and nothing prints, Python did not fail.
It made a decision.

Beginners often think:

“It didn’t print anything… something broke.”

Nope.

Python simply decided:

“This doesn’t apply. I’ll skip it.”

That’s the whole point of if-statements.

Think in Plain English First

Before writing an if statement, say it out loud.

For example:

  • If the number is bigger than 10, print a message

  • If the name is Alex, say hello

  • If the user typed yes, continue

Then translate that sentence into code.

Python is very literal.
If you can explain it clearly in words, the code usually follows easily.

A Friendly Example

				
					name = input("What's your name? ")

if name == "Alex":
    print("Hey Alex! Nice to see you.")
				
			

Only Alex gets the greeting.
Everyone else gets silence.

Harsh? Maybe.
Educational? Definitely.

What You’ve Learned

You now know how to:

  • Write your first if statement

  • Ask a yes-or-no question

  • Control whether code runs

  • Use indentation to group code

  • Understand that skipping code is normal

That’s a huge step.

Mini Quiz

Try these before moving on:

  1. What does an if statement do in plain English?

  2. What happens if the condition is false?

  3. Why is indentation important?

  4. What kind of answer must a condition have?

  5. Will this code always print something? Why or why not?

				
					age = 10

if age > 18:
    print("Adult")
				
			
ZeroToPyHero