Python Syntax
Every programming language has rules that determine how code must be written. These rules are called syntax. In this lesson, you will learn the fundamental syntax rules of Python and understand why clean, correctly structured code matters.
Learning objectives
By the end of this lesson, you should be able to:
- Explain what programming syntax means.
- Identify Python statements and expressions.
- Understand why indentation matters in Python.
- Explain Python's case-sensitive nature.
- Follow Python naming conventions and rules.
- Use comments correctly.
- Recognize common syntax errors.
- Write cleaner and more reliable Python code.
What Is Python Syntax?
Syntax means the set of rules that determines how code must be written so that a programming language can understand it.
Human languages have grammar. Programming languages have syntax.
| Human Language | Programming Language |
|---|---|
| Grammar | Syntax |
| Sentence | Statement |
| Meaning | Program behavior |
A simple example
This is valid Python:
print("Hello")
But this is not valid Python:
print("Hello"
The closing parenthesis is missing. Python therefore cannot correctly interpret the statement.
Syntax errors are not something to fear. They are messages telling you that Python could not understand the structure of your code.
Python Statements
A statement is an instruction that Python can execute.
For example:
print("Hello")
print("Welcome")
print("Learning Python")
This program contains three statements. Python processes them as part of executing the program.
Assignment statements
Python can also use statements to assign values to variables.
name = "Adnan"
age = 25
We will study variables in detail in the next few lessons.
Expressions
An expression is a piece of code that Python can evaluate to produce a value.
Arithmetic expression
10 + 5
The result of this expression is:
15
Expressions inside print()
print(10 + 5)
15
Python evaluates the expression and then
passes its result to print().
Expressions produce values. Statements perform actions or control what the program does.
Indentation in Python
One of Python's most important syntax rules is indentation.
Indentation means adding spaces at the beginning of a line. Python uses indentation to define blocks of code.
Example
if True:
print("Python is running")
The indented print() statement
belongs to the if block.
Incorrect indentation
if True:
print("Python is running")
This produces a syntax error because Python expects an indented block after the colon.
Four spaces are the standard indentation convention in Python. Most modern editors can automatically insert the correct indentation.
Indentation will become especially important when we study conditions, loops, functions and classes.
Python Is Case-Sensitive
Python distinguishes between uppercase and lowercase letters.
Therefore, these names are different:
name
Name
NAME
Example
name = "Adnan"
print(name)
But this is a different variable:
Name = "Adnan"
print(name)
If name has not previously been
defined, Python will not treat Name
as the same thing.
Many beginner errors come from typing a variable name with different capitalization from the original name.
Python Naming Rules
Python allows you to create names for variables, functions, classes and other objects. These names are called identifiers.
Valid names
name = "Adnan"
age = 25
student_name = "Ali"
total2 = 100
Basic naming rules
- A name can contain letters.
- A name can contain numbers.
- A name can contain underscores.
- A name cannot begin with a number.
- Names are case-sensitive.
- Python keywords cannot be used as ordinary identifiers.
Invalid examples
2name = "Ali"
student-name = "Ali"
class = "Python"
Better naming
Choose names that clearly communicate what the value represents.
student_name = "Ali"
student_age = 20
total_marks = 450
Clear names make programs easier to read, understand and maintain.
Strings and Quotation Marks
Strings are sequences of text. Python allows strings to be written using single or double quotation marks.
message = "Hello Python"
name = 'Adnan'
print(message)
print(name)
Matching quotation marks
The opening and closing quotation marks should match.
message = "Hello"
Incorrect quotation marks can cause a syntax error.
Apostrophes inside strings
You can choose the opposite quote type when a string contains an apostrophe.
message = "Python's syntax is readable"
print(message)
Strings will be covered much more deeply in the dedicated Strings lessons later in PyLearn.
Operators in Python
Operators are symbols or keywords used to perform operations on values.
Arithmetic operators
| Operator | Meaning | Example |
|---|---|---|
| + | Addition |
10 + 5
|
| - | Subtraction |
10 - 5
|
| * | Multiplication |
10 * 5
|
| / | Division |
10 / 5
|
Example
total = 10 + 5
print(total)
15
Operators will be explored in detail in separate lessons later in the course.
Multiple Statements
Python programs normally contain multiple statements written on separate lines.
name = "Adnan"
age = 25
print(name)
print(age)
Each statement is easy to identify because it occupies its own line.
Semicolons
Python can technically place multiple statements on one line using a semicolon.
x = 10; y = 20; print(x + y)
However, this is generally less readable. Prefer separate lines in normal Python code.
Python is designed to emphasize readable code. Clean formatting helps you and other developers understand a program faster.
Line Continuation
Python generally allows expressions inside parentheses, brackets or braces to span multiple lines.
Using parentheses
total = (
10 +
20 +
30
)
print(total)
60
Explicit line continuation
A backslash can also be used to continue an expression.
total = 10 + \
20 + \
30
print(total)
In modern Python code, grouping expressions with parentheses is generally preferred because it is clearer and less error-prone.
Common Syntax Errors
Let's look at some errors you are likely to encounter while learning Python.
1. Missing closing parenthesis
print("Hello"
Correct:
print("Hello")
2. Incorrect indentation
if True:
print("Hello")
3. Invalid variable name
2student = "Ali"
4. Incorrect capitalization
Name = "Ali"
print(name)
5. Unclosed string
print("Hello)
When Python reports an error, read the error message carefully. The line mentioned by Python is often the best place to start investigating.
You've reached the end of this lesson.
Review the examples, open the Practice Lab, and mark this lesson complete when you're ready.
Comments
Comments are notes written for programmers. Python ignores comments when executing the program.
Inline comments
Comments are useful for explaining non-obvious decisions and documenting important parts of a program.
Write comments that explain why something is being done when the reason is not obvious from the code.