Python Beginner Lesson 4: Running Different Code with if Conditions

In Python Beginner Lesson 4: Running Different Code with if Conditions, we cover if conditions in a beginner-friendly way. The final goal of this lesson is to understand the flow that runs different code depending on a condition. Instead of merely copying code, you will also see why the syntax is used and where beginners commonly make mistakes.

This topic is a basic frame for later automation, data analysis, and web API work. The example is small, but it trains the habit of reading input, processing it in order, and checking output.

Original Korean article

What You Will Learn in This Lesson

  • Core topic: if conditions
  • Today’s goal: understand the flow that runs different code depending on a condition
  • Practice flow: understand the concept → run the example → read the code line by line → try variations
  • Recommended study time: 30–50 minutes

Start with the Big Picture

Python Beginner Lesson 4: Running Different Code with if Conditions - A conditional statement chooses one execution path among several options by judging the situation.
A conditional statement chooses one execution path among several options by judging the situation.

When learning if conditions, the most important task is not memorizing grammar names. You need to understand what role the concept plays inside an actual program. At the beginner stage, keep asking these three questions.

  • Where did this value come from?
  • In what order does this code run?
  • Where should I store the result if I want to use it again?

Core Concept in Detail

  • An if statement checks whether a condition is true before running a block.
  • elif adds another condition when the previous one was false.
  • else handles every remaining case that did not match earlier conditions.

At first, the explanation may feel abstract. That is why it is best to run the example below and confirm the idea with your own eyes.

Terms Beginners Should Know

TermSimple Explanation
ConditionAn expression that becomes true or false.
BranchA path the program chooses based on a condition.
IndentationSpaces at the beginning of a line that show which code belongs to a block.

Practice Setup

Create a new Python file and run the example below. Using lowercase English letters and numbers in file names reduces avoidable errors. Save files as lesson.py or practice_01.py. After typing the code, avoid changing many things at once; run it, check the result, and then adjust small parts.

Example Code

score = int(input("Score: "))

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "D"

print(f"A score of {score} gets grade {grade}.")

Understanding the Code Line by Line

  1. The first line receives a score and converts it to an integer.
  2. The if line checks the highest range first.
  3. Each elif is checked only if the previous condition was false.
  4. The else block handles scores below 70, and the final line prints the result.

At this stage, the key is the order in which you read the code. Python normally runs from top to bottom. Even when functions or conditions create special flow later, beginners should first get used to the default top-to-bottom execution flow.

Change Values and Check the Result

Once the example runs, change only a very small part. Start with one number, one string, or one variable name so that errors are easy to find. After each change, save the file and run it again.

Part to ChangeWhat to Check
Input or variable valueCheck how the result sentence changes.
Output sentenceCheck whether the message is clearer for the user.
Code orderCheck whether changing the order creates an error or changes the result.

Common Errors and How to Fix Them

Python Beginner Lesson 4: Running Different Code with if Conditions - A score-to-grade practice is a good way to understand the order in which if, elif, and else are evaluated.
A score-to-grade practice is a good way to understand the order in which if, elif, and else are evaluated.
Error or SituationWhy It HappensHow to Fix It
IndentationErrorThe lines inside the condition are not indented consistently.Use the same number of spaces, usually four, for each block.
Wrong order of conditionsA broad condition was checked before a narrower one.Check higher score ranges first in this example.
ValueErrorThe input could not be converted to an integer.Enter a number, or add input validation later.

When an error appears, first read the last line of the error message. Then check the file name and line number. Most beginner errors come from parentheses, quotation marks, indentation, variable names, or type conversion.

Practice Problems to Try on Your Own

  • Change the score ranges and check whether the grade changes correctly.
  • Add a message for perfect scores.
  • Try entering boundary values such as 69, 70, 79, 80, 89, and 90.

When solving exercises, do not look for the answer code immediately. First write the input → process → output flow on paper. Once you can see the flow, writing the code becomes much easier.

Where This Connects in Real Work

If conditions does not end with a small example. It appears again when automating files, reading table data for analysis, and handling response values from web APIs. The beginner syntax you learn now becomes the shared language for tools such as pandas, requests, and FastAPI.

A Beginner-Friendly Analogy

When you first encounter if conditions, symbols may stand out before meaning. But grammar becomes tiring if you see it only as symbols. A better approach is to understand its role. The core of this lesson is to understand the flow that runs different code depending on a condition. In other words, you decide what task to give Python and practice writing the order of that task as code.

Think of a cooking recipe. You prepare ingredients, handle them in order, control the heat, and put the result on a plate. Python code is similar: prepare values, process them in a fixed order, and print or save the result. For beginners, gaining this sense of sequence matters most.

Follow the Execution Flow Visually

Before running code, divide your thinking into the three boxes below. This habit helps not only with simple examples but also with longer projects later.

SectionQuestion to AskMeaning in This Lesson
InputWhat value does the program receive first?The value the user enters or the value already written in the if conditions example.
ProcessWhat calculation or judgment happens?The part Python runs step by step to achieve the goal: understand the flow that runs different code depending on a condition.
OutputWhat result does the user confirm?Printed results, saved files, created graphs, or changed data.

Filling in this table by yourself improves code comprehension. Beginners struggle not only because they do not know syntax, but more often because they cannot separate input, processing, and output.

Debugging Routine: What to Check When an Error Happens

Do not delete code randomly when an error appears. Follow this order and you can find most beginner errors yourself.

  1. Read the last line of the error message.
  2. Check the file name and line number.
  3. On that line, check parentheses, quotation marks, colons, and commas.
  4. Make sure variable names exactly match the names defined above.
  5. Check whether values that must be calculated as numbers are still strings.
  6. Undo the part you just changed and run the code again.

After repeating this routine, errors start to look less frightening and more like clues. Python skill grows more from reading and fixing errors than from avoiding them entirely.

Application Direction: Grow Today’s Example a Little

Once this example feels familiar, keep the if conditions structure and change one of the input values, output sentences, storage methods, or repetition counts. You do not need to create a completely new example. At the beginner stage, many small variations are best.

  • Change variable names to make them more meaningful.
  • Rewrite output sentences as messages a real user would read.
  • Intentionally try invalid input and observe what happens.
  • Add comments to explain the code and check your understanding.
  • Try to produce the same result in a slightly different way.

Lesson Checkpoints

  • ☐ I can explain why if conditions is needed.
  • ☐ I ran the example code myself.
  • ☐ I can explain the role of at least three lines of code.
  • ☐ When an error appeared, I checked the last line and line number.
  • ☐ I changed and ran at least one practice problem on my own.

Related Articles

FAQ

Can I follow if conditions even if I am completely new to Python?

Yes. This lesson is written for readers who are learning programming for the first time. Focus less on memorizing code and more on understanding the execution result and flow.

I copied the example exactly, but I get an error. What should I check first?

Check parentheses, quotation marks, colons, indentation, and variable names first. Many beginner errors come from a single missing character or a variable name typed differently.

The example code looks too short. Should I study longer code?

Short code is better at the beginner stage. Understand a short program accurately first, then change values and add features one at a time.

What should I do next?

Change and run at least one exercise from this lesson. Then move on to Python Beginner Lesson 5: Automating Repetition with for and while Loops, where the concepts from earlier lessons will naturally appear again.

Next Lesson Preview

The next lesson is Python Beginner Lesson 5: Automating Repetition with for and while Loops. We will build on today’s execution flow and make the next concept more concrete.

References