Home › Python Tutorial › Python Syntax

Python Syntax

⏱ 5 min read Updated: 25 Nov 2026

Python syntax refers to the set of rules that is used to write the program in python. Just as any language has grammatical rules, Python also has specific rules to write the code. It has very easy syntax. We are carefully define the indentation, variables, keyword and statements to write the python programming language.

1. First Python Program

We are going to write the first python program to write Hello World using the print() function.

Example:

print("Hello World!")

Output:

Hello World!

Explanation:

In this example, we use the print function to print the Hello world on the screen.

2. Indentation in Python

In Python, indentation means adding spaces at the beginning of a code line. It is used to identify code blocks and show which statements belong together.

Example:

if 10 > 5:

   print("10 is greater than 5")

Output:

10 is greater than 5

Explanation:

In this example, we use the if condition to check whether 10 is greater than 5. After the condition, we use a colon (:). Then, we add four spaces before the print() function to show that it belongs to the if block.

Note: Correct indentation is important in Python. If you use incorrect indentation, Python may show an IndentationError.

3. Comments in Python

We use the comments to understand and remember the code. The interpreter cannot execute the comments line of code. We use the Hash (#) symbol to write the Single line comments.

Example:

# This is a comment print("Welcome to Python")

Output:

Welcome to Python

Explanation:

In the above example, we use the # comment symbol. The interpreter cannot execute the code while executing the code.

4. Variables in Python

A variable is used to store a value in Python. We do not need to declare a separate keyword or data type when creating a variable in Python. Python automatically identifies the data type based on the assigned value.

Example:

name = "John" age = 22 price = 99.50 print(name) print(age) print(price)

Output:

Shivam 22 99.5

Explanation:

In the above example, we create three variables: name, age, and price. The name variable stores a text value ("John"), the age variable stores an integer value (22), and the price variable stores a decimal value (99.50). We use the print() function to display the values of these variables on the screen.

5. Case Sensitivity in Python

Python is a case-sensitive programming language. It means that uppercase and lowercase letters are treated differently. For example, name, Name, and NAME are considered three different variables in Python.

Example:

name = "Rahul"

Name = "Amit"

print(name)

print(Name)

Output:

Rahul Amit

6. Statements in Python

In python, statements is a instruction that executes the interpreter. We do nt write the separate semicolon(;) in a general python statements.

Example:

a = 10

b = 20

sum = a + b

print(sum)

Output:

30

7. Multiple Statements in Python

Python allows you to write multiple statements on a single line. You can use a semicolon (;) to separate multiple statements.

Example:

a = 10;

 b = 20;

 print(a + b)

Output:

30

8. Keywords in Python

Keywords are reserved words that have special meanings in Python. They are used to define the structure and syntax of a Python program.

Some common Python keywords are listed below:

KeywordUse
ifTo check a condition
elseTo execute another block when a condition is false
forTo run a loop
whileTo run a loop based on a condition
defTo define a function
classTo create a class
returnTo return a value from a function
importTo import a module
breakTo stop a loop
continueTo skip the current iteration

Example:

if 10 > 5:

   print("Condition is True")

Output:

Condition is True

Explanation:

In this example, if is a keyword used to check a condition. Since 10 is greater than 5, the condition is true, and the print() function displays the output.

9. Strings in Python

A String is used to store text in Python. You can write a string using single quotes (' '), double quotes (" "), or triple quotes (''' ''').

Example:

name = "Shivam"

city = 'Noida'

message = """Welcome to Python Programming"""

print(name)

print(city)

print(message)

Output:

Shivam Noida Welcome to Python Programming

Explanation:

In the above example, we create three string variables: name, city, and message. We use different types of quotation marks to store text in these variables. The print() function displays their values on the screen.

10. Code Blocks in Python

A Code Block is a group of statements that belong to a particular condition, loop, or function. It helps Python identify which statements should be executed together.

In Python, a colon (:) and indentation are used to define code blocks.

Example:

def greet():

   print("Hello")

   print("Welcome to Python")

greet()

Output:

Hello Welcome to Python

Explanation:

In this example, we use the def keyword to create a function named greet(). Inside the function, we write two print() statements with proper indentation. Both statements belong to the function block. When we call the greet() function, Python executes these statements and displays the output.

Important Rules of Python Syntax

While writing Python code, you should keep the following rules in mind:

  1. Use proper indentation in Python.
  2. Python is a case-sensitive programming language.
  3. You do not need to declare a separate data type when creating variables.
  4. Use the # symbol to write comments.
  5. You do not need to use a semicolon (;) at the end of every statement.
  6. Use a colon (:) and indentation to define code blocks.
  7. Python keywords cannot be used as variable names.
  8. Strings can be written using single, double, or triple quotes.

Conclusion

Python Syntax is a set of rules used to write Python programs correctly. Python has a simple syntax, which makes it easy for beginners to learn. Once you understand indentation, variables, comments, keywords, and statements, writing and understanding Python programs becomes easier.