Curriculum
Course: Grade XII AI
Login
Text lesson

Unit 1-Python Programming II

School Revise · Class 12 Artificial Intelligence · Unit 1

Python Programming II

Code 843, Class 12. AI needs strong Python and data skills. Here we use functions, NumPy and Pandas to handle data.

NumPy

number arrays

Pandas

data tables

What this unit is about

We build on Python with functions, and the NumPy and Pandas libraries that AI uses to work with numbers and data.

1. Functions and lists

A function is reusable code that can return a result. Lists and loops process many values, and a list comprehension builds a list in one line.

squares = [x * x for x in range(1, 4)] print(squares) Output: [1, 4, 9]

2. NumPy

NumPy works with arrays of numbers. np.array([…]) makes an array, and a.sum(), a.mean() and a.max() summarise it quickly.

3. Pandas

Pandas handles data in tables. A DataFrame is a table; df.groupby summarises groups and df[“col”].mean() gives an average, ready for analysis.

Practise with the interactive

Explore the idea by tapping. The interactive opens right here in the lesson.

Try it in the code lab

Write and run real Python right here in the lesson, then work through the practice problems with answers.

Starting Python...

Coding practice problems, with answers

Type each one into the code lab above, then open the card to see the worked solution and its output.

Problem 1. Write a function that adds two numbers.

SOLUTION

def add(a, b): return a + b print(add(5, 3))

OUTPUT

8
Problem 2. Build a list of squares in one line.

SOLUTION

squares = [x * x for x in range(1, 4)] print(squares)

OUTPUT

[1, 4, 9]
Problem 3. Make a NumPy array and sum it.

SOLUTION

import numpy as np a = np.array([1, 2, 3, 4]) print(a.sum())

OUTPUT

10
Problem 4. Find the mean and max with NumPy.

SOLUTION

import numpy as np a = np.array([2, 4, 6]) print(a.mean(), a.max())

OUTPUT

4.0 6
Problem 5. Make a Pandas DataFrame.

SOLUTION

import pandas as pd df = pd.DataFrame({“marks”: [80, 90, 70]}) print(df[“marks”].mean())

OUTPUT

80.0
Problem 6. Group and average with Pandas.

SOLUTION

import pandas as pd df = pd.DataFrame({“cls”: [“A”, “A”, “B”], “m”: [80, 90, 70]}) print(df.groupby(“cls”)[“m”].mean().tolist())

OUTPUT

[85.0, 70.0]
Problem 7. Filter rows in a DataFrame.

SOLUTION

import pandas as pd df = pd.DataFrame({“m”: [80, 40, 90]}) print(df[df[“m”] >= 50][“m”].tolist())

OUTPUT

[80, 90]
Problem 8. Sum a list with a loop.

SOLUTION

nums = [10, 20, 30] s = 0 for x in nums: s = s + x print(s)

OUTPUT

60
Problem 9. Handle a division error safely.

SOLUTION

try: print(10 / 0) except ZeroDivisionError: print(“cannot divide by zero”)

OUTPUT

cannot divide by zero
Problem 10. Count values in a Pandas column.

SOLUTION

import pandas as pd df = pd.DataFrame({“m”: [80, 90, 70]}) print(df[“m”].count())

OUTPUT

3

Practice set A, multiple choice

1. Which library works with number arrays?

NumPy.

2. Which handles data tables?

Pandas.

3. Which builds a list in one line?

A list comprehension.

4. Which Pandas method summarises groups?

groupby().

Quick summary

Python for AI uses functions (reusable code that returns a result), list comprehensions (build a list in one line), and libraries. NumPy works with arrays of numbers (sum, mean, max). Pandas handles data in tables: a DataFrame holds rows and columns, groupby summarises groups, and mean gives averages, ready for analysis.

Open the Virtual Lab

These free Class 12 Artificial Intelligence (Code 843) notes explain Python programming for AI, functions and list comprehensions, and the NumPy and Pandas libraries with groupby with clear examples and practice, for CBSE students across India and the Gulf including the UAE, Saudi Arabia, Qatar, Oman, Kuwait and Bahrain.

© 2026 School Revise. All rights reserved. Original content aligned to the CBSE and NCERT Class 12 Artificial Intelligence syllabus. Unauthorised copying is not permitted.

Layer 1
Login Categories