6 Python Syntax
- Understand what syntax means in programming
- Learn how to store and display text using variables and the
print()function - Follow standard Python variable naming guidelines
- Learn how Python uses indentation and whitespace
- Write clear comments to explain your code
6.1 What is the Syntax of a Programming Language?
The syntax of a programming language is the set of rules that defines how programs must be written. Just like human languages have grammar and punctuation rules so we can understand one another, programming languages have syntax rules so the computer can understand our instructions.
If you break a syntax rule in Python, the computer will not try to guess what you meant—it will simply raise a SyntaxError and stop running.
6.2 Assigning Variables
A variable is a named reference used to store a piece of information in a program so you can access and use it later.
In Python, creating (or initialising) a variable is straightforward:
- You choose a name for your variable.
- You use the equals sign (
=) to assign a value to it. - Python automatically works out what kind of data you are storing (such as text wrapped in quotation marks, known as a string).
researcher_name = "Kavi"
project_title = "Genome Analysis"
print(researcher_name)
print(project_title)6.3 Variable Naming Conventions — Making Your Life Easy!
One of the hardest parts of reading someone else’s code is dealing with variable names that make no sense to anyone except the original author.
When naming variables, follow Python’s guidelines (from the official style guide known as PEP 8) for consistency and readability:
- Use lowercase with underscores (snake_case): Separate words with underscores (e.g.
sample_id,species_name). - Case-sensitive:
sample,Sample, andSAMPLEare three completely different variables to Python. - Do not start with numbers: Variable names cannot begin with a digit (e.g.
1st_sampleis invalid, butsample_1is fine). - Stick to letters, numbers, and underscores: Do not use special characters, spaces, quotation marks, or brackets in the variable name itself.
- Be descriptive: Choose clear, meaningful names (
lab_locationis helpful;x12is not). - Avoid overly long names:
sample_typeis ideal, whereasthe_type_of_sample_we_collected_from_the_field_todayis unnecessarily long. - Do not use Python keywords: Avoid using built-in command names like
printas variable names, as this will overwrite the built-in function.
6.4 Indentation
In Python, indentation (leading spaces at the start of a line) is used to define code blocks. We will see more of this later in this course when we look at conditional statements and loops.
Because Python relies on indentation to group instructions, putting an unexpected space at the start of any line will trigger an IndentationError. This happens frequently across multi-line code—even when using simple print() statements!
6.4.1 Multi-Line Indentation Errors with print()
Consider these two examples where multiple print() statements are run in sequence:
print("Step 1: Sample logged")
print("Step 2: Buffer added")
print("Step 3: Centrifuge complete")print("Step 1: Sample logged")
print("Step 2: Buffer added")
print("Step 3: Centrifuge complete")In the second example, an extra space at the beginning of Line 2 causes an IndentationError immediately.
Why does this happen?
In Python, you cannot indent a line unless it follows a specific statement that creates a new code block (like an if statement or a loop, which we will cover later). Because Line 1 is not starting a new block, Python does not expect Line 2 to be indented and immediately halts execution.
6.5 Printing Multiple Items and Splitting Lines
You can print multiple items at once by passing them into print(), separated by commas. Python will automatically put a space between each item:
lead_scientist = "Dr Aris Thorne"
facility = "Central Lab"
print("Project Lead:", lead_scientist, "at", facility)Output:
Project Lead: Dr Aris Thorne at Central Lab
If your print() statement becomes long, Python allows you to split it across multiple lines inside the round brackets (). This implicit line continuation makes your code much neater and easier to read:
print("Project update:", project_title, "is currently managed by", lead_scientist, "in", facility)
print(
"Project update:",
project_title,
"is currently managed by",
lead_scientist,
"in",
facility
)Notice how indenting the items on new lines inside the brackets keeps the code organised. Because these lines sit inside round brackets (), Python knows they are part of the same print() instruction and will not raise an indentation error!
6.6 Commenting Your Code
Comments are notes written directly in your script to explain what the code is doing. Python ignores comments completely—they produce no output and exist solely to help humans read the code.
# Store the active status of the ongoing trial
experiment_stage = "Data Collection"
print(experiment_stage) # You can also place comments at the end of a code line
"""
This is a multi-line comment (using triple quotes).
It allows you to write longer notes across several lines
without needing a hash symbol on every single line.
"""6.7 Exercises
6.8 Summary
- Syntax: The official rules of how code must be written for Python to understand it.
- Variables: Stored values assigned using
=. Use descriptive snake_case names. - Indentation: Extra spaces at the start of a line matter in Python. Unintended leading spaces across sequential statements trigger an
IndentationError. - Multiple items in print(): Separate variables and text strings with commas. Split long
print()statements across multiple lines inside the brackets to keep code clean. - Comments (
#): Notes ignored by Python that describe what your code is doing for human readers.