Python syntax is designed to be readable and clean, looking very similar to the English language.
In most programming languages, curly braces {} or keywords are used to define a block of code. In Python, block definition is done using indentation (whitespace at the beginning of a line).
- You must use the same number of spaces in the same block of code, otherwise Python will throw an
IndentationError. - The standard practice is to use 4 spaces for indentation.
if 5 > 2:
print("Five is greater than two!")In Python, a new line signifies the end of a statement. However, if a statement is long, it can be split across multiple lines.
You can split statements inside parentheses (), brackets [], or braces {} without using any continuation characters. This is the preferred style in Python.
# Preferred: implicit continuation inside parentheses
total = (1 +
2 +
3)
print(total) # Output: 6You can write a single statement across multiple lines using the backslash (\) continuation character.
total = 1 + \
2 + \
3
print(total) # Output: 6The pass statement is a null operation. It does nothing when executed. It is used as a placeholder when a statement is syntactically required but you do not want to run any code yet.
# Placeholder for a function to be implemented later
def future_function():
passUnlike languages such as C++ or Java, Python does not require a semicolon (;) at the end of statements. Semicolons are optional and can be used to write multiple statements on a single line, though this is generally discouraged for readability.
print("Hello"); print("World")PEP 8 is the official style guide for writing Python code. It provides guidelines on formatting code to make it as readable and consistent as possible. Following PEP 8 is highly recommended for all Python developers.