0% found this document useful (0 votes)
27 views8 pages

Ct3 QB Answers

Uploaded by

sornalakshmi22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views8 pages

Ct3 QB Answers

Uploaded by

sornalakshmi22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1) Write about Python, and why is it widely used in fields such as data science and web

development? List its key features.

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.

answers: 1. Python and Its Key Features

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.

Key Features of Python:

1. Easy to Learn: Simple syntax and readability.


2. Interpreted Language: No need for compilation; runs directly.
3. Dynamically Typed: No need to declare data types.
4. Extensive Libraries: Libraries for AI, ML, data science, web development, and
more.
5. Cross-Platform: Works on Windows, macOS, and Linux.
6. Object-Oriented and Functional: Supports multiple programming paradigms.

2. Python Program to Sort and Remove Duplicates


# Accept a list of numbers, sort it, and remove duplicates
numbers = list(map(int, input("Enter numbers separated by space:
").split()))
sorted_unique = sorted(set(numbers))
print("Sorted list without duplicates:", sorted_unique)

3. Difference Between List and Tuple

Aspect List Tuple


Mutable, ordered collection of Immutable, ordered collection of
Definition
elements. elements.
Syntax my_list = [1, 2, 3] my_tuple = (1, 2, 3)
Mutability Can be changed after creation. Cannot be changed after creation.
Performance Slower due to mutability. Faster due to immutability.

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

4. Key Attributes of a NumPy Array


• ndim: Number of dimensions of the array.
• shape: Tuple representing the size of the array along each dimension.
• dtype: Data type of array elements.

Example:

import numpy as np

arr = np.array([[1, 2], [3, 4]])


print("Array:", arr)
print("ndim:", arr.ndim) # Output: 2
print("shape:", arr.shape) # Output: (2, 2)
print("dtype:", arr.dtype) # Output: int64

5. Classify Students as Pass or Fail


import pandas as pd

# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Grade': [75, 40, 85]}
df = pd.DataFrame(data)

# Function to classify students


def classify_student(grade):
return "Pass" if grade >= 50 else "Fail"

df['Result'] = df['Grade'].apply(classify_student)
print(df)

6. Pandas Series vs DataFrame

Aspect Series DataFrame


Definition One-dimensional labeled array. Two-dimensional labeled data structure.
Example Usage Storing a column of data. Storing a table of data.

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

A set is an unordered collection of unique elements. It supports operations like union,


intersection, and difference.
Example:

# Create and manipulate sets


set_a = {1, 2, 3}
set_b = {3, 4, 5}

print("Union:", set_a | set_b)


print("Intersection:", set_a & set_b)
print("Difference:", set_a - set_b)

Differences:

• No duplicates.
• Unordered.

8. NumPy vs Pandas

(i) Concepts:

• NumPy: Focuses on numerical computations.


• Pandas: Focuses on data manipulation and analysis.

(ii) Create Examples:

import numpy as np
import pandas as pd

arr = np.array([1, 2, 3])


df = pd.DataFrame({'A': [1, 2, 3]})

(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.

9. While vs For Loop

• For: Iterate over a sequence.


• While: Repeats until a condition is false.

Example:
# For loop
for i in range(5):
print(i)

# While loop
i = 0
while i < 5:
print(i)
i += 1

10. Union, Intersection, and Difference of Sets


set1 = {"Alice", "Bob", "Charlie"}
set2 = {"Charlie", "David", "Eve"}

print("Union:", set1 | set2)


print("Intersection:", set1 & set2)
print("Difference:", set1 - set2)

11. Dictionary and Operations

A dictionary is a collection of key-value pairs.

# 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)

12. Percentile in NumPy


import numpy as np

data = [1, 2, 3, 4, 5]
print(np.percentile(data, 50)) # 50th percentile (median)

13. NumPy vs List Slicing


# NumPy slicing
arr = np.array([1, 2, 3, 4])
print(arr[1:3]) # Slices view

# Python list slicing


lst = [1, 2, 3, 4]
print(lst[1:3]) # Creates a new list

14. Sales Data DataFrame


df = pd.DataFrame({
'Product Name': ['A', 'B', 'C'],
'Quantity': [10, 20, 30],
'Price': [100, 200, 300]
})
print(df.head())

15. Comparison of Python Data Structures

Structure Definition Unique Features


List Ordered, mutable. Allows duplicates, dynamic resizing.
Tuple Ordered, immutable. Immutable, hashable.
Set Unordered, unique values. No duplicates, supports set operations.
Dictionary Key-value pairs. Keys are unique, fast lookups.

16. Popular Python Libraries

• NumPy: Numerical computations.


• Pandas: Data manipulation.
• Matplotlib: Data visualization.
• Seaborn: Statistical visualization.
• Scikit-learn: Machine learning algorithms.

17.2. Unique Features of Each Data Structure with Examples

List

1. Dynamic Size: Lists can grow or shrink dynamically.


2. Allows Duplicate Elements: Lists can store multiple occurrences of the same value.

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:

print(my_list[1]) # Output: 2 (access element at index 1)

Tuple

1. Immutable: Tuples cannot be modified after creation.


2. Hashable: Can be used as keys in a dictionary or elements in a set.

Adding Elements: Not applicable, as tuples are immutable.

Accessing Elements:
my_tuple = (1, 2, 3)
print(my_tuple[1]) # Output: 2

Set

1. Unique Elements: Automatically removes duplicates.


2. Supports Mathematical Operations: Union, intersection, difference, etc.

Adding Elements:

my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}

Accessing Elements: Sets do not support indexing as they are unordered.

Dictionary

1. Key-Value Pair: Stores data as key-value pairs.


2. Fast Lookups: Access values in constant time using keys.

Adding Elements:

my_dict = {'a': 1, 'b': 2}


my_dict['c'] = 3
print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}

Accessing Elements:

print(my_dict['b']) # Output: 2

18. Popular Python Libraries for Data Analysis and Visualization

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.]

You might also like