Python Syntax: Basic Rules and Examples

Introduction

Python is one of the most beginner-friendly programming languages available today. One of the primary reasons for its popularity is its simple and readable syntax.

Syntax refers to the set of rules that define how a programming language is written and interpreted.

Python uses indentation instead of curly braces to define code blocks, reducing visual clutter and improving code readability.

What is Python Syntax?

Python syntax is the collection of rules that determine how Python programs are written and executed. These rules define how variables, functions, loops, conditions, and other programming constructs should be structured.

The Python interpreter checks the syntax of your code before executing it. If the syntax is incorrect, Python generates a syntax error.

📖
Characteristics of Python Syntax:
  • Easy to read and write
  • Uses indentation instead of braces
  • Case-sensitive language
  • Supports dynamic typing
  • Minimal use of punctuation
  • Requires proper spacing and indentation

Basic Python Syntax

A simple Python statement looks like this:

Input:


print("Hello, World!")

Output:

Hello, World!

This single line of code displays text on the screen using the print() function.

Unlike languages such as Java or C++, Python does not require semicolons at the end of statements.

1. Python Indentation

One of the most important syntax rules in Python is indentation.

Indentation refers to spaces at the beginning of a line.

Python uses indentation to define code blocks.

Example:


age = 18

if age >= 18:
    print("You are eligible to vote")

Output:

You are eligible to vote

The indented line belongs to the if statement.

Without proper indentation, Python generates an error.

Incorrect Example


age = 18

if age >= 18:
print("You are eligible to vote")

Output:

IndentationError

Always use consistent indentation, typically four spaces.

2. Python Variables

Variables are used to store data values.

Example:


name = "John"
age = 25

print(name)
print(age)

Output:

John
25

Python automatically determines the data type of variables.

Note: No explicit declaration is required.

3. Python Comments

Comments are used to explain code and improve readability.

Single-Line Comment:


# This is a comment

print("Hello")

Output:

Hello

Python ignores comments during execution.

4. Python Case Sensitivity

Python is a case-sensitive language.

This means:

name and Name are treated as different variables.

Example:


name = "John"
Name = "David"

print(name)
print(Name)

Output:

John
David

Always pay attention to letter casing.

5. Multiple Statements

Python allows multiple statements on separate lines.

Example:


x = 10
y = 20
z = x + y

print(z)

Output:

30

Although Python allows multiple statements on one line using semicolons, it is generally discouraged.

Example:


x = 10; y = 20; print(x + y)

Output:

30

6. Python String Syntax

Strings can be enclosed in either single quotes or double quotes.

Single Quotes:


name = 'John'

Double Quotes:


name = "John"

Both are valid.

Example:


print("Python")
print('Programming')

Output:

Python
Programming

7. Multi-Line Strings

Python supports multi-line strings using triple quotes.

Example:


message = """
Welcome
to
Python
Tutorial
"""
print(message)

Output:

Welcome
to
Python
Tutorial

Multi-line strings are useful for long text blocks.

8. Python Input Syntax

The input() function allows users to enter data.

Example:


name = input("Enter your name: ")
print("Hello", name)

Output:

Enter your name: John
Hello John

9. Python Function Syntax

Functions are reusable blocks of code.

Example:


def greet():
    print("Welcome to Python")
greet()

Output:

Welcome to Python

Notice the colon (:) and indentation.

These are important syntax requirements.

10. Python Loop Syntax

Loops execute code repeatedly.

For Loop Example:


for i in range(5):
    print(i)

Output:

0
1
2
3
4

While Loop Example:


count = 1
while count <= 3:
    print(count)
    count += 1

Output:

1
2
3

11. Python Conditional Syntax

Conditional statements help make decisions.

Example:


marks = 80
if marks >= 50:
    print("Pass")
else:
    print("Fail")

Output:

Pass

The colon and indentation are mandatory.

12. Real-Life Example

Imagine a school system that determines whether a student has passed.

Example:


student_name = "John"
marks = 75
if marks >= 50:
    print(student_name, "has passed")
else:
    print(student_name, "has failed")

Output:

John has passed

This simple example demonstrates how Python syntax is used in real-world applications.

13. Benefits of Python Syntax

1. Easy to Learn

Python syntax is simple and beginner-friendly.

2. Highly Readable

Code resembles plain English.

3. Faster Development

Developers can write programs quickly.

4. Fewer Lines of Code

Python often requires less code than other languages.

5. Improved Maintainability

Readable code is easier to maintain and debug.

14. Common Mistakes

1. Incorrect Indentation

Incorrect:


if True:
print("Hello")

Correct:


if True:
    print("Hello")

2. Missing Colon

Incorrect:


if age >= 18
    print("Adult")

Correct:


if age >= 18:
    print("Adult")

3. Using Undefined Variables

Incorrect:


print(name)

Output:

NameError

Correct:


name = "John"
print(name)

Output:

John

4. Case Sensitivity Errors

Incorrect:


Name = "John"
print(name)

Output:

NameError

Correct:


name = "John"
print(name)

Output:

John

5. Unmatched Quotes

Incorrect:


print("Hello)

Correct:


print("Hello")

15. Conclusion

Python syntax is one of the biggest reasons behind Python’s popularity. Its clean, readable, and beginner-friendly structure allows developers to write programs quickly while maintaining high code quality. Features such as indentation, simple variable declarations, straightforward function definitions, and readable control structures make Python easier to learn compared to many other programming languages.

Python Syntax – Interview Questions

Q 1: What is Python syntax?
Ans: Python syntax defines the rules for writing valid Python code.
Q 2: Does Python use braces {}?
Ans: No, Python uses indentation instead of braces to define code blocks.
Q 3: Is Python syntax case-sensitive?
Ans: Yes, Python treats uppercase and lowercase letters differently.
Q 4: What is indentation in Python?
Ans: Indentation is the spacing used to group statements into blocks.
Q 5: Does Python require semicolons?
Ans: No, semicolons are not required in Python.

Python Syntax – Objective Questions (MCQs)

Q1. Which of the following is the correct way to print a message in Python?






Q2. Which of the following is not a valid variable name in Python?






Q3. What is the correct indentation in Python used for?






Q4. Which of the following symbols is used to start a comment in Python?






Q5. What will be the output of the following code?

x = 5
y = "5"
print(x + int(y))






Related Python Tutorials