Welcome to Module 3 of Python Fundamentals Level 1 Professional! In the previous module, you learned how to install Python, use a code editor and write your first Python program.
Now, you will learn the basic rules Python follows when reading your instructions. These rules are called Python syntax. You will also learn how to write comments, use indentation and identify common syntax errors.
By the end of this module, you should be able to:
Syntax refers to the rules that explain how instructions must be written in a programming language.
Think about writing a sentence in English. You need to arrange words correctly so that other people can understand what you mean. Programming languages also have rules that must be followed.
Python syntax tells the computer how to read and understand the instructions you write.
print("Welcome to Python")
Output:
Welcome to Python
This instruction is written correctly. Python understands that it should display the words inside the quotation marks.
print("Welcome to Python)
This instruction is incorrect because the closing quotation mark is missing. Python will report a syntax error instead of displaying the message.
Remember: Python is very particular about how instructions are written. A small mistake can stop a program from running.
Every programming language has rules. Below are some important Python syntax rules you should understand.
Python instructions must be spelt correctly. For example, the function used to display information is written as print().
Correct:
print("Hello")
Incorrect:
pritn("Hello")
Parentheses are the round brackets used in Python. They are commonly used when calling functions.
Correct:
print("I am learning Python")
Incorrect:
print("I am learning Python"
The incorrect example is missing the closing parenthesis.
When writing text inside a Python instruction, you normally place the text inside quotation marks.
Correct:
print("My favourite subject is Mathematics")
Python accepts single quotation marks and double quotation marks for ordinary text strings.
print('Welcome to CRMNuggets')
print("Welcome to CRMNuggets")
Both instructions display the same message.
Python normally executes instructions from top to bottom. This means the order of your instructions can affect the result.
print("Good morning")
print("My name is Amina")
print("I am learning Python")
Output:
Good morning
My name is Amina
I am learning Python
Python is a case-sensitive programming language. This means it treats uppercase and lowercase letters as different.
For example, name, Name and NAME are treated as three different names.
name = "Tunde"
Name = "Amina"
print(name)
print(Name)
Output:
Tunde
Amina
Notice that changing the capital letter changes the name Python recognises.
The same rule applies to Python keywords and built-in functions. For example, print() is correct, while Print() is not the same instruction.
Indentation means adding spaces at the beginning of a line of code. Python uses indentation to organise instructions into groups.
Think of indentation like arranging paragraphs in a school essay. It helps people understand which instructions belong together.
In Python, indentation is not just for appearance. It is part of the language's syntax.
if 10 > 5:
print("10 is greater than 5")
Output:
10 is greater than 5
Notice that the print() instruction has spaces before it. These spaces show that the instruction belongs to the if statement.
Consider this incorrect example:
if 10 > 5:
print("10 is greater than 5")
Python will report an indentation error because the instruction that belongs inside the if statement has not been indented.
The recommended standard in Python is to use four spaces for each indentation level.
if 20 > 10:
print("The condition is true")
The print() instruction is indented by four spaces.
Most modern code editors can automatically insert indentation when you press the Enter key after certain Python instructions.
You should avoid mixing tabs and spaces in the same indentation structure because this can cause errors.
A comment is a note written inside a program to explain what a particular instruction or section of code does.
Comments help programmers understand their code. They are also useful when several people are working on the same project.
Python ignores comments when executing a program.
A single-line comment is a note written on one line. In Python, the hash symbol (#) is used to begin a comment.
# This program displays a welcome message
print("Welcome to Python")
Output:
Welcome to Python
Python ignores the comment and executes the print() instruction.
You can also write a comment after an instruction on the same line.
print("Hello") # Display a greeting
The computer displays Hello, while the comment is ignored.
A multi-line comment is used when you want to write a longer explanation that covers several lines.
Python does not have a special multi-line comment symbol. You can use the # symbol at the beginning of each line.
# This program welcomes students
# to the Python programming course.
# It is designed for beginners.
print("Welcome, students!")
You may also see triple-quoted strings used for longer descriptions and documentation. These are string literals, not technically comments, although they are often used as docstrings to explain functions, classes and modules.
"""
This program displays a welcome message.
It introduces students to Python.
"""
print("Welcome to Python")
Keywords are special words that Python has reserved for particular purposes. They are part of the language and have special meanings.
You should not use Python keywords as variable names or ordinary identifiers.
| Keyword | Purpose |
|---|---|
| if | Checks whether a condition is true. |
| else | Provides an alternative when a condition is false. |
| for | Repeats instructions over a sequence. |
| while | Repeats instructions while a condition is true. |
| def | Defines a function. |
| return | Sends a result back from a function. |
| True | Represents a true Boolean value. |
| False | Represents a false Boolean value. |
| import | Brings a module into a program. |
| class | Defines a class in object-oriented programming. |
You will learn more about many of these keywords in later modules.
An identifier is a name used to identify something in a Python program, such as a variable, function or class.
For example, a student record may contain a variable called student_name.
student_name = "Amina"
print(student_name)
Output:
Amina
In this example, student_name is an identifier because it is the name used for the variable.
student_name
age
total_score
school2026
first_name
2student # Starts with a number
student name # Contains a space
class # Uses a Python keyword
Writing code that works is important, but writing code that other people can understand is also important.
Clean code is code that is organised, readable and easy to maintain. Python programmers follow common style guidelines to make their programs easier to understand.
x="Tunde"
y=13
print(x)
print(y)
student_name = "Tunde"
student_age = 13
print(student_name)
print(student_age)
Both programs produce the same information, but the second example uses meaningful names and spacing that make it easier to understand.
A syntax error happens when Python cannot understand an instruction because it does not follow the language's rules.
Here are some common examples:
Incorrect:
print("Welcome to Python)
Correct:
print("Welcome to Python")
Incorrect:
print("Hello"
Correct:
print("Hello")
Incorrect:
if 5 > 2:
print("Five is greater")
Correct:
if 5 > 2:
print("Five is greater")
Incorrect:
pritn("Hello")
Correct:
print("Hello")
Incorrect:
Print("Welcome")
Correct:
print("Welcome")
Imagine you are developing a simple student profile program for a school in Ibadan, Nigeria.
Create a Python program that:
# Student profile program
student_name = "Amina"
student_age = 13
school_name = "Future Leaders School"
favourite_subject = "Computer Studies"
# Display student information
print("STUDENT PROFILE")
print(student_name)
print(student_age)
print(school_name)
print(favourite_subject)
Expected output:
STUDENT PROFILE
Amina
13
Future Leaders School
Computer Studies
Your school wants a simple program that displays a student's examination information.
# Student examination result program
student_name = "Tunde"
examination_score = 85
# Display the student's result
print("Student Name:")
print(student_name)
print("Examination Score:")
print(examination_score)
Expected output:
Student Name:
Tunde
Examination Score:
85
| Term | Meaning |
|---|---|
| Syntax | The rules for writing instructions in a programming language. |
| Indentation | Spaces at the beginning of a line of code used to organise instructions. |
| Comment | A note in a program that Python ignores during execution. |
| Keyword | A reserved word with a special meaning in Python. |
| Identifier | A name used for variables, functions and other program elements. |
| Case Sensitive | A language feature that treats uppercase and lowercase letters differently. |
| Syntax Error | An error caused by code that does not follow Python's syntax rules. |
| Clean Code | Code that is organised and easy to read and understand. |
In this module, you have learned that:
Answer the following questions to test your understanding.
Congratulations! You have completed Module 3 of Python Fundamentals Level 1 Professional.
In Module 4: Python Variables and Data Types, you will learn how to store information in Python, create variables, work with numbers and text, and understand the different types of data used in programming.
Keep learning, keep practising and keep writing code. Every line you write takes you one step closer to becoming a programmer!