Unit 4: Programming for AI
Master ASSEB Class XI AI Unit 4: Python fundamentals, Jupyter, Google Colab, Kaggle, NumPy, Pandas, Matplotlib, and textbook code errata.
Unlock Unit 4: Programming for AI
Access verified code errata manual, 50 external viva questions, period lesson scripts, and practical examination rubrics.
Class XI • Unit 4 Exercise Solutions
Human-crafted academic answers for all exercise questions (Pages 26–29) formulated to secure full marks in the ASSEB Board Examinations. Includes step-by-step calculations, diagrams, and examiner scoring tips.
Which built-in function in Python outputs text to the console screen?
What is the data type returned by type(3.14) in Python?
Which sequence data structure in Python is MUTABLE?
What is the primary Python library used for reading, cleaning, and manipulating tabular data frames?
What does the floor division operator // compute in Python?
What is the difference between a variable and a constant in Python?
A variable is a named location in memory whose value can be reassigned during program execution (e.g., score = 10; score = 20). A constant represents a fixed value that is not intended to change throughout execution (conventionally named in ALL_CAPS in Python, e.g., PI = 3.14159).
Explain the purpose and importance of indentation in Python.
Unlike languages like C++ or Java that use curly brackets {} to delimit code blocks, Python uses whitespace indentation (conventionally 4 spaces) to define the structural scope of conditional blocks, loops, and function definitions. Inconsistent indentation causes an IndentationError.
Differentiate between a for loop and a while loop with code examples.
for loop: Used when iterating over a known sequence or collection of elements.for x in range(3): print(x) (Prints 0, 1, 2)
while loop: Repeats indefinitely as long as a Boolean condition remains True.count = 0while count < 3: count += 1
What are NumPy arrays and why are they preferred over standard Python lists in AI?
NumPy arrays (ndarray) store homogeneous data elements in contiguous memory blocks, enabling vectorized, low-level C-compiled arithmetic operations without Python loop overhead. They are orders of magnitude faster, consume significantly less memory, and support multi-dimensional matrix operations essential for neural networks.
Write a complete Python program using Pandas and Matplotlib to: (1) Create a DataFrame of 3 students with columns 'Name', 'Attendance', and 'Marks', (2) Calculate the average Marks, and (3) Plot a bar chart of Names versus Marks.
import pandas as pdimport matplotlib.pyplot as plt# 1. Create student datasetdata = { 'Name': ['Ananya', 'Bikash', 'Chayanika'], 'Attendance': [92, 85, 96], 'Marks': [78, 88, 92]}df = pd.DataFrame(data)print("--- Student DataFrame ---")print(df)# 2. Calculate average marksavgmarks = df['Marks'].mean()print(f"Average Marks: {avgmarks:.2f}")# 3. Plot Bar Chartplt.bar(df['Name'], df['Marks'], color='teal')plt.xlabel('Student Name')plt.ylabel('Marks Scored (out of 100)')plt.title('Student Academic Performance in AI')plt.ylim(0, 100)plt.show()
Explain Python file handling. Detail the modes ('r', 'w', 'a'), the purpose of the with context manager, and demonstrate how to read and write a JSON file.
File Modes: 'r' opens an existing file for reading; 'w' opens for writing (overwriting existing content); 'a' opens for appending new data to the end of the file.
The with Context Manager: Automatically guarantees that file descriptors are released and the file is safely closed when the indented block finishes, even if an unhandled runtime exception occurs.
JSON File Handling Example:
import json# Writing to JSONconfig = {'learningrate': 0.01, 'batchsize': 32, 'model': 'CNN'}with open('modelconfig.json', 'w') as f: json.dump(config, f, indent=4)# Reading from JSONwith open('modelconfig.json', 'r') as f: loadedconfig = json.load(f)print("Loaded Learning Rate:", loadedconfig['learning_rate'])