Subject – Artificial Intelligence
Class XII
Chapter - Python Programming II
Q. Define Numpy.
Ans. Numpy stands for Numerical Python. It is an essential and powerful library for numerical computing. It
provides functions for creating and manipulating arrays, which are optimized for mathematical operations.
Unlike lists and other sequence data types it allows fast calculations on large databases, making it a fundamental
tool in AI, Data Science, Machine Learning and Scientific Computing.
Q.2. Write Python code to create a 1D array from the values stored in a list.
Ans. import numpy as np
L = [5,15,20,30]
N = [Link](L)
print (N)
Output –
[5 15 20 30]
Q.3. Write a Python code to create a 2D array of 2X4 size.
Ans. import numpy as np
L = [ [5,15,25,30], [10,20,30,40] ]
N = [Link](L)
print(N)
Output -
[ [ 5 15 25 30]
[10 20 30 40]]
Q.4. What do you understand by indexing in numpy?
Ans. Indexing refers to position of elements of an array. Each element has a unique index number. Numpy uses
2 types of indexing-
Positive indexing (Forward indexing)
Negative indexing (Backward indexing)
In Forward indexing the first element of the array starts from (0) zero and the next value increases by 1.
Negative Indexing allows access to elements from the end of array. Last element starts from -1 and it decreases
further.
Q.5. What is array slicing in numpy?
Ans. Array slicing refers to extracting a specific portion of an array. The general syntax for array slicing is –
<array name>[ start : stop : step]
Start – The index from where slicing begins and the value is inclusive.
Stop - The index where slicing ends and the value is exclusive.
Step – The interval between specified elements. By default the step size is one.
For example –
import numpy as np
a = [Link](1,11)
print(a)
print(a[3:7])
Output –
[ 1 2 3 4 5 6 7 8 9 10]
[ 4 5 6 7]
Q.6. Write an example of 2D array slicing.
Ans. A 2D array is similar to matrix, where each element is identified using 2 indices – row index and column
index. To slice a 2D array we specify a slice for both row and column.
Syntax –
<array name>[row_start : row_stop : row_step, column_start : column_stop : column_step]
For example –
import numpy as np
arr = [Link]([ [ 10, 11, 12, 13], [4, 5, 6, 7], [21, 22, 23, 24]])
print(arr[1:3, 2:4])
Output –
[[6 7]
[23 24]]
Q.7. What do you understand by pandas library in Python?
Ans. Pandas is a popular Python library for data analysis. It has derived its name from “panel data system”,
which is an ecometrics term for multidimensional, structured data sets. It is built on top of numpy and extends
its functionality by providing high-level data structures and powerful tools for data manipulation, cleaning,
filtering, grouping, merging etc.
Q.8. What do you mean by data structure?
Ans. A data structure is a particular way of storing and organizing data in a computer to suit a specific purpose
so that it can be accessed and worked within appropriate ways. Python provides various data structures like
Series, DataFrames and Panels.
Q.9. Write difference between Series and DataFrame.
Ans. Difference between Series and DataFrame –
Property Series DataFrame
Dimensions 1 – dimensional 2 – dimensional
Homogeneous – all the elements must Heterogeneous – DataFrame object
Type of
be of same data type can have elements of different
Data
datatypes.
Mutability Value mutable Value mutable
Size immutable Size mutable
Q.10. Create the following Series with the indexes specified –
A 25
B 30
C 35
D 45
Ans. import pandas as pd
Ser = [Link]([25,30,35,45], index = [‘A’, ‘B’, ‘C’, ‘D’])
print(Ser)
Q.11. Create the following Dataframe from a dictionary. Also write commands to add a new column and
a row after creating the dataframe.
Marks Sport Students
0 79.5 Cricket Ruchika
1 83.7 Football Neha
2 74.0 Badminton Mark
3 88.5 Athletics Gurjyot
4 89.0 Kabaddi Jamaal
Ans. To create Dataframe -
import pandas as pd
d = {‘Marks’ : [79.5, 83.7, 74.0, 88.5, 89.0], ‘Sport’ : [‘Cricket’, ‘Football’, ‘Badminton’,
‘Athletics’, ‘Kabaddi’], ‘Students’ : [‘Ruchika’, ‘Neha’, ‘Mark’, ‘Gurjyot’, ‘Jamaal’]}
df = [Link](d)
print(df)
To add a new column
df[‘House’] = [‘B’, ‘D’, ‘S’, ‘T’]
To add a new row
[Link][5] = [88.0, ‘Squash’, ‘Samriddhi’, ‘D’]
Q.12. Write a Python code to delete a row and a column from the dataframe created in the previous
question.
Ans To delete a row with index position 2
>>>[Link](2)
To delete the column ‘House’
>>>[Link](‘House’, axis = 1)
Q.13. What are some important attributes of a Pandas Dataframe? Exaplain their purpose.
Ans. From book Page No. 165 (Long answer type questions ) [Link]. 1
Q. 14. What do you understand by csv format of files?
Ans. The acronym CSV is short for Comma-separated Values. The CSV format refers to a tabular data that has
been saved as plaintext where data is separated by commas. In AI and ML, CSV files are frequently used to
store datasets for training models. The pandas library is commonly used to work with CSV data.
Q.15. What are missing values? Why it is required to handle missing values?
Ans. Missing Values are the values that cannot contribute to any computation or we can say that missing values
are the values that carry no computational significance.
Data Science is all about inferring accurate results / predictions from the given data and if the missing values
are not handled properly, then it may affect the overall result and the researcher may end up drawing inaccurate
inference about the data. The performance of a predictive model based on data having mean values can hugely
be impacted if the missing values are high in number and are not appropriately handled.
Q.16. What are the different ways for handling missing values?
Ans. There are several ways by which you can handle missing values in DataFrames –
To detect missing values in a dataset isnull( )is used.
To delete an entire row containing NaN values dropna( ) function is used
fillna( ) function is used to replace missing values by the values specified,