VM-LEARNING /class.xi ·track.ai ·ch-b3 session: 2026_27
$cd ..

~/Python Programming

root@vm-learning ~ $ open ch-b3
PART B ▪ UNIT 3
08
Python Programming
Level 1: Basics & Control Flow · Level 2: CSV · NumPy · Pandas · Scikit-learn
Python is a general-purpose, high-level programming language created by Guido van Rossum and released in 1991. It was named after the BBC comedy series "Monty Python's Flying Circus". Python is a powerhouse in data science and AI because of its simple syntax, huge library ecosystem and platform independence. The CBSE Class XI AI curriculum divides Python into two levels — Level 1 (basics + control flow) and Level 2 (CSV files + NumPy / Pandas / Scikit-learn).
Learning Outcome: Explain Python basics & write programs with tokens · Use selective and iterative statements effectively · Use Python libraries (NumPy, Pandas, Scikit-learn) efficiently
LEVEL 1 — PYTHON BASICS & CONTROL FLOW

1.1 Features of Python

🎓 High LevelEasy-to-read code close to human language.
🔄 InterpretedRuns line-by-line — no separate compile step.
🆓 Free & Open SourceAnyone can use, share or modify it.
🌐 Cross-PlatformRuns on Windows / Linux / macOS with the right interpreter.
✍️ Easy to LearnSyntax similar to plain English.
🔤 ASCII + UNICODEHandles all standard text encodings.
💡 Variety of EditorsIDLE · PyCharm · Anaconda · Spyder · Jupyter · Google Colab.
🚀 Widely UsedWeb, AI, data science, automation, scripting.

1.2 Python Editors — Jupyter Notebook

Jupyter Notebook is an open-source web application that lets you create and share documents containing live code, equations, visualisations and text — all in one file. It is widely used in data science and AI research.

Installation & Running

# Install via pip (in admin mode command prompt)
pip install notebook

# Launch Jupyter Notebook from a terminal
jupyter notebook
You can also install Jupyter bundled with Anaconda — a data-science Python distribution that comes with NumPy, Pandas, Matplotlib, Scikit-learn pre-installed.

1.3 Tokens — Building Blocks of Python

A token is the smallest unit of a Python program that the interpreter recognises. Every line of Python code is made up of tokens — the interpreter breaks source code into tokens during lexical analysis before parsing.
🔑
Keywords
Reserved words with special meaning. Cannot be used as variable names.
e.g., if, else, for, while, def, class, import, True, False, None.
🏷️
Identifiers
Names for variables, functions, classes, modules.
Rules: cannot start with a digit · no special characters except _ · cannot be a keyword.
💠
Literals
Raw data values written directly in code.
Types: String · Numeric · Boolean (True/False) · Special (None) · Literal collections.
Operators
Symbols / keywords that perform operations. See §1.4 for full list.
⌨️
Punctuators
Special symbols that organise code: : ( ) [ ] { } , ; . ' " / \ & @ ! ? | ~
💬
Comments
Begin with #. Ignored by the interpreter — help readability.

1.4 Operators in Python

TypeOperatorsExample
Arithmetic+   -   *   /   %5 + 2 = 7
Relational==   !=   <   >   <=   >=5 > 3 → True
Assignment=   +=   -=   *=   /=x = 10; x += 5 → 15
Logicaland   or   notTrue and False → False
Bitwise&   |   ^   <<   >>5 & 3 → 1
Identityis   is nota is b → True/False
Membershipin   not in"a" in "cat" → True

1.5 Data Types

Python is a dynamically-typed language — it figures out the type of a variable automatically from the value assigned to it.

TypeDescriptionExample
Integer (int)Whole numbers.a = 10
Boolean (bool)True / False values.result = True
FloatNumbers with a fractional part.x = 5.5
ComplexNumbers with real + imaginary parts.num = 3+2j
String (str)Immutable sequence of characters. Single or double quotes.name = "Ria"
ListMutable sequence of comma-separated values in [ ].lst = [25, 15.6, "car", "XY"]
TupleImmutable sequence in ( ).tup = (11, 12.3, "abc")
SetUnordered, no duplicates, in { }.s = {25, 3, 3.5}
DictionaryUnordered key : value pairs in { }.d = {1:"One", 2:"Two"}
Mutable vs Immutable: List / Set / Dictionary → mutable (can change after creation). String / Tuple → immutable.

1.6 Input Function & Type Casting

The input() function reads text from the user. It always returns a string — so to get numeric input, use a conversion function.

name = input("What is your name? ")     # returns string
age = int(input("Your age? "))           # convert to integer
marks = float(input("Your marks? "))       # convert to float
The explicit conversion of one data type to another is called Type Casting. Functions: int() · float() · str() · bool() · list() · tuple().

Sample Program 1 — Display a string

print("National Animal - Tiger")
National Animal - Tiger

Sample Program 2 — Area of a rectangle

length = 50
breadth = 20
area = length * breadth
print("Area of rectangle =", area)
Area of rectangle = 1000

Sample Program 3 — Total marks of a student

name = input("Enter name: ")
m1 = float(input("Marks in subject 1: "))
m2 = float(input("Marks in subject 2: "))
m3 = float(input("Marks in subject 3: "))
total = m1 + m2 + m3
print(name, "scored total =", total)

1.7 Control Flow Statements

By default, Python runs statements top-to-bottom. To selectively run or repeat code, use control flow statements:

🔀
Selection
if / if-else / if-elif-else — choose which block to run.
🔁
For Loop
Iterates a fixed number of times (when you know the count).
🔄
While Loop
Repeats as long as a condition is True.

Selection — if / else

if condition:
    # runs if condition is True
elif another_condition:
    # runs if second condition is True
else:
    # runs if all above are False
Python uses indentation (4 spaces) to define blocks. Missed / extra indent = error.

Sample Program 4 — Restaurant menu selector

choice = input("Veg / Non-veg? ")
if choice == "Veg":
    print("Menu - Veg only")
elif choice == "Non-veg":
    print("Menu - Veg & Non-veg")
else:
    print("Menu - All options available")

Sample Program 5 — Triangle type

a = float(input("Side 1: "))
b = float(input("Side 2: "))
c = float(input("Side 3: "))

if a == b == c:
    print("Equilateral triangle")
elif a == b or b == c or a == c:
    print("Isosceles triangle")
else:
    print("Scalene triangle")

For Loop — Syntax

for variable in sequence:
    # body — runs once per item

range(5) generates 0, 1, 2, 3, 4 (five values starting from 0).

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

Sample Program 6 — Even numbers 100-110 & their squares

for i in range(100, 111):
    if i % 2 == 0:
        print(i, "square =", i**2)

Sample Program 7 — Display each list element with its type

lst = [25, 15.6, "car", "XY", 100]
for item in lst:
    print(item, type(item))

Sample Program 8 — Split a string into words

s = input("Enter a sentence: ")
words = s.split()
for w in words:
    print(w)

Sample Program 9 — Display dictionary values

d = {1: "One", 2: "Two", 3: "Three"}
for k in d:
    print(k, "→", d[k])
LEVEL 2 — CSV FILES · NUMPY · PANDAS · SCIKIT-LEARN

2.1 CSV Files (Comma-Separated Values)

A CSV file stores tabular data (rows + columns) as plain text, with values separated by commas. Each line is one record. CSV is the default format for datasets used in AI programming.

Common CSV Operations

TaskCode
Import the CSV libraryimport csv
Open for readingfile = open("student.csv", "r")
Open for writingfile = open("student.csv", "w")
Close a filefile.close()
Write a rowwr = csv.writer(file); wr.writerow([12, "Kalesh", 480])
Read all rowsfor rec in csv.reader(file): print(rec)

Sample Program 10 — Read a CSV file

import csv
file = open("students.csv", "r")
details = csv.reader(file)
for rec in details:
    print(rec)
file.close()
CSV files can be easily created in spreadsheets (Excel, LibreOffice Calc, Google Sheets) — just save with the .csv extension.

2.2 Introducing Python Libraries

A library is a collection of ready-made, reusable functions organised into modules. Libraries are imported with the import statement. For example, the math library contains sqrt(), pow(), abs(), sin().

For AI and data science, three libraries are essential: NumPy (numeric computing), Pandas (data manipulation), Scikit-learn (machine learning).

2.3 NumPy (Numerical Python)

NumPy is a general-purpose array-processing package. Its main structure is the ndarray (N-dimensional array) — homogeneous (all elements of the same type) and can hold any dimension. NumPy operations are vectorised, making them fast.

Why Use NumPy in AI?

Suppose you have exam scores for students in multiple subjects. NumPy arrays let you efficiently:

Installation

pip install numpy

Creating NumPy Arrays

import numpy as np

# From a list of tuples
arr1 = np.array([(1, 2, 3), (4, 5, 6)])
print(arr1)

# Empty array, then fill with user input
arr2 = np.empty(5)
for i in range(5):
    arr2[i] = float(input("Enter value: "))

2.4 Pandas (Panel Data / Python Data Analysis)

Pandas is a powerful library for data manipulation. Built on top of NumPy, it is ideal for working with tabular data (spreadsheets, SQL tables). The two core structures are Series (1-D) and DataFrame (2-D).

Installation

pip install pandas

Series — 1-D Labelled Array

A Series is a one-dimensional array where each value has a label called an index. By default the index is 0, 1, 2, 3… Think of a Series as one column in a spreadsheet.

import pandas as pd

s = pd.Series([25, 30, 35, 40])
print(s)

DataFrame — 2-D Labelled Table

A DataFrame is a 2-D table (rows + columns) where each column can have a different data type. Think of it as a MySQL table or a spreadsheet sheet.

import pandas as pd
import numpy as np

# Method 1 — From NumPy ndarray
arr = np.array([[80, 75], [90, 85]])
df1 = pd.DataFrame(arr, columns=["Maths", "Science"])

# Method 2 — From list of dictionaries
data = [{"Name": "Amit", "Marks": 75},
        {"Name": "Ashu", "Marks": 95}]
df2 = pd.DataFrame(data)
Dictionary keys become column labels; lists become rows. Missing values are automatically filled with NaN (Not a Number).

Common DataFrame Operations

TaskCode
Add a new columndf["NewCol"] = [1, 2, 3]
Add a new rowdf.loc["English"] = [80, 90, 85]
Delete a row (axis = 0)df.drop("Hindi", axis=0)
Delete columns (axis = 1)df.drop(["Rajat", "Meenakshi"], axis=1)
Access by labeldf.loc["Maths"]
Access by index positiondf.iloc[0]

Handling Missing Values (NaN)

TaskCode
Detect missing valuesdf.isnull()
Any missing in a column?df["Music"].isnull().any()
Total number of NaNdf.isnull().sum()
Drop rows with NaNdf.dropna()
Fill NaN with a valuedf.fillna(1)

DataFrame Attributes

AttributeWhat it returns
df.indexRow labels (index).
df.columnsColumn labels.
df.dtypesData type of each column.
df.valuesData as a NumPy array.
df.shape(rows, columns) tuple.
df.head(n)First n rows (default 5).
df.tail(n)Last n rows.

Import / Export CSV with Pandas

import pandas as pd

# Import a CSV into a DataFrame
df = pd.read_csv("students.csv")

# Export a DataFrame to CSV
df.to_csv("output.csv", sep=",")
Parameters of read_csv(): sep = separator character · header = row number of column names (default 0 = first row).

2.5 Scikit-learn (sklearn) — Machine Learning in Python

Scikit-learn is the most popular Python library for machine learning. Built on NumPy, SciPy and Matplotlib, it offers a consistent interface for supervised & unsupervised learning, model evaluation, and data pre-processing.
Teacher's note: this topic is best taught after the Machine Learning Algorithms unit (Part B Unit 6).

Key Features

Installation

pip install scikit-learn

2.6 End-to-End ML Pipeline — The Iris Example

The Iris dataset is a classic ML dataset with 150 iris flower samples across 3 species (setosa, versicolor, virginica). For each flower: sepal length, sepal width, petal length, petal width.

Step 1 — Load the Dataset

from sklearn.datasets import load_iris
iris = load_iris()

X = iris.data       # feature vectors (input)
y = iris.target     # target variable (species labels)

print(X[:10])       # first 10 rows

A sample row [5.1, 3.5, 1.4, 0.2] means sepal length = 5.1 cm, sepal width = 3.5 cm, petal length = 1.4 cm, petal width = 0.2 cm.

Step 2 — Split Train / Test Sets (80 : 20)

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=1)
ParameterMeaning
X_train, y_trainFeatures + labels for training (80 %).
X_test, y_testFeatures + labels for testing (20 %).
test_size = 0.220 % data used for testing.
random_state = 1Fixes the random seed — same split every time the code runs.

Step 3 — Train the K-Nearest-Neighbours Classifier

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)

n_neighbors=3 means the classifier uses the 3 closest training points to predict each new sample.

Step 4 — Predict and Evaluate

y_pred = knn.predict(X_test)

from sklearn import metrics
accuracy = metrics.accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Step 5 — Predict on New Sample Data

sample = [[5, 5, 3, 2], [2, 4, 3, 5]]
preds = knn.predict(sample)

pred_species = []
for p in preds:
    pred_species.append(iris.target_names[p])

print("Predictions:", pred_species)
This is the standard ML pipeline: Load data → Split train/test → Train a model (fit) → Predict → Evaluate accuracy.
PRACTICE PROGRAMS (Level 1 & Level 2)

3.1 Practice Programs from Syllabus

The CBSE syllabus requires at least 5 programs on Level 1 (operators, data types, control statements) and 5 programs on Level 2 (NumPy, Pandas, Scikit-learn).

Level 1 — Program 1: Tipper (15 % & 20 %)

bill = float(input("Total restaurant bill: "))
tip15 = bill * 0.15
tip20 = bill * 0.20
print("15% tip =", tip15)
print("20% tip =", tip20)

Level 1 — Program 2: Driving-licence eligibility

age = int(input("Enter your age: "))
if age >= 18:
    print("Eligible for driving licence")
else:
    print("Not eligible - wait", 18-age, "more years")

Level 1 — Program 3: Car-service check

km = float(input("Kilometre reading: "))
if km >= 15000:
    print("Car needs service")
else:
    print("Service not needed yet")

Level 1 — Program 4: First 10 even natural numbers (for loop)

for i in range(2, 21, 2):
    print(i)

Level 1 — Program 5: Salary computation

basic = float(input("Basic salary: "))
HRA = 0.30 * basic
DA = 0.20 * basic
PF = 0.12 * basic
net = basic + HRA + DA - PF
print("Net Salary =", net)

Level 2 — Program 1: NumPy Series from array

import numpy as np
import pandas as pd

arr = np.array([10, 20, 30, 40, 50])
s = pd.Series(arr)
print(s)

Level 2 — Program 2 to 5: admission.csv analysis

Based on a CSV with columns Name, CLASS, Gender, Marks:

import pandas as pd

# (a) Create DataFrame from CSV
df = pd.read_csv("admission.csv")

# (b) Display first 3 rows
print(df.head(3))

# (c) Display Ravi's details
print(df[df["Name"] == "Ravi"])

# (d) Total rows and columns
print(df.shape)

# (e) Display Gender column
print(df["Gender"])

3.2 Practical Certification

From the syllabus: Earn a credential on IBM SkillsBuild — Python for Data Science.
  1. Visit skillsbuild.org → sign up as a High School Student.
  2. In the dashboard, search "Python".
  3. Complete the tutorial + exercises.
  4. Monitor your progress; explore additional courses (Data Science, ML, AI fundamentals).

Useful Python Learning Links

📘 TutorialsProgramiz · W3Schools · GeeksforGeeks · LearnPython.org
📊 Pandas / NumPyAnalyticsVidhya · Pandas.pydata.org · scikit-learn.org
🎓 Free coursesKaggle Learn · Udemy · AI Student Community
📜 Official docsPython.org · Jupyter.org · NumPy.org

Quick Revision — Key Points to Remember

  • Python created by Guido van Rossum in 1991; named after Monty Python's Flying Circus.
  • Features: high-level · interpreted · free · cross-platform · easy syntax · ASCII+UNICODE.
  • Editors: IDLE · PyCharm · Anaconda · Spyder · Jupyter Notebook · Google Colab.
  • 5 tokens: Keywords · Identifiers · Literals · Operators · Punctuators (+ comments with #).
  • 7 operator types: Arithmetic · Relational · Assignment · Logical · Bitwise · Identity · Membership.
  • 9 data types: int · bool · float · complex · string · list · tuple · set · dictionary.
  • Mutable: list / set / dictionary. Immutable: string / tuple.
  • input() always returns string → type-cast with int() / float() / str() / bool().
  • Python uses indentation (4 spaces) to define code blocks.
  • Selection: if / elif / else.
  • Loops: for (known count, uses range()) · while (condition-based).
  • CSV = Comma-Separated Values; module is csv; methods: writerow, reader.
  • NumPy = Numerical Python. Core: ndarray. Install: pip install numpy.
  • Pandas = Panel Data / Python Data Analysis. Core structures: Series (1-D) and DataFrame (2-D). Install: pip install pandas.
  • DataFrame operations: add/drop rows & columns · loc[] (label) vs iloc[] (position) · isnull() · dropna() · fillna().
  • DataFrame attributes: index · columns · dtypes · values · shape · head() · tail().
  • Pandas & CSV: pd.read_csv() to import · df.to_csv() to export.
  • Scikit-learn = most popular ML library. Install: pip install scikit-learn.
  • ML pipeline: Load → Split (train_test_split, 80:20) → Train (fit) → Predict (predict) → Evaluate (accuracy_score).
  • Iris dataset: 150 flowers, 3 species (setosa/versicolor/virginica), 4 features (sepal & petal length/width).
  • KNN (K-Nearest Neighbours): supervised classification algorithm. n_neighbors=3 → uses 3 closest points to decide.
  • Certification: IBM SkillsBuild — Python for Data Science.
🧠Practice Quiz — test yourself on this chapter