“`
Python is a versatile and powerful programming language that emphasizes readability and simplicity. In this chapter, we will explore the basic syntax and semantics of Python, focusing on indentation, comments, and line continuation. By the end of this chapter, you will understand how to structure your Python code properly.
Python Indentation
Unlike many other programming languages that use braces {}
to define code blocks, Python relies on indentation. This means that the grouping of your code depends on how it is indented. Proper indentation is not optional in Python—it is a requirement for the code to execute.
Example: Proper Indentation
if True:
print('This is indented properly')
print('Another indented line')
```
“`
In the example above, the lines following the if
statement are indented, making them part of the same block. If you fail to indent correctly, Python will raise an error.
Incorrect Indentation Example
if True:
```
print(‘This will cause an error’) # Missing indentation
“`
Error: IndentationError: expected an indented block
Tips for Proper Indentation
- Use 4 spaces per level of indentation (this is the Python convention).
- Avoid mixing spaces and tabs.
- Many code editors, such as VS Code and PyCharm, can help you format your code correctly.
Comments in Python
Comments are essential for making your code readable and maintainable. Python supports two types of comments:
Single-line Comments
Single-line comments begin with a #
symbol. Everything after the #
on the same line is ignored by Python.
Example:
# This is a single-line comment
```
print(‘Hello, World!’) # This comment explains the line
“`
Multi-line Comments
Python does not have a specific syntax for multi-line comments. Instead, you can use a series of single-line comments or a string literal (usually triple quotes) that is not assigned to a variable.
Example 1: Using multiple #
# This is a comment
```
# spanning multiple lines
# to explain the code
“`
Example 2: Using a string literal
"""
```
This is a multi-line comment. It is often used for documentation. “””
“`
Line Continuation in Python
In Python, you can split long lines of code into multiple lines using a backslash \
. This is especially useful for improving readability.
Example:
total = 1 + 2 + 3 + \
4 + 5 + 6
```
print(total) # Output: 21
“`
Alternatively, you can use parentheses for implicit line continuation. This is often preferred because it eliminates the need for a backslash.
Example:
total = (1 + 2 + 3 +
4 + 5 + 6)
```
print(total) # Output: 21
“`
Exercises
To solidify your understanding of Python’s basic syntax and semantics, try the following exercises:
Exercise 1: Fix the Indentation Error
The following code has an indentation error. Fix it to make it work:
if 5 > 3:
```
print(‘Five is greater than three!’)
“`
Exercise 2: Add Comments
Write a Python script with at least three single-line comments explaining what the code does. For example:
# This program calculates the sum of two numbers
```
number1 = 10 number2 = 20
# Adding the two numbers
sum = number1 + number2 print(sum) # Displaying the result
“`
Exercise 3: Use Line Continuation
Rewrite the following code using both backslash \
and parentheses:
result = 10 + 20 + 30 + 40 + 50 + 60
```
“`
With this foundational knowledge, you are now ready to explore more advanced Python topics. Remember, good coding practices such as proper indentation and clear comments will make your code easier to understand and maintain.
“`