Skip to main content

Conditional Statements: if (Part 1)

Following our exploration of Common Built-in Functions, this article introduces a fundamental concept in programming: conditional statements. We'll start with the most basic conditional statement, the if statement.


📚 Prerequisites

A basic understanding of Python variables, data types, and comparison operators.


🎯 Article Outline: What You'll Master

In this article, you will learn:

  • What conditional statements are and why they are useful.
  • The syntax of the if statement.
  • How to write a simple if statement.
  • The importance of indentation in Python.

🧠 Section 1: What are Conditional Statements?

Conditional statements allow you to control the flow of your program. They let you execute certain blocks of code only if a specific condition is met. This is the foundation of decision-making in your code.


💻 Section 2: The if Statement

The if statement is the most basic conditional statement. It executes a block of code only if a specified condition is True.

Syntax:

if condition:
# code to be executed if the condition is true
  • condition: An expression that evaluates to a boolean value (True or False).
  • :: A colon is required at the end of the if line.
  • Indentation: The code block within the if statement must be indented.

🛠️ Section 3: Writing a Simple if Statement

Let's look at a simple example:

age = 20

if age >= 18:
print("You are eligible to vote.")

In this example, the condition age >= 18 is True, so the code inside the if block is executed, and the message "You are eligible to vote." is printed.

If the condition were False, the code inside the if block would be skipped:

age = 16

if age >= 18:
print("You are eligible to vote.") # This line will not be executed

🔬 Section 4: The Importance of Indentation

Python uses indentation to define blocks of code. The code inside an if statement must be indented (usually with four spaces). If you don't indent the code, you'll get an IndentationError.

# Correct indentation
if True:
print("This is correct.")

# Incorrect indentation (will cause an error)
if True:
print("This is incorrect.")

💡 Conclusion & Key Takeaways

You've now learned the basics of the if statement, the most fundamental conditional statement in Python.

Let's summarize the key takeaways:

  • Conditional Statements: Control the flow of your program.
  • if Statement: Executes a block of code only if a condition is True.
  • Syntax: if condition:
  • Indentation: Python uses indentation to define code blocks.

Challenge Yourself: Write a script that takes a number as input and prints "The number is positive" if the number is greater than zero.


➡️ Next Steps

In the next article, we'll continue our exploration of conditional statements with "Conditional Statements: if-else (Part 2)".

Happy coding!


Glossary (Python Terms)

  • Conditional Statement: A statement that executes a block of code only if a specific condition is met.
  • Indentation: The spaces at the beginning of a code line.

Further Reading (Python Resources)