Lesson 8 of 70 – Python Syntax
11.2%

Python Syntax

Python syntax refers to the rules that define how Python programs are written. Python has a simple and readable syntax that makes programs easier to understand and maintain.

Note: Python uses indentation to define blocks of code. Proper indentation is an important part of Python syntax.
What is Python Syntax?

Syntax is the set of rules that determines how instructions must be written in a programming language.

For example, a simple Python statement is:

print("Hello Python")

This statement follows Python's syntax rules and displays a message on the screen.

Python Statements

A statement is an instruction that Python can execute.

Example:
name = "Rahul"
age = 20
print(name)
print(age)

Each line contains a Python statement.

Indentation

Indentation means the spaces at the beginning of a line. Python uses indentation to define blocks of code.

Example:
age = 20

if age >= 18:
    print("You are an adult")
Output:
You are an adult

The print() statement is indented because it belongs to the if block.

Importance of Indentation

Indentation is mandatory when Python expects a block of code. Incorrect indentation can cause an IndentationError or change the meaning of the program.

Correct:
if 10 > 5:
    print("10 is greater")
Incorrect:
if 10 > 5:
print("10 is greater")

A common convention is to use 4 spaces for each indentation level.

Python is Case-Sensitive

Python is a case-sensitive programming language. This means uppercase and lowercase letters are treated differently.

Example:
name = "Rahul"
Name = "Amit"

Here, name and Name are two different variable names.

Remember: print, Print, and PRINT are not the same in Python.
Comments

Comments are notes written inside a program for developers. Python ignores comments while executing the program.

Single-Line Comment:
# This is a comment
print("Hello Python")

A single-line comment begins with the # symbol.

Multi-Line Comments

Python does not have a dedicated multi-line comment syntax. Developers commonly use multiple # lines for comments.

Example:
# This is the first comment
# This is the second comment
# This is the third comment

Triple-quoted strings can also span multiple lines, but they are technically string literals rather than a special multi-line comment syntax.

Variables and Assignment

Python variables are created when a value is assigned to a name.

Example:
name = "Rahul"
age = 20
salary = 25000

Python does not require you to declare the variable type separately before assigning a value.

Multiple Statements

Normally, each Python statement is written on a separate line.

Example:
x = 10
y = 20
z = x + y
print(z)
Output:
30

Writing statements on separate lines generally makes programs easier to read.

Multiple Statements on One Line

Python allows multiple simple statements on one line using a semicolon, although this is generally less readable.

Example:
x = 10; y = 20; print(x + y)
Output:
30
Best Practice: Prefer writing separate statements on separate lines for better readability.
Line Continuation

A long expression can be split across multiple lines. Parentheses, brackets, and braces allow expressions to continue naturally across lines.

Example:
total = (10 + 20 + 30 +
        40 + 50)

Python also supports an explicit line-continuation character, but using parentheses is usually clearer.

Blocks of Code

A block of code is a group of statements that belong together. Python uses indentation to define blocks.

Example:
if age >= 18:
    print("Adult")
    print("Eligible to vote")

Both print() statements belong to the if block because they have the same indentation level.

Colon in Python

A colon : is used after statements that introduce a new block of code, such as if, for, while, functions, and classes.

Example:
if 10 > 5:
    print("True")

The colon tells Python that an indented block follows.

Python Naming Rules

Python identifiers such as variable, function, and class names follow certain rules.

  • A name can contain letters, digits, and underscores.
  • A name cannot start with a digit.
  • Names are case-sensitive.
  • Python keywords cannot be used as ordinary identifiers.
  • Spaces are not allowed inside names.
Valid Examples:
student_name
age
total_marks
student1
Invalid Examples:
1student
student name
class
Python Keywords

Keywords are reserved words that have special meanings in Python. They cannot normally be used as variable or function names.

Examples include:

if     else     elif     for
while     class     def     return
import     try     except     True
False     None     and     or
Whitespace

Whitespace refers to spaces, tabs, and line breaks in source code. Python uses indentation whitespace to define code blocks.

Extra spaces inside expressions are generally ignored, but indentation at the beginning of a line can have syntactic meaning.

Example:
x = 10
y = 20

The spaces around the = operator are optional but recommended because they improve readability.

Python Syntax Errors

A syntax error occurs when Python cannot understand the structure of the code according to its grammar rules.

Example:
if 10 > 5
    print("Hello")

The colon after the condition is missing, so Python reports a syntax error.

Correct:
if 10 > 5:
    print("Hello")
Good Python Syntax Practices
  • Use meaningful variable and function names.
  • Use 4 spaces for indentation.
  • Keep related code together.
  • Write comments where they improve understanding.
  • Keep lines reasonably readable.
  • Use spaces around operators where appropriate.
  • Follow consistent naming conventions.
  • Avoid unnecessary semicolons.
  • Use blank lines to separate logical sections.
Key Points
  • Python syntax defines the rules for writing Python programs.
  • Python uses indentation to define code blocks.
  • Four spaces are commonly used for one indentation level.
  • Python is case-sensitive.
  • Comments begin with the # symbol.
  • A colon is used to introduce many code blocks.
  • Python variables are created through assignment.
  • Keywords have special meanings and cannot normally be used as identifiers.
  • Python names cannot start with a digit.
  • Correct syntax is required for Python programs to execute successfully.

🧠 Quick Quiz

What does Python use to define blocks of code?