Python syntax is the set of rules used to write and organise Python code. These rules define how statements, expressions, blocks, and other elements are structured so that the Python interpreter can execute the code correctly.
Indentation in Python
Indentation refers to the spaces at the beginning of a code line. Python uses indentation to define blocks of code.
if 10 > 5:
print("This is true!")
print("Inside the if block")
print("Outside the if block")
Output
This is true! Inside the if block Outside the if block
Explanation:
- The two indented print() statements belong to the if block.
- Python uses indentation instead of braces {} to define code blocks.
- Statements at the same indentation level belong to the same block.
- Incorrect indentation can cause an IndentationError.
Python Statements
A statement is an instruction that Python executes. Statements can perform operations such as assigning values, calling functions, or controlling program flow.
x = 10
print(x)
Output
10
Explanation:
- x = 10 is an assignment statement.
- print(x) is a function call statement.
- Python executes statements in order.
Python Variables
A variable is a name that refers to an object in Python. The type of the object is determined by the value assigned to the variable.
a = 10
print(type(a))
b = 'GeeksforGeeks'
print(type(b))
Output
<class 'int'> <class 'str'>
Explanation:
- a refers to an integer object.
- b refers to a string object.
- type() returns the type of an object.
- Python does not require a type declaration when creating a variable.
Python Identifiers
Identifiers are names used for variables, functions, classes, and other objects in Python.
first_name = "Ram"
print(first_name)
Output
Ram
Explanation:
- first_name is an identifier.
- It refers to the string "Ram".
- Python identifiers are case-sensitive
Rules for Python Identifiers
- Letters can be uppercase or lowercase.
- Digits 0–9 and underscores _ are allowed.
- Must start with a letter or underscore.
- Cannot contain spaces or special characters.
- Cannot be a Python keyword.
Python keywords
Keywords are reserved words that have predefined meanings in Python. They cannot be used as identifiers such as variable, function, or class names.
Some Python keywords are:
False None True and as
assert async await break case
class continue def del elif
else except finally for from
global if import in is
lambda match nonlocal not or
pass raise return try while
with yield
Comments in Python
Comments are used to provide information about the code. Python ignores comments during execution.
1. Single Line Comment
A single-line comment starts with the # symbol.
first_name = "Reddy"
last_name = "Anna"
print(first_name, last_name)
Output
Reddy Anna
Explanation:
- # marks the beginning of a comment.
- Everything after # on that line is ignored by Python.
- Comments can be placed on a separate line or after a statement.
2. Python Multi-line Comment
Python does not have a dedicated multi-line comment syntax. Multiple # comments can be used for multiple lines.
f_name = "Alen"
print(f_name)
Explanation:
- Each line starts with #.
- Python ignores these lines during execution.
- Triple-quoted strings are string literals, not technically multi-line comments.
Line Continuation in Python
A long statement can be written across multiple lines to improve readability. Python supports both implicit and explicit line continuation.
1. Implicit Line Continuation
Python allows statements to continue across multiple lines inside parentheses (), square brackets [], and curly braces {}.
numbers = [
1, 2, 3,
4, 5, 6,
7, 8, 9
]
print(numbers)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
- The list is written across multiple lines.
- Square brackets allow the statement to continue automatically.
- No backslash is required.
2. Explicit Line Continuation
A backslash \ can be used to continue a statement on the next line.
total = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9
print(total)
Output
45
Explanation:
- \ tells Python that the statement continues on the next line.
- The expression is treated as one statement.
- Nothing should be written after the backslash on the same line.
Note: A space or any other character after \ causes a SyntaxError.
Taking Input from User in Python
The input() function is used to take input from the user. It returns the entered value as a string
name = input("Enter your name:")
print("Hello, " + name)

Explanation:
- input() takes input from the user.
- "Enter your name: " is displayed as a prompt.
- The entered value is stored in name.
- print() displays the greeting with the entered name.