Saturday , December 5 2026
Python Basic Syntax - Python Identifiers, Reserved Words, Lines and Indentation

Python Basic Syntax

Updated: 28 November 2026Level: BeginnerReading time: about 8 minutesPython 3.8+

Syntax is the set of rules for how a Python program must be written. Python is known for clean, readable syntax: no semicolons, no curly braces, and indentation that actually matters.

What You Will Learn

  • How to write and run your first program
  • Identifiers and keywords
  • Why indentation matters
  • Comments, multi-line statements and quotes

Prerequisites

Python 3 installed. See Python Installation.

Your First Python Program

Example
print('Hello, Machine Learning!')

Explanation: print() displays text. Save it as hello.py and run python hello.py, or run it in a Jupyter cell with Shift+Enter.

Output
Hello, Machine Learning!

Identifiers

An identifier is the name of a variable, function or class.

Rule Valid Invalid
Start with a letter or _ name, _count 1name
Only letters, digits, _ total_marks total-marks
Not a keyword my_class class
Note: Python is case-sensitive: Age and age are different.

Keywords

Example
import keyword
print(keyword.kwlist)

Explanation: Keywords such as if, for, def, class, True and None have special meaning and cannot be used as names. The exact list depends on your Python version.

Indentation

Python groups code into blocks using indentation. Use 4 spaces per level.

Example
marks = 75
if marks >= 40:
    print('Pass')
    print('Well done')
else:
    print('Fail')

Explanation: Both indented lines belong to the if block. Inconsistent indentation raises IndentationError.

Output
Pass
Well done

Comments

Example
# single-line comment
print('Hi')  # comment after code

"""
Triple-quoted text is often used
as a multi-line comment or docstring.
"""

Explanation: Python ignores everything after #. Use comments to explain why the code does something.

Multi-Line Statements

Example
total = 10 + \
        20 + \
        30
days = ['Mon', 'Tue',
        'Wed', 'Thu']
print(total, days)

Explanation: A backslash continues a line. Inside (), [] or {} you can break lines without it, which is the preferred style.

Output
60 ['Mon', 'Tue', 'Wed', 'Thu']

Quotes

Example
a = 'single'
b = "double"
c = '''triple quotes
span lines'''
print("It's easy")

Explanation: Single and double quotes are the same. Triple quotes allow text over several lines.

Taking Input

Example
name = input('Enter your name: ')
print('Hello', name)

Explanation: input() always returns a string.

Common Errors

Problem Cause and fix
IndentationError Indent the line after if/for/def/class consistently with 4 spaces.
SyntaxError: invalid syntax Usually a missing : or an unclosed bracket or quote.
NameError: name 'Print' is not defined Python is case-sensitive; use print.

Practice Exercises

  1. Print your name, city and favourite language on three lines.
  2. Write if/else that prints Even or Odd.
  3. Check if lambda is a keyword with keyword.iskeyword().

Interview Questions

Q: How does Python define blocks?
By indentation instead of braces.

Q: What does input() return?
A string.

Summary

Python uses indentation for blocks, # for comments, case-sensitive names, and no semicolons.

Next: Python Variable Types · Python Basic Operators

About Machine Learning

Check Also

Python Inheritance: super(), Method Overriding and MRO

Python inheritance with super(), method overriding, isinstance, multiple inheritance and the method resolution order.