Syntax
The basic rules that define correctly structured Python code.
Syntax
name = valueSyntax is the set of rules for how a program is written. A Python program is a list of instructions called statements, usually one per line.
Key facts
- Python is case-sensitive:
nameandNameare different. - Statements normally end at the end of the line — no semicolons needed.
- Indentation (spaces at the start of a line) is meaningful and defines blocks.
Example
price = 10
quantity = 3
total = price * quantity
print(total)
# Output:
# 30When to use it
- A developer names variables with snake_case to follow PEP 8 and keep the codebase consistent.
- A code reviewer rejects a submission because a misspelled variable name causes a silent logic bug.
- A beginner fixes an error after learning that Python treats 'Total' and 'total' as different names.
More examples
Case-sensitive variable names
Shows that Python distinguishes names by case, so score and Score are separate variables.
score = 95
Score = 100 # different variable
print(score) # 95
print(Score) # 100Multiple statements, one per line
Illustrates the convention of one statement per line with no semicolons required.
item = 'book'
price = 12.99
discount = 0.1
final = price * (1 - discount)
print(item, final)
# Output:
# book 11.691snake_case naming convention
Demonstrates PEP 8 snake_case naming for variables, the standard Python style.
first_name = 'Grace'
last_name = 'Hopper'
full_name = first_name + ' ' + last_name
print(full_name)
# Output:
# Grace Hopper
Discussion