Python Syntax

Last Updated : 13 Apr 2026

Python syntax refers to the set of rules that define how Python code is written and organized. It is simple, clear, and easy to understand that makes Python programming a great choice for both beginners and experienced developers.

In this chapter, you will learn about Python syntax, its basic rules, and how to write clean and correct Python code.

What is Python Syntax?

Python syntax is like the grammar of the Python language. It defines how statements are written so that the Python interpreter can understand and execute them correctly. Proper syntax helps in writing structured, readable, and error-free code.

Python Interactive and Script Modes

Python provides two main ways to run programs: Interactive Mode and Script Mode. Let us see how to print "Hello, World!" using both modes.

1. Interactive Mode

In Interactive Mode, we use the Python interpreter directly from the command line by typing "python".

Syntax:

In the above syntax, >>> denotes a Python Command Prompt where we can type the commands. Let us now write a simple syntax to print "Hello, World!" in Python prompt and press Enter.

Example

Output:

Hello, World!

2. Script Mode

Another method to use Python Interpreter is by using the Python Script mode. In script mode the code is written in a .py file and then executed from the command prompt. Let us now write a simple syntax to print "Hello, World!" using the script mode and save it as sample.py.

Example

Execute Now

Output:

$ python sample.py
Hello, World!

Python Variables

Variables in Python are used to store data values. Unlike other languages, Python variables do not require explicit declaration of type; they are dynamically typed. This means that the type of variable is determined at runtime based on the value assigned to it.

Let us see a simple example showing the way of defining variables in Python.

Example

Execute Now

Output:

10 -> <class 'int'>
TpointTech -> <class 'str'>

Explanation:

In the above example, we have defined two variables of different data types. As we can observe, we have not declared their data types explicitly.

To learn more about Variables and their data types in Python, visit - Python Variables and Python Data Types.

Indentation in Python

Python uses indentation to define blocks of code instead of using curly braces {} as in other languages like C or Java. Indentation enhances code readability and is a crucial part of Python syntax. Every block of code (such as loops, functions, and conditionals) must be indented at the same level.

Example

Execute Now

Output:

9 is greater than 5
This is part of the if block
This is outside the if block

Explanation:

In the above example, we have used the if statement to check if 9 is greater than 5. If true, it will print the indented line of codes.

In case of improper indentation, it will raise an IndentationError. Proper indentation ensures the logical structure of the code is maintained.

Python Identifiers

Identifiers are names used for variables, functions, classes, and other entities in Python. They must follow specific rules to be valid:

  1. It can contain letters (A-Z, a-z), digits (0-9), and underscores (_).
  2. It cannot start with a digit.
  3. We cannot use Python keywords as identifiers.
  4. Case-sensitive (MyVar and myvar are different identifiers).
  5. It should be descriptive and follow naming conventions (e.g., using snake_case for variables and functions and CamelCase for class names).

Python Keywords

Keywords are reserved words in Python that cannot be used as variable names. Examples include if, else, while, for, def, class, import, etc.

FalseNoneTrueand
assertasawaitasync
breakclasscontinuedef
delelifelseexcept
finallyforfromglobal
ifimportnotin
returnyieldwhilewith
raiseorpasstry
islambdanonlocalmatch
case   

To learn more about Keywords, visit - Python Keywords

Python Comments

Comments in Python are used to explain code and make it more readable. They help other developers understand the purpose of the code and serve as a reminder for the programmer. Comments are ignored by the Python interpreter, meaning they do not affect the execution of the program.

Here is an example showing different types of comments in Python.

Example

Execute Now

Output:

Sum of a and b is 24

Explanation:

In the above example, the line followed by the # symbol is treated as comments. As a result, it is ignored by the interpreter. Moreover, the multiline comments declared using the triple quotes are often considered as docstrings.

To learn more about Comments, visit - Python Comments

Multiline Statements in Python

Writing a long statement in a code is not good practice causing unreadability. To prevent this, we can break the long line of code into multiple lines. This can be done either explicitly using backslash (\) or implicitly by parentheses ().

A backslash (\) in Python is a special character used to indicate line continuation. It allows long lines of code to be split across multiple lines for better readability without affecting the code's execution.

Let us take a look at the following example showing how backslash (\) works in Python.

Example

Execute Now

Output:

522

Explanation:

In the above example, we have used the backslash (\) to split the long line of code into multiple lines in order to increase the readability of the code.

To learn more about multiline statements, visit - Multi-Line Statements in Python

Taking Input from User in Python

In Python, we use the input() function to take user input. The input is always received as a string, even if the user enters a number.

Let us see the following example showing how to take input from the users in Python.

Example

Execute Now

Output:

Enter your first name: Tony
Enter your last name: Prince
Welcome, Tony Prince

Explanation:

In the above example, we have used the input() function to take input from the user.


Next TopicPython Keywords