Python is a powerful and beginner-friendly programming language known for its simplicity and readability. One of the most distinctive features of Python is its use of indentation to define code blocks. Unlike other programming languages that use braces {}
, Python relies on proper spacing and alignment to group statements. In this chapter, we’ll explore Python’s indentation rules, comments, and line continuation, along with examples, exercises, and practical work (TP) to help you master these concepts.
Why Indentation Matters in Python
In many programming languages like C, Java, or JavaScript, curly braces {}
are used to define blocks of code. However, Python takes a different approach. It uses indentation (spaces or tabs) to group statements logically. This makes Python code visually clean and easy to read.
Example of Indentation
if True:
print('This is indented properly') # This code is indented
In the example above:
- The
if
statement is followed by a colon:
. - The
print()
statement is indented with 4 spaces (or a tab) to indicate that it belongs to theif
block. - If the indentation is incorrect, Python will throw an
IndentationError
.
Comments in Python
Comments are essential for explaining your code. Python supports two types of comments:
- Single-line comments: Use the
#
symbol. - Multi-line comments: Use triple quotes
'''
or"""
.
Example of Comments
# This is a single-line comment
"""
This is a multi-line comment.
It can span across multiple lines.
"""
Line Continuation in Python
Sometimes, a single line of code can become too long. Python allows you to break it into multiple lines using the backslash \
for line continuation.
Example of Line Continuation
total = 1 + 2 + 3 + \
4 + 5 + 6
print(total) # Output: 21
Here, the backslash \
tells Python that the statement continues on the next line.
Exercises to Practice
Now that you’ve learned the basics, let’s test your understanding with some exercises.
Exercise 1: Fix the Indentation
The following code has incorrect indentation. Fix it so it runs without errors.
if 5 > 2:
print('5 is greater than 2')
Exercise 2: Add Comments
Add a single-line comment and a multi-line comment to the code below.
x = 10
y = 20
sum = x + y
print(sum)
Exercise 3: Line Continuation
Rewrite the following code using line continuation to make it more readable.
result = 10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 + 100
print(result)
TP (Practical Work)
To solidify your understanding, here are some practical tasks to complete. These tasks will help you apply the concepts of indentation, comments, and line continuation in real-world scenarios.
TP 1: Create a Simple Calculator
Write a Python program that performs basic arithmetic operations (addition, subtraction, multiplication, and division) using proper indentation and comments to explain each step.
# Example structure
num1 = 10
num2 = 5
# Addition
sum = num1 + num2
print("Sum:", sum)
# Subtraction
difference = num1 - num2
print("Difference:", difference)
# Add more operations here...
TP 2: Fix and Improve Code
The following code is poorly formatted and lacks comments. Fix the indentation, add comments, and use line continuation where necessary.
if 10 > 5:
print('10 is greater than 5')
else:
print('5 is greater than 10')
total = 10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 + 100
print(total)
TP 3: Write a Program with Nested Conditions
Write a Python program that uses nested if
statements to check multiple conditions. Ensure proper indentation and add comments to explain the logic.
# Example structure
age = 18
has_license = True
if age >= 18:
if has_license:
print("You are eligible to drive.")
else:
print("You need a license to drive.")
else:
print("You are too young to drive.")
Conclusion
Understanding Python’s indentation, comments, and line continuation is crucial for writing clean and error-free code. By mastering these basics and completing the exercises and practical work (TP), you’ll be well on your way to becoming a proficient Python programmer. Practice the exercises provided, and don’t forget to experiment with your own code!
If you found this post helpful, share it with your friends and leave a comment below. Happy coding!