- It reads like English — students can focus on problem solving rather than wrestling with syntax.
- It is free and open-source — runs on Windows, macOS, Linux and Raspberry Pi without any licence cost.
- It has a huge standard library and thousands of third-party modules (NumPy, Pandas, Matplotlib, scikit-learn, Flask, Django, Pygame…).
- A beginner can write useful code in a single afternoon; a professional can build production systems in it.
8.1 Introduction to Python
Python was born at Centrum Wiskunde & Informatica (CWI) in the Netherlands in December 1989, when Guido van Rossum was looking for a "hobby project" over the Christmas break. The language was named after the British comedy group Monty Python's Flying Circus — not the snake (although the snake logo is now the official mascot).
| Milestone | Year | Highlights |
|---|---|---|
| Python 1.0 | 1994 | First public release — functions, modules, exceptions. |
| Python 2.0 | 2000 | List comprehensions, garbage collection. |
| Python 3.0 | 2008 | Big overhaul; broke backward compatibility. print() becomes a function. |
| Python 3.12 | 2023 | Better error messages, faster interpreter. |
| Current (2026) | 3.13+ | Now the dominant version worldwide; Python 2 is officially retired. |
print x (Python 2) is now print(x).
8.2 Features of Python
Python stays popular because it checks almost every box a modern language should. The nine features below are the ones CBSE examines most often.
| Feature | Meaning |
|---|---|
| Easy to learn & readable | Syntax is close to plain English; indentation replaces {/} blocks, so code is visually clean. |
| Free & open-source | Anyone can download, use, modify and redistribute Python at no cost. |
| Platform-independent | The same .py file runs on Windows, macOS and Linux without changes. |
| Interpreted | No separate compile step. The interpreter reads the source line by line and executes it immediately. |
| Dynamically typed | You do not declare a variable's type — Python figures it out from the value you assign. |
| Rich standard library | "Batteries included" — comes with modules for maths, random numbers, files, dates, web, regex and much more. |
| Object-oriented | Supports classes, inheritance and objects — while allowing a simple "script style" for small programs. |
| Extensible & embeddable | Can call C / C++ code for speed, and can itself be embedded into larger applications. |
| GUI & web ready | Tkinter ships with Python for GUIs; Django and Flask power major websites. |
8.3 Your First Program — "Hello, World!"
A long tradition: every programming lesson begins with a program that prints a greeting. In Python it is just one line.
print("Hello, World!")
Save the file as hello.py, open a terminal in that folder, and run:
python hello.py
Expected output:
Hello, World!
print— a built-in function that writes to the screen.()— parentheses send arguments to the function."Hello, World!"— a string literal, the text to be printed.
main(), no compilation — you are writing Python.
8.4 Execution Modes — Interactive vs. Script
Python can be used in two complementary ways.
8.4.1 Comparison
| Criterion | Interactive Mode | Script Mode |
|---|---|---|
| How you enter code | One line at a time at the >>> prompt | Write the whole program in a .py file |
| Output shown | After every statement | Only what print() produces |
| Code saved? | No — lost when you close the shell | Yes — the .py file persists |
| Good for | Quick tests, exploring, calculator use | Real programs, assignments, projects |
| How to start | Type python (or open IDLE) in a terminal | Create file.py, run python file.py |
8.5 Python Character Set
The character set of a language is the collection of symbols it understands. Python uses the full Unicode character set, but the characters you will actually type in your code fall into four groups:
| Category | Characters |
|---|---|
| Letters | A – Z, a – z (and any Unicode letter — Python 3 accepts names like गिनती or número) |
| Digits | 0 – 9 |
| Special symbols | + − * / % = < > ( ) [ ] { } , . : ; # ' " and so on |
| Whitespace | Space, tab, newline — significant in Python because indentation defines blocks. |
8.6 Python Tokens
When Python reads your source code, the very first job of the interpreter is to chop the text into small meaningful pieces called tokens. A token is the smallest unit of a program that has meaning to the compiler. Python has five kinds of tokens:
8.6.1 Keywords
Keywords are reserved words that have a fixed meaning in Python. You cannot use them as the name of a variable, function or class. Python 3.13 has 35 keywords; you do not need to memorise all of them, but you will meet every one during Class XI & XII.
| 35 Python Keywords (alphabetical) | ||||
|---|---|---|---|---|
| False | None | True | and | as |
| assert | async | await | break | class |
| continue | def | del | elif | else |
| except | finally | for | from | global |
| if | import | in | is | lambda |
| nonlocal | not | or | pass | raise |
| return | try | while | with | yield |
import keyword print(keyword.kwlist)
8.6.2 Identifiers
An identifier is a name that you give to something in your program — a variable, a function, a class, a module. Identifiers follow strict rules:
- Must start with a letter (A – Z or a – z) or an underscore
_. Cannot start with a digit. - The remaining characters may be letters, digits (0 – 9) or underscores.
- Identifiers are case-sensitive —
Name,nameandNAMEare three different names. - A keyword from the table above cannot be used as an identifier.
- There is no fixed length limit.
- Special characters such as
@ # $ %are not allowed inside a name.
| Example | Valid? | Why |
|---|---|---|
name | ✓ | Starts with a letter. |
student_age | ✓ | Letters + underscore. |
_total | ✓ | Starts with underscore. |
Marks2024 | ✓ | Digits allowed after a letter. |
2marks | ✗ | Starts with a digit. |
student-age | ✗ | Contains a hyphen (looks like subtraction to Python). |
for | ✗ | Is a keyword. |
first name | ✗ | Contains a space. |
total@marks | ✗ | Contains a special symbol. |
8.6.3 Literals
A literal is a fixed value written directly in the source code. Python supports five groups:
| Type | Examples |
|---|---|
| Integer literal | 0, 100, −37, 1_000_000 (underscores allowed for grouping) |
| Floating-point literal | 3.14, −0.5, 6.022e23 (e for ×10ⁿ) |
| String literal | "hello", 'world', """triple-quoted spans many lines""" |
| Boolean literal | True, False (note the capital T and F) |
| None literal | None — represents "no value / nothing" |
8.6.4 Operators
An operator is a symbol that tells Python to perform some action on one or two values (called operands). The full treatment of operators comes in Chapter 10; here is a preview:
- Arithmetic —
+ − * / // % ** - Relational —
== != < > <= >= - Logical —
and,or,not - Assignment —
=and augmented forms like+=,-=,*= - Identity —
is,is not - Membership —
in,not in
8.6.5 Punctuators
Punctuators are symbols used to structure a program. They do not act on data directly; they tell Python where things start and end.
| Symbol | Used for |
|---|---|
( ) | Function call / grouping in expressions |
[ ] | Lists, indexing, slicing |
{ } | Dictionaries and sets |
, | Separating items in a list, arguments, etc. |
: | Marks the start of an indented block (if, while, def …) |
. | Access attribute or method — math.pi, name.upper() |
' " | String delimiters |
# | Begins a single-line comment |
; | Optional — separates multiple statements on one line |
8.7 Variables
= operator, and Python figures out the type automatically.
name = "Ananya" # string age = 16 # integer pi = 3.14159 # float can_vote = False # boolean print(name, age, pi, can_vote)
Output: Ananya 16 3.14159 False
x = 10 # x is an int x = "hello" # now x is a string — perfectly legal
8.7.1 How an assignment works — the Box Model
When you write age = 16, Python does three things:
- Creates an integer object
16in memory. - Creates a name
age. - Makes
agerefer to the object.
8.8 L-Value and R-Value
Every assignment statement in Python (or any C-family language) has two sides separated by =:
- L-value (left-hand value) — the location where the data will be stored. It must be something that can hold a value — a variable name, a list element, etc.
- R-value (right-hand value) — the value that will be placed there. It can be a literal, an expression, or another variable.
marks = 85 # L-value: marks (a variable — can hold a value) # R-value: 85 (the value being stored) area = length * breadth # L-value: area # R-value: length * breadth (an expression, evaluated first)
10 = marks # ❌ 10 is not a location marks + 5 = 100 # ❌ an expression cannot be an L-valueYou will see this idea again in C++ and in pointer-based languages.
8.9 Comments
A comment is text inside the source file that Python ignores. Comments exist for human readers — to explain why a piece of code exists, a non-obvious shortcut, or a reminder to fix something later.
8.9.1 Single-line comments (#)
Anything from # to the end of the line is a comment.
# Compute the GST-included price price = base * 1.18 # 18% GST
8.9.2 Multi-line "comments" (triple-quoted strings)
Python has no formal multi-line comment syntax. The common trick is to use a triple-quoted string, which Python will evaluate and immediately discard if it isn't assigned to anything.
""" This function computes the area of a rectangle given its length and breadth. Returns a float rounded to 2 decimal places. """ def area(length, breadth): return round(length * breadth, 2)
help(). That is why the convention above is so useful.
8.9.3 Why write comments?
- Non-obvious reasoning — explain why the code does what it does, not what (well-named variables already tell that).
- Constants that have meaning —
TAX_RATE = 0.18 # 18% GST for India (2026). - TODO reminders —
# TODO: handle negative inputs. - Disable a line temporarily during debugging — a popular "comment it out" trick.
# ❌ WHAT — useless, the code already says this i = i + 1 # add 1 to i # ✓ WHY — valuable, captures the intent i = i + 1 # skip duplicate row — see issue #42Aim for the second style.
📌 Quick Revision — Chapter 8 at a Glance
- Python — created by Guido van Rossum (1991); named after Monty Python's Flying Circus. High-level, interpreted, dynamically typed.
- Key features: easy / readable · free & open-source · platform-independent · interpreted · dynamically typed · huge standard library · object-oriented · extensible · GUI & web ready.
- Hello World:
print("Hello, World!"). Run withpython hello.py. - Execution modes:
- Interactive — REPL at
>>>prompt; quick tests, output after every line, code not saved. - Script — code in a
.pyfile; real programs; saved & reusable.
- Interactive — REPL at
- Character set: letters, digits, special symbols, whitespace (significant in Python).
- Five tokens: Keywords · Identifiers · Literals · Operators · Punctuators.
- Keywords — 35 reserved words (
if,else,for,while,def,class,True,False,None, …). Cannot be used as names. - Identifier rules: start with letter/
_, then letters/digits/_; case-sensitive; no keywords; no spaces or special symbols. - Literals: Integer, Float, String, Boolean (
True/False), None. - Variables: bound with
=; type inferred automatically; same name can be rebound to any type. - L-value / R-value: L-value is the location (a name); R-value is the value (literal or expression).
L = R. - Comments:
#for single-line; triple-quoted strings for multi-line / docstrings. Write why, not what.