Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† PYTHON FUNDAMENTALS LEVEL 1 PROFESSIONAL ยท Lesson 4 of 9

Module Three

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
4
PYTHON FUNDAMENTALS LEVEL 1 PROFESSIONAL Module 3: Python Syntax, Comments and Indentation

Module 3: Python Syntax, Comments and Indentation

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.

Learning Objectives

By the end of this module, you should be able to:

  • Explain what Python syntax means.
  • Identify the basic rules for writing Python code.
  • Understand how Python uses indentation.
  • Write single-line and multi-line comments.
  • Understand the importance of quotation marks and parentheses.
  • Recognise Python keywords and identifiers.
  • Identify and correct common syntax errors.
  • Write simple, clean and readable Python programs.

1. What Is Python Syntax?

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.

Example of Correct Python Syntax

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.

Example of Incorrect Python Syntax

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.

2. Basic Rules of Python Syntax

Every programming language has rules. Below are some important Python syntax rules you should understand.

2.1 Use Correct Spelling

Python instructions must be spelt correctly. For example, the function used to display information is written as print().

Correct:

print("Hello")

Incorrect:

pritn("Hello")

2.2 Use Parentheses Correctly

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.

2.3 Use Quotation Marks for Text

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.

2.4 Follow the Correct Order

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

3. Understanding Python Case Sensitivity

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.

Example

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.

4. What Is Indentation in Python?

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.

Example of Indentation

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.

What Happens When Indentation Is Missing?

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.

5. How Many Spaces Should You Use?

The recommended standard in Python is to use four spaces for each indentation level.

Example

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.

6. What Are Python Comments?

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.

6.1 Single-Line Comments

A single-line comment is a note written on one line. In Python, the hash symbol (#) is used to begin a comment.

Example

# This program displays a welcome message
print("Welcome to Python")

Output:

Welcome to Python

Python ignores the comment and executes the print() instruction.

6.2 Comments Beside Instructions

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.

6.3 Multi-Line Comments

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")

7. What Are Python Keywords?

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.

Examples of Python Keywords

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.

8. What Are Identifiers?

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.

Rules for Naming Identifiers

  • An identifier can contain letters, numbers and underscores.
  • It must not begin with a number.
  • It must not contain spaces.
  • It must not be a reserved Python keyword.
  • Identifiers are case-sensitive.

Examples of Valid Identifiers

student_name
age
total_score
school2026
first_name

Examples of Invalid Identifiers

2student       # Starts with a number
student name   # Contains a space
class          # Uses a Python keyword

9. Writing Clean and Readable Python Code

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.

Good Programming Practices

  • Use meaningful names for variables and functions.
  • Use four spaces for indentation.
  • Add comments when they help explain your code.
  • Keep instructions organised.
  • Avoid unnecessary complicated code.
  • Use consistent naming styles.

Example of Poorly Organised Code

x="Tunde"
y=13
print(x)
print(y)

Example of More Readable Code

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.

10. Common Python Syntax Errors

A syntax error happens when Python cannot understand an instruction because it does not follow the language's rules.

Here are some common examples:

10.1 Missing Quotation Marks

Incorrect:

print("Welcome to Python)

Correct:

print("Welcome to Python")

10.2 Missing Parentheses

Incorrect:

print("Hello"

Correct:

print("Hello")

10.3 Incorrect Indentation

Incorrect:

if 5 > 2:
print("Five is greater")

Correct:

if 5 > 2:
    print("Five is greater")

10.4 Incorrect Spelling

Incorrect:

pritn("Hello")

Correct:

print("Hello")

10.5 Using the Wrong Capitalisation

Incorrect:

Print("Welcome")

Correct:

print("Welcome")

11. Practical Activity: Create a Well-Organised Student Profile

Imagine you are developing a simple student profile program for a school in Ibadan, Nigeria.

Your Task

Create a Python program that:

  1. Displays the student's name.
  2. Displays the student's age.
  3. Displays the school name.
  4. Displays the student's favourite subject.
  5. Uses comments to explain the program.
  6. Uses clear and meaningful names.

Sample Solution

# 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

12. Mini Project: Create a School Result Display Program

Your school wants a simple program that displays a student's examination information.

Project Requirements

  • Use a meaningful variable name for the student's name.
  • Use a meaningful variable name for the examination score.
  • Add a comment explaining the program.
  • Display the student's name and score.
  • Arrange your code neatly using proper spacing.

Sample Project Code

# 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

13. Important Terms to Remember

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.

14. Module Summary

In this module, you have learned that:

  • Python syntax refers to the rules for writing Python instructions.
  • Python is case-sensitive.
  • Quotation marks are used to represent text strings.
  • Parentheses are important when calling functions such as print().
  • Indentation organises instructions into blocks.
  • Four spaces are the recommended standard for each indentation level.
  • Comments begin with the # symbol for single-line comments.
  • Keywords are reserved words in Python.
  • Identifiers are names given to program elements.
  • Clean code makes programs easier to understand and maintain.
  • Syntax errors can be corrected by checking spelling, punctuation and indentation.

15. Knowledge Check

Answer the following questions to test your understanding.

  1. What does Python syntax mean?
    • A. Rules for writing Python instructions
    • B. A computer keyboard
    • C. A type of monitor
    • D. A web browser
  2. What does indentation mean?
    • A. Deleting a program
    • B. Adding spaces at the beginning of a line of code
    • C. Closing a computer
    • D. Changing the screen brightness
  3. What is the recommended number of spaces for each indentation level in Python?
    • A. One space
    • B. Two spaces
    • C. Four spaces
    • D. Ten spaces
  4. Which symbol is used to begin a single-line comment in Python?
    • A. @
    • B. #
    • C. $
    • D. %
  5. What does case-sensitive mean?
    • A. Python ignores all letters
    • B. Python treats uppercase and lowercase letters differently
    • C. Python only accepts numbers
    • D. Python does not allow text
  6. Which of these is a Python keyword?
    • A. student_name
    • B. school
    • C. if
    • D. total_score
  7. Which of these is a valid identifier?
    • A. 2student
    • B. student name
    • C. student_name
    • D. class
  8. What happens when Python encounters a syntax error?
    • A. It automatically repairs every error
    • B. It may stop executing the affected code and report an error
    • C. It shuts down the computer
    • D. It deletes the program
  9. Which of these is a correctly written Python instruction?
    • A. print("Hello")
    • B. Print("Hello")
    • C. print("Hello"
    • D. pritn("Hello")
  10. Why are comments useful in Python?
    • A. They make the computer faster
    • B. They explain parts of the code
    • C. They delete errors automatically
    • D. They replace all instructions

Knowledge Check Answers

Click here to reveal the correct answers
  1. A โ€” Rules for writing Python instructions.
  2. B โ€” Adding spaces at the beginning of a line of code.
  3. C โ€” Four spaces.
  4. B โ€” #.
  5. B โ€” Python treats uppercase and lowercase letters differently.
  6. C โ€” if.
  7. C โ€” student_name.
  8. B โ€” It may stop executing the affected code and report an error.
  9. A โ€” print("Hello").
  10. B โ€” They explain parts of the code.

16. What Is Next?

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!

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’