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.
Syntax is the set of rules that determines how instructions must be written in a programming language.
For example, a simple Python statement is:
This statement follows Python's syntax rules and displays a message on the screen.
A statement is an instruction that Python can execute.
Each line contains a Python statement.
Indentation means the spaces at the beginning of a line. Python uses indentation to define blocks of code.
The print() statement is indented because it belongs to the if block.
Indentation is mandatory when Python expects a block of code. Incorrect indentation can cause an IndentationError or change the meaning of the program.
A common convention is to use 4 spaces for each indentation level.
Python is a case-sensitive programming language. This means uppercase and lowercase letters are treated differently.
Here, name and Name are two different variable names.
Comments are notes written inside a program for developers. Python ignores comments while executing the program.
A single-line comment begins with the # symbol.
Python does not have a dedicated multi-line comment syntax. Developers commonly use multiple # lines for comments.
Triple-quoted strings can also span multiple lines, but they are technically string literals rather than a special multi-line comment syntax.
Python variables are created when a value is assigned to a name.
Python does not require you to declare the variable type separately before assigning a value.
Normally, each Python statement is written on a separate line.
Writing statements on separate lines generally makes programs easier to read.
Python allows multiple simple statements on one line using a semicolon, although this is generally less readable.
A long expression can be split across multiple lines. Parentheses, brackets, and braces allow expressions to continue naturally across lines.
Python also supports an explicit line-continuation character, but using parentheses is usually clearer.
A block of code is a group of statements that belong together. Python uses indentation to define blocks.
Both print() statements belong to the if block because they have the same indentation level.
A colon : is used after statements that introduce a new block of code, such as if, for, while, functions, and classes.
The colon tells Python that an indented block follows.
Python identifiers such as variable, function, and class names follow certain rules.
Keywords are reserved words that have special meanings in Python. They cannot normally be used as variable or function names.
Examples include:
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.
The spaces around the = operator are optional but recommended because they improve readability.
A syntax error occurs when Python cannot understand the structure of the code according to its grammar rules.
The colon after the condition is missing, so Python reports a syntax error.
What does Python use to define blocks of code?