1.1 Features of Python
1.2 Python Editors — Jupyter Notebook
Installation & Running
# Install via pip (in admin mode command prompt) pip install notebook # Launch Jupyter Notebook from a terminal jupyter notebook
1.3 Tokens — Building Blocks of Python
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
| Type | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % | 5 + 2 = 7 |
| Relational | == != < > <= >= | 5 > 3 → True |
| Assignment | = += -= *= /= | x = 10; x += 5 → 15 |
| Logical | and or not | True and False → False |
| Bitwise | & | ^ << >> | 5 & 3 → 1 |
| Identity | is is not | a is b → True/False |
| Membership | in 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.
| Type | Description | Example |
|---|---|---|
| Integer (int) | Whole numbers. | a = 10 |
| Boolean (bool) | True / False values. | result = True |
| Float | Numbers with a fractional part. | x = 5.5 |
| Complex | Numbers with real + imaginary parts. | num = 3+2j |
| String (str) | Immutable sequence of characters. Single or double quotes. | name = "Ria" |
| List | Mutable sequence of comma-separated values in [ ]. | lst = [25, 15.6, "car", "XY"] |
| Tuple | Immutable sequence in ( ). | tup = (11, 12.3, "abc") |
| Set | Unordered, no duplicates, in { }. | s = {25, 3, 3.5} |
| Dictionary | Unordered key : value pairs in { }. | d = {1:"One", 2:"Two"} |
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
int() · float() · str() · bool() · list() · tuple().
Sample Program 1 — Display a string
print("National Animal - Tiger")
Sample Program 2 — Area of a rectangle
length = 50 breadth = 20 area = length * breadth print("Area of rectangle =", area)
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
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")
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])
2.1 CSV Files (Comma-Separated Values)
Common CSV Operations
| Task | Code |
|---|---|
| Import the CSV library | import csv |
| Open for reading | file = open("student.csv", "r") |
| Open for writing | file = open("student.csv", "w") |
| Close a file | file.close() |
| Write a row | wr = csv.writer(file); wr.writerow([12, "Kalesh", 480]) |
| Read all rows | for 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 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().
2.3 NumPy (Numerical Python)
Why Use NumPy in AI?
Suppose you have exam scores for students in multiple subjects. NumPy arrays let you efficiently:
- Calculate average scores per subject.
- Find total scores per student.
- Compute the overall average across all subjects.
- Identify the highest and lowest scores.
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)
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)
Common DataFrame Operations
| Task | Code |
|---|---|
| Add a new column | df["NewCol"] = [1, 2, 3] |
| Add a new row | df.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 label | df.loc["Maths"] |
| Access by index position | df.iloc[0] |
Handling Missing Values (NaN)
| Task | Code |
|---|---|
| Detect missing values | df.isnull() |
| Any missing in a column? | df["Music"].isnull().any() |
| Total number of NaN | df.isnull().sum() |
| Drop rows with NaN | df.dropna() |
| Fill NaN with a value | df.fillna(1) |
DataFrame Attributes
| Attribute | What it returns |
|---|---|
df.index | Row labels (index). |
df.columns | Column labels. |
df.dtypes | Data type of each column. |
df.values | Data 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=",")
2.5 Scikit-learn (sklearn) — Machine Learning in Python
Key Features
- Wide range of supervised and unsupervised algorithms.
- Tools for model selection, evaluation, validation.
- Supports classification, regression, clustering, dimensionality reduction.
- Integrates seamlessly with NumPy, SciPy, Pandas.
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)
| Parameter | Meaning |
|---|---|
| X_train, y_train | Features + labels for training (80 %). |
| X_test, y_test | Features + labels for testing (20 %). |
| test_size = 0.2 | 20 % data used for testing. |
| random_state = 1 | Fixes 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)
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
- Visit skillsbuild.org → sign up as a High School Student.
- In the dashboard, search "Python".
- Complete the tutorial + exercises.
- Monitor your progress; explore additional courses (Data Science, ML, AI fundamentals).
Useful Python Learning Links
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.