Skip to main content

Set B - Question 1

1. a) Consider the following NumPy array containing sensor readings:

import numpy as np
temperature = np.array([22.5, 30.1, 40.0, 18.9, 25.4, 40.0, 35.2])

Demonstrate the following array operations: i. Display the shape, size, and data type of the array. ii. Find the minimum, maximum, and average temperature. iii. Replace the reading 40 with 38. iv. Sort the temperature readings in ascending order. v. Explain the benefit of NumPy arrays for sensor-data processing.

Answer

i. Display the shape, size, and data type of the array

import numpy as np
temperature = np.array([22.5, 30.1, 40.0, 18.9, 25.4, 40.0, 35.2])

print("Shape:", temperature.shape) # (7,)
print("Size:", temperature.size) # 7
print("Data Type:", temperature.dtype) # float64

ii. Find the minimum, maximum, and average temperature

# Statistical operations using NumPy built-ins
min_temp = np.min(temperature)
max_temp = np.max(temperature)
avg_temp = np.mean(temperature)

print(f"Min: {min_temp}, Max: {max_temp}, Avg: {avg_temp:.2f}")

iii. Replace the reading 40 with 38

# Boolean indexing to replace elements matching a condition
temperature[temperature == 40.0] = 38.0
print("Modified readings:", temperature) # [22.5, 30.1, 38.0, 18.9, 25.4, 38.0, 35.2]

iv. Sort the temperature readings in ascending order

# Returns a sorted copy of the array
sorted_temperature = np.sort(temperature)
print("Sorted readings:", sorted_temperature)

v. Explain the benefit of NumPy arrays for sensor-data processing

  • Memory & Speed Efficiency: High-frequency streams of sensory floating points are processed in highly optimized, contiguous memory blocks instead of slow Python pointer lists.
  • Spike/Outlier Removal: Boolean masks quickly locate and filter noise or sensor errors (e.g., temp[temp > 100] = np.nan) instantly.
  • Easy Scaling/Calibration: Broadcasting allows shifting or scaling values simultaneously (e.g., temp_celsius = temp_fahrenheit - 32) without writing any loops.

1. b) Consider the following student list:

(Assume: student_list = ["Alice", "Bob", "Charlie", "David", "Eva"]) Answer the following: i. Traverse the list using a for loop and display each student name. ii. Check whether "Cathy" and "Rahul" are present using membership operators. iii. Add "Farah" to the list and insert "Gokul" at the second position. iv. Create a cloned copy of the list using slicing or copy(). v. Explain the differences between list aliasing and list cloning with respect to modifying the copied list.

Answer

i. Traverse the list using a for loop and display each student name

student_list = ["Alice", "Bob", "Charlie", "David", "Eva"]

for student in student_list:
print(student)

ii. Check whether "Cathy" and "Rahul" are present using membership operators

# Using membership operator 'in'
for name in ["Cathy", "Rahul"]:
if name in student_list:
print(f"'{name}' is present.")
else:
print(f"'{name}' is NOT present.")

iii. Add "Farah" to the list and insert "Gokul" at the second position

# append() adds Farah to the end
student_list.append("Farah")

# insert(index, item) places Gokul at index 1 (second position)
student_list.insert(1, "Gokul")
print("Modified list:", student_list)

iv. Create a cloned copy of the list using slicing or copy()

# Way 1: Slicing
cloned_list_slice = student_list[:]

# Way 2: copy() method
cloned_list_copy = student_list.copy()

print("Cloned (Slice):", cloned_list_slice)

v. Explain the differences between list aliasing and list cloning with respect to modifying the copied list

  • List Aliasing: Assigns list reference directly using = (e.g., alias = original). No new object is created (id(alias) == id(original)). Modifying the alias instantly alters the original list.
  • List Cloning: Creates a brand-new object in memory using slicing [:] or copy(). Modifying elements or length in the cloned list does not affect the original list.

1. c) Differentiate between List, Tuple, Set, and Dictionary in Python based on ordering, mutability, duplicates, and method of storing data. (Provide a quick, easy-to-draw comparative matrix/table).

Answer

FeatureList (list)Tuple (tuple)Set (set)Dictionary (dict)
OrderingOrdered (insertion order)Ordered (insertion order)Unordered (no sequential order)Ordered (insertion order of keys since Python 3.7+)
MutabilityMutableImmutableMutable (elements must be immutable/hashable)Mutable (keys are immutable/hashable)
DuplicatesAllowedAllowedNot Allowed (duplicates filtered out)Duplicate Keys: Not Allowed
Duplicate Values: Allowed
Storage MethodContiguous references indexed by integersContiguous references in fixed, read-only memoryHash Table of unique valuesHash Table of Key-Value pairs (key: value)
Syntax[1, 2, 3](1, 2, 3){1, 2, 3}{"key": "value"}