PyLearn
Lesson 04 · Foundations

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.

30–40 minutes Beginner Core Concept
Reading progress 0%

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.
01

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.

Important idea

Syntax errors are not something to fear. They are messages telling you that Python could not understand the structure of your code.

02

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.

03

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:

Result
15

Expressions inside print()

print(10 + 5)
Output
15

Python evaluates the expression and then passes its result to print().

Remember

Expressions produce values. Statements perform actions or control what the program does.

04

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.

Standard practice

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.

05

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.

Watch your capitalization

Many beginner errors come from typing a variable name with different capitalization from the original name.

06

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.

07

Comments

Comments are notes written for programmers. Python ignores comments when executing the program.

# Display a welcome message
print("Welcome to PyLearn")

Inline comments

age = 25  # Student age

Comments are useful for explaining non-obvious decisions and documenting important parts of a program.

Good practice

Write comments that explain why something is being done when the reason is not obvious from the code.

08

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.

09

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)
Output
15

Operators will be explored in detail in separate lessons later in the course.

10

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.

Readability matters

Python is designed to emphasize readable code. Clean formatting helps you and other developers understand a program faster.

11

Line Continuation

Python generally allows expressions inside parentheses, brackets or braces to span multiple lines.

Using parentheses

total = (
    10 +
    20 +
    30
)

print(total)
Output
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.

12

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)
Debugging mindset

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.

Open Practice Lab