Tutorials › Python Tutorial › Python Syntax and Comments

Python Syntax and Comments

10 min read · Lesson 3 of 24

Syntax is the set of rules that decides how code must be written so the computer understands it. Python's syntax is famously clean, but it has one rule that surprises newcomers: indentation matters.

One statement per line

In Python, each line is normally one instruction (a statement). You do not need a semicolon at the end.

Example PYTHON
school = "Maseno School"
students = 1200
print(school)
print(students)
Result
Maseno School
1200
Try it Yourself »

Python runs the lines from top to bottom, one after the other.

Indentation

Many languages use curly braces { } to group lines together. Python uses indentation – the spaces at the start of a line. Lines that belong to the same block must be indented by the same amount.

Example PYTHON
marks = 78
if marks >= 50:
    print("Pass")
    print("Well done, Achieng!")
print("Results printed.")
Result
Pass
Well done, Achieng!
Results printed.
Try it Yourself »

The two indented print() lines belong to the if statement, so they only run when the condition is true. The last line is not indented, so it always runs. You will learn about if properly in Python If...Else.

The standard is four spaces per level. Code editors insert them automatically when you press Tab.

Warning: Mixing different amounts of indentation in one block causes an IndentationError, and the program will not run at all. Pick four spaces and stick to it.

Here is what broken indentation looks like (display only – it will not run):

if marks >= 50:
    print("Pass")
      print("Well done!")   # IndentationError: unexpected indent

Case sensitivity

Python treats uppercase and lowercase letters as different. name, Name and NAME are three separate variables, and Print("hi") fails because the function is called print, all lowercase.

Comments

A comment is a note for humans. Python ignores it completely. Comments start with a # and run to the end of the line.

Example PYTHON
# Calculate the cost of a matatu trip for a group
fare = 100          # fare per person in KES
people = 5
print(fare * people)  # total cost
Result
500
Try it Yourself »

Good comments explain why the code does something, not just repeat what it obviously does.

Commenting out code

You can put # in front of a line to stop it from running without deleting it. This is useful while testing.

Example PYTHON
print("This line runs.")
# print("This line is switched off.")
print("So does this one.")
Result
This line runs.
So does this one.
Try it Yourself »

Multi-line notes

Python has no special multi-line comment symbol. You can either start each line with #, or use a string in triple quotes that is not assigned to anything. Python evaluates the string and throws it away.

Example PYTHON
"""
This program prints the capital cities
of three East African countries.
"""
print("Kenya: Nairobi")
print("Uganda: Kampala")
print("Tanzania: Dodoma")
Result
Kenya: Nairobi
Uganda: Kampala
Tanzania: Dodoma
Try it Yourself »
Tip: Triple-quoted strings at the top of a file or function are called docstrings. Tools use them to build documentation, which is why you will see them a lot in real projects.

Long lines

If a line gets too long, you can break it inside brackets. Python knows the statement continues until the bracket closes.

Example PYTHON
total = (1200 + 3500 +
         800 + 450)
print(total)
Result
5950
Try it Yourself »

Exercises

Exercise

Add the symbol that starts a comment.

 This is a comment
print("Hello")
Exercise

How many spaces is the standard indentation in Python? Fill in the number.

Standard indentation =  spaces

Next: store values in Python Variables.

Sign in or create a free account to track progress, earn XP, badges and a certificate.