Dec 23 py
Dec 23 py
Answer Key
Section A
1. d. a.title()
2.b. 2
3. a. Categorical data
4.c. 3
5.d. DataFrame
7.b. type()
9. a. 15
Section B
Tuple unpacking is a feature of Python that allows you to assign the elements of
a tuple to separate variables in a single statement. This is achieved by using the
assignment operator (=) followed by the tuple you want to unpack. The number
of variables on the left-hand side of the assignment operator must match the
12. What is a NumPy array, and how does it differ from a Python list? 2 Marks
• It is basically a table of elements which are all of the same type and
indexed by a tuple of positive integers.
2. Multidimensional Array:
Data in multidimensional arrays are stored in tabular form
Homogeneous Data Type: In a NumPy array, all elements are of the same
data type, unlike Python lists, which can contain elements of different
data types. This homogeneity allows for more efficient memory storage
and mathematical operations.
Answer:
Answer:
16.
a) Compare and contrast the list and set data structures in Python with
examples.
5 Marks
Answer:Any 5 Points- 1 mark each
List Set
The list allows duplicate elements The set doesn’t allow duplicate elements.
b) Write Python code to open a file, read its contents, write any sentence, and
print them to the console. 5 Marks
Answer: Answer should contain open() function in read and write mode.
17.
a) List out some common types of plots that Matplotlib can create, and explain
any two of them in brief. 5 Marks
Answer:List out plots-1 mark, explanation of any 2 plots-2 marks each
● common types of plots that Matplotlib can create:
1. Line Plot:
2. Scatter Plot
3. Bar Plot
4. Histogram
5. Pie plot
6. Area plot
1. Line Plot:
Line plots are drawn by joining straight lines connecting data points where the x-
axis and y-axis values intersect. Line plots are the simplest form of representing
data. In Matplotlib, the plot() function represents this.
Example:
import matplotlib.pyplot as pyplot
Program Output:
Bar Plot
The bar plots are vertical/horizontal rectangular graphs that show data
comparison where you can gauge the changes over a period represented in
another axis (mostly the X-axis). Each bar can store the value of one or multiple
data divided in a ratio. The longer a bar becomes, the greater the value it holds.
In Matplotlib, we use the bar() or bar() function to represent it.
Example:
import matplotlib.pyplot as pyplot
pyplot.bar([0.25,2.25,3.25,5.25,7.25],[300,400,200,600,700],
lab_l="Carpenter",color='b',width=0.5)
pyplot.bar([0.75,1.75,2.75,3.75,4.75],[50,30,20,50,60],
label="Plumber", color='g',width=.5)
pyplot.legend()
pyplot.xlabel('Days')
pyplot.ylabel('Wage')
pyplot.title('Details')
Example:
import matplotlib.pyplot as pyplot
x1 = [1, 2.5,3,4.5,5,6.5,7]
y1 = [1,2, 3, 2, 1, 3, 4]
x2=[8, 8.5, 9, 9.5, 10, 10.5, 11]
y2=[3,3.5, 3.7, 4,4.5, 5, 5.2]
pyplot.scatter(x1, y1, label = 'high bp low heartrate', color='c')
pyplot.scatter(x2,y2,label='low bp high heartrate',color='g')
pyplot.title('Smart Band Data Report')
pyplot.xlabel('x')
pyplot.ylabel('y')
pyplot.legend()
# Print the chart
pyplot.show()
Program Output:
Pie Plot
A pie plot is a circular graph where the data get represented within that
components/segments or slices of pie. Data analysts use them while
representing the percentage or proportional data in which each pie slice
represents an item or data classification. In Matplotlib, the pie() function
represents it.
Example:
import matplotlib.pyplot as pyplot
slice = [12, 25, 50, 36, 19]
activities = ['NLP','Neural Network', 'Data analytics', 'Quantum Computing',
'Machine Learning']
cols = ['r','b','c','g', 'orange']
pyplot.pie(slice,
labels =activities,
colors = cols,
startangle = 90,
shadow = True,
explode =(0,0.1,0,0,0),x
autopct ='%1.1f%%')
pyplot.title('Training Subjects')
pyplot.show()
Program Output:
b) Explain Dictionary data type in Python. How to create, access, and modify
dictionary elements?
5 Marks
Answer:Dictionary explanation: 2 marks, create, access, and modify
dictionary elements: 1 mark each.
Dictionary:
• Python dictionary is an unordered collection of elements.
• The data is stored as key-value pairs using a Python dictionary.
• This data structure is mutable.
• Keys must only have one component.
• Values can be of any type, including integer, list, and tuple.
• Eg.
d = {1: 500, 'name': 'Akash', 'roll': 120, 'city': 'Pune', 0: 100, 2: 'Hello'}
● While indexing is used with other data types to access values, a dictionary
uses keys. Keys can be used inside square brackets []
● Dictionaries are mutable. We can add new elements or change the value
of existing elements using an assignment operator.
● If the key is already present, then the existing value gets updated. In case
the key is not present, a new (key: value) pair is added to the dictionary.
18.
a) Develop a Python program that implements a function that calculates the
factorial of a given number. 5 Marks
Answer:
Any logic that is applicable can be used to calculate the factorial of a number,
but it should be in a function only.
Program:
y\x -2 4
1 0.1 0.1
-3 0.2 0.4
5 0.1 0.1
i) Marginal Distribution of y is
y 1 -3 5
h(y) 0.2 0.6 0.2
(1 Mark)
ii) As P(-2,1) =0.1≠ P(x=-2).P(y=1)=0.4*0.2=0.08, x and y are not independent
(2mark)
iii) P(Y=5/X=4)= f(4,5)/g(4)=0.1/0.6=1/6 (2 Marks )
19.
a) Explain different NumPy attributes. 5 Marks
1. shape:
The shape attribute returns a tuple representing the dimensions (size) of the
NumPy array. For a 1D array, it returns the number of elements; for a 2D array, it
returns a tuple with the number of rows and columns, and so on.
import numpy as np
2. dtype:
The dtype attribute returns the data type of the elements in the NumPy
array.
import numpy as np
arr = np.array([1, 2, 3])
print(arr.dtype) # Output: int64
3. size:
The size attribute returns the total number of elements in the array.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.size) # Output: 6
4. ndim:
The ndim attribute returns the number of dimensions (axes) in the array.
import numpy as np
arr = np.array([1, 2, 3])
print(arr.ndim) # Output: 1 (1D array)
5. type():
Prints type of array object
20.
a. Suppose you have a dataset representing the test scores (out of 100) of a
group of students in a math class. The scores are as follows: 85, 92, 78, 88, 95,
90, 82, and 89. Calculate the mean and standard deviation of these test scores
5 Marks
Answer:---
(xi - )² 6.39 + 21.20 + 88.39 + 0.38 + 58.92 + 6.90 + 29.09 + 2.62 213.09 (1 mark)
So, the standard deviation of the test scores is approximately 5.16. (1 mark)
Pandas is a popular Python library for data manipulation and analysis. It provides
two main key data structures for working with structured data: Series and
DataFrame. These data structures are built on top of NumPy arrays and offer
powerful and flexible ways to work with data. Here's an explanation of each:
1.Series:
import pandas as pd
res = pd.Series(list6)
print(res)
Output:
2. DataFrame:
DataFrame is the most commonly used Pandas data structure, and it's suitable
for a wide range of data analysis tasks.
Example:
import pandas as pd
# list of strings
df = pd.DataFrame(l)
display(df)
Output:
21.
a. Write Python statements that create an empty list, an empty tuple, an empty
set, an empty dictionary, and an empty string. 5 marks
empty list:
l=[]
an empty tuple:
t=()
an empty set:
empty_set=set()
an empty dictionary:
d={}
an empty string:
s=” “
( )
th
9
marks of Mathematics is 44 and variance of marks in Python is of the
16
variance of marks in Mathematics. Find the mean marks in Python programming
and the coefficient of correlation between marks in two subjects.
5 Marks
Answer:
n= 50 ,Y =44
2 9 2 σx 3
σ x = σ y =¿ = ____________ 1M
16 σy 4
Given regression equation X on Y is 3Y – 5X + 180 = 0
3
X = Y +36.
5
σx 3
And b XY =r =
σy 5
r = 0.8. _____________2M
the regression line pass through the point ( X , Y ) ,
X =62.4 ________2M.
22.
a) Compare and contrast the use of central tendency measures and dispersion
measures in Exploratory Data Analysis. 5 marks
Answer:
While loop:
while expression:
statements
Sample Example :
# Python program to illustrate while loop
count = 0
while (count < 3):
count = count + 1
print("Python")
Output :
Python
Python
Python
for loop :
Python For loop is used for sequential traversal i.e. it is used for iterating over an
iterable like String, Tuple, List, Set or Dictionary.
Loop continues until we reach the last item in the sequence. The body of for loop
is separated from the rest of the code using indentation.
Sample Example :
# Python program to illustrate Iterating over a list
l = [10,20,30]
for i in l:
print(i)
Output :
10
20
30