Python’s syntax is known for being clean, readable, and highly expressive. In this section, we will cover the fundamental rules you need to follow when writing Python code.
1. Indentation rules
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
Python uses indentation to indicate a block of code (like a loop, a function, or conditional statements).
Example of correct indentation:
if 5 > 2:
print("Five is greater than two!")
Example of a Syntax Error:
If you omit the indentation, Python will throw an error:
if 5 > 2:
print("Five is greater than two!") # IndentationError: expected an indented block
Important Rules:
- The number of spaces is up to you as a programmer, but it has to be at least one.
- Convention: The standard practice is to use 4 spaces for each indentation level.
- You have to use the same number of spaces in the same block of code, otherwise Python will give you an error.
# Correct
if 5 > 2:
print("Five is greater than two!")
print("This is also in the block.")
# Error
if 5 > 2:
print("Five is greater than two!")
print("This is also in the block.") # IndentationError
2. Comments (single-line, multi-line)
Comments are used to explain Python code, make the code more readable, and prevent execution when testing code.
Single-line Comments
Comments start with a #, and Python will ignore the rest of the line.
# This is a comment
print("Hello, World!") # This is a comment at the end of a line
Multi-line Comments
Python does not really have a distinct syntax for multi-line comments.
To add a multiline comment you could insert a # for each line:
# This is a comment
# written in
# more than just one line
print("Hello, World!")
Alternatively, you can use multi-line strings (triple quotes """ or '''). Since Python will ignore string literals that are not assigned to a variable, you can use them as a multi-line comment:
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
3. Statements and line continuation
Usually, a Python statement ends with a newline. However, Python allows you to write long statements over multiple lines using the line continuation character (\).
Explicit Line Continuation
total = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9
Implicit Line Continuation
Line continuation is implied inside parentheses (), brackets [], and braces {}. This is the preferred method over using the backslash.
# Implicit continuation (preferred)
total = (1 + 2 + 3 +
4 + 5 + 6 +
7 + 8 + 9)
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
4. Case sensitivity
Python is a case-sensitive language. This means that variables, functions, class names, and all other identifiers must be typed with the exact same capitalization.
For example, my_variable, My_variable, and MY_VARIABLE are treated as three completely different identifiers.
age = 25
Age = 30
AGE = 35
print(age) # Outputs: 25
print(Age) # Outputs: 30
Also, Python keywords are case-sensitive. You must use if, while, True, False, None exactly as they are defined. Typing IF or true will result in a syntax error.
Discussion
Loading comments...