def compute_area(width, height):
# This is the start of a function block
= width * height
area return area
# Calling the function
print(compute_area(5, 10)) # This line is not indented, showing it's not part of the function
50
Before we explore Python functions in the next section, it’s crucial to understand Python’s indentation rules, as they are fundamental to writing structured and error-free Python code.
Indentation is not just a matter of style in Python; it is syntactically significant. Python uses indentation to define blocks of code. The correct use of indentation is essential for your code to run as expected.
Key Points About Indentation:
Example code:
def compute_area(width, height):
# This is the start of a function block
= width * height
area return area
# Calling the function
print(compute_area(5, 10)) # This line is not indented, showing it's not part of the function
50
Explanation:
compute_area
starts with a colon (:
) and the subsequent code is indented four spaces.area
and the return
statement are part of the function due to their indentation.print
statement is not indented relative to the function definition, indicating it is outside the function.Certainly! Here’s an example that illustrates incorrect indentation and the potential issues it can cause. This example can be included in the introductory section on Python indentation to show the importance of following Python’s indentation rules accurately.
Example code with incorrect Indentation:
def calculate_percentage(total, amount):
if total > 0:
= (amount / total) * 100
percentage return percentage
else:
return 0
IndentationError: expected an indented block after 'if' statement on line 3
Explanation:
IndentationError
because the indentation levels are not consistent.percentage = (amount / total) * 100
is meant to be executed if the condition total > 0
is true. However, it’s not indented, making it unclear whether it’s supposed to be inside the if
statement or outside.return percentage
statement should be indented to align under the if
condition, indicating that it is part of the if
block. The else:
line is also not correctly aligned with the if
, causing a syntax error.Corrected Version:
def calculate_percentage(total, amount):
if total > 0:
= (amount / total) * 100
percentage return percentage
else:
return 0
What this corrects:
percentage = (amount / total) * 100
statement is now properly indented under the if
condition, clearly indicating it belongs to the block that executes when total > 0
.return percentage
is also indented correctly to show that it’s part of the if
condition. The else
block is aligned with the if
statement, maintaining consistent structure.This example highlights how improper indentation can lead to syntax errors and logical errors in Python. It’s vital to keep a consistent indentation level throughout the code block to ensure clarity and functionality of the code. By adhering to these practices, you prevent runtime errors and make your code easier to read and debug.