Ct3 QB Answers
Ct3 QB Answers
2) Write a program to accept a list of numbers from the user. Sort the list into ascending
order and remove any duplicates.
3) Differentiate list from tuples with appropriate examples.
4) Brief the key attributes of a NumPy array? Explain the purpose of ndim, shape, and
dtype.
5) You have a DataFrame representing student grades. Write a function to classify students
as "Pass" or "Fail" based on their grades and apply it to the DataFrame.
6) Explain the differences between a Pandas Series and a Pandas DataFrame. Provide
examples of when each would be used.
7) Describe the concept of sets in Python. How do you create and manipulate sets, and
what makes them different from lists and tuples?
8) Discuss the key differences between NumPy and Pandas, focusing on their structures,
functionalities, and performance. Include the following aspects in your answer:
(i)Describe the fundamental concepts behind NumPy and Pandas libraries in Python.
(ii)Explain the process of creating a NumPy array and a Pandas DataFrame. Provide
examples of syntax.
(iii)Provide examples of slicing in both libraries and explain when each method is
preferred.
(iv) Discuss the speed performance of NumPy and Pandas in handling large datasets.
9) Explain the differences between a WHILE loop and a FOR loop in python. When would
you choose one over the other?
10) Write a Python program to find the union, intersection, and difference between two sets
of student names.
11) Brief dictionary and it’s any 5 operations with examples.
12) What is a percentile? How can it be calculated using NumPy’s percentile() function?
13) How does slicing in NumPy differ from slicing in Python lists? Explain with examples.
14) Create a DataFrame to represent sales data for a store (Product Name, Quantity, Price).
Display the first five rows of the DataFrame.
15) Compare the following Python data structures: Set, List, Tuple, and Dictionary. Your
answer should include:
16) 1.Definition and a brief description of each data structure.
17) 2.At least two unique features of each data structure with example.
• Adding elements (if applicable).
• Accessing elements (if applicable).
Explain some popular Python libraries for data analysis and visualization.
Describe their use cases briefly.
Python is a high-level, general-purpose programming language known for its simplicity and
readability. It is widely used in fields like data science and web development due to its rich
ecosystem of libraries and frameworks, versatility, and ease of use.
Reasons for Python's Popularity:
• Data Science: Python has powerful libraries like NumPy, Pandas, and Matplotlib
for data analysis, manipulation, and visualization.
• Web Development: Frameworks like Django and Flask make building web
applications easier.
• Community Support: Python has a vast community and extensive documentation.
Example:
# List
my_list = [1, 2, 3]
my_list[0] = 10 # Mutable
print(my_list) # Output: [10, 2, 3]
# Tuple
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # Raises an error
Example:
import numpy as np
# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Grade': [75, 40, 85]}
df = pd.DataFrame(data)
df['Result'] = df['Grade'].apply(classify_student)
print(df)
Example:
import pandas as pd
# Series
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
print(s)
# DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
print(df)
7. Sets in Python
Differences:
• No duplicates.
• Unordered.
8. NumPy vs Pandas
(i) Concepts:
import numpy as np
import pandas as pd
(iii) Slicing:
# NumPy slicing
print(arr[1:3])
# Pandas slicing
print(df.iloc[0:2])
(iv) Performance:
NumPy is faster for numerical computations, while Pandas excels at handling tabular data.
Example:
# For loop
for i in range(5):
print(i)
# While loop
i = 0
while i < 5:
print(i)
i += 1
# Dictionary example
my_dict = {'a': 1, 'b': 2}
print("Keys:", my_dict.keys()) # Keys
print("Values:", my_dict.values()) # Values
my_dict['c'] = 3 # Add
print(my_dict)
data = [1, 2, 3, 4, 5]
print(np.percentile(data, 50)) # 50th percentile (median)
List
Adding Elements:
my_list = [1, 2, 3]
my_list.append(4) # Add to the end
print(my_list) # Output: [1, 2, 3, 4]
Accessing Elements:
Tuple
Accessing Elements:
my_tuple = (1, 2, 3)
print(my_tuple[1]) # Output: 2
Set
Adding Elements:
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
Dictionary
Adding Elements:
Accessing Elements:
print(my_dict['b']) # Output: 2
1. NumPy:
o Use Case: Efficient numerical computations, handling multi-dimensional
arrays, and performing linear algebra.
o Example: Calculating the mean of an array.
2. import numpy as np
3. data = np.array([1, 2, 3, 4])
4. print(np.mean(data)) # Output: 2.5
5. Pandas:
o Use Case: Data manipulation, cleaning, and analysis. Works well with tabular
data (DataFrames).
o Example: Reading a CSV file and summarizing data.
6. import pandas as pd
7. df = pd.read_csv('data.csv')
8. print(df.describe())
9. Matplotlib:
o Use Case: Creating static, interactive, and animated visualizations (e.g., line
plots, bar charts).
o Example: Plotting a simple line chart.
10. import matplotlib.pyplot as plt
11. plt.plot([1, 2, 3], [4, 5, 6])
12. plt.show()
13. Seaborn:
o Use Case: Statistical data visualization with advanced features like heatmaps,
pair plots, and violin plots.
o Example: Creating a heatmap.
14. import seaborn as sns
15. import pandas as pd
16. data = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
17. sns.heatmap(data, annot=True)
18. Plotly:
o Use Case: Interactive and web-based visualizations (e.g., dashboards).
o Example: Creating an interactive scatter plot.
19. import plotly.express as px
20. df = px.data.iris()
21. fig = px.scatter(df, x="sepal_width", y="sepal_length",
color="species")
22. fig.show()
23. Scikit-learn:
o Use Case: Machine learning tasks like classification, regression, clustering,
and preprocessing.
o Example: Implementing a simple linear regression model.
24. from sklearn.linear_model import LinearRegression
25. model = LinearRegression()
26. model.fit([[1], [2], [3]], [1, 2, 3])
27. print(model.predict([[4]])) # Output: [4.]