Introduction to Python if Statements: Writing Conditionals and Indentation Rules

In programming, we frequently encounter situations where we want to execute code only if a specific condition is met. For example, “Add an item to the cart only if it is in stock” or “Display ‘Pass’ only if the score exceeds the passing line.”

In Python, we use the if statement to control this logic. This article explains the basic syntax of Python if statements and how conditional branching works.


目次

Basic Structure of an if Statement

Python’s if statement is written with the following syntax:

if condition:
    # Processing executed if the condition is True
    # (Indented block)

The important points are that a colon (:) is required after the condition, and the processing to be executed when the condition is met must be written with indentation.


Sample Code: Exam Pass/Fail Judgment

As a concrete example, let’s create a program that determines whether an exam score exceeds the passing line.

# Passing line and student's score
passing_score = 70
student_score = 85

# Check if the score is greater than or equal to the passing line
if student_score >= passing_score:
    print("Congratulations. You passed.")
    print(f"Your Score: {student_score}")

print("Judgment process finished.")

Output:

Congratulations. You passed.
Your Score: 85
Judgment process finished.

In this example, the condition student_score >= passing_score (85 >= 70) is evaluated. Since the result is True, the two indented print statements are executed.

If student_score were 50, the condition would be False. The indented block would be skipped, and only “Judgment process finished.” would be displayed.


Conditions and Boolean Values

The “condition” written immediately after the if statement can be anything that eventually evaluates to a Boolean value (True or False). You can also store the result of a comparison in a variable and use that variable as the condition.

# System maintenance status
is_maintenance_mode = True

# Use the boolean variable directly as a condition
if is_maintenance_mode:
    print("Maintenance in progress. Access denied.")

Output:

Maintenance in progress. Access denied.

In this way, the if statement simply looks at whether “the expression immediately following it is True” to decide whether to execute the code.


Importance of Indentation (Blocks)

In Python, the scope of the if statement (the range executed when the condition is true) is defined by indentation.

score = 90

if score > 80:
    print("Excellent work.")      # Indented: Affected by the condition
    print("Keep it up.")          # Indented: Affected by the condition

print("Processing complete")      # Not indented: Always executed

While other programming languages often use braces {} to define blocks, in Python, indentation has grammatical meaning. You must align the number of spaces correctly (usually 4 spaces).


Summary

  • Write conditional branches in the format: if condition:.
  • The indented block immediately following is executed only if the condition is True.
  • You can use comparison operators (>=, <) or boolean variables as conditions.
  • Blocks of code are defined by indentation.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次