Q1. Explain mutable and immutable datatypes in Python with examples.
In Python, datatypes are classified into two main categories: **mutable** and **immutable**. This
distinction is very important because it defines how variables behave in memory and how data is
modified during program execution. **Immutable Datatypes:** Immutable objects cannot be
changed after creation. If we try to modify them, Python creates a new object instead of altering the
existing one. Examples include `int`, `float`, `string`, and `tuple`. For instance: ```python x = 10
print(id(x)) x = x + 5 print(id(x)) ``` The memory address changes because a new object is created.
Similarly, with strings: ```python s = "hello" s = s + " world" ``` The original string remains
unchanged, and a new one is created. Tuples also behave similarly because once created, their
elements cannot be altered. **Mutable Datatypes:** Mutable objects allow direct modifications
without creating new ones. Examples include `list`, `dict`, and `set`. For example: ```python lst = [1,
2, 3] print(id(lst)) lst.append(4) print(lst) print(id(lst)) ``` The ID remains the same because the list is
updated in place. Dictionaries and sets behave in the same way, where elements can be inserted,
deleted, or changed. **Practical Uses:** Immutable datatypes are used when security, stability, and
predictability are needed, such as keys in dictionaries or constants in code. Mutables are more
useful when handling collections of data that need updates, like datasets or objects in simulations.
**Applications:** For instance, immutable strings make Python safe for concurrent programming,
while mutable lists make operations like sorting, appending, and removing efficient. Developers
often use immutables when data must not change, while mutables are chosen when flexibility is
needed. **Conclusion:** Choosing between mutable and immutable datatypes depends on the
scenario. Immutable types provide safety and reliability, while mutable types provide efficiency and
flexibility in handling large, dynamic data structures.
Q2. Write a descriptive note on NumPy and Pandas libraries. Why are they
useful for graphics and data handling?
Python has become the backbone of data science, and two libraries—**NumPy** and
**Pandas**—play an essential role in handling and visualizing data. **NumPy:** NumPy (Numerical
Python) is designed for numerical computation. It introduces arrays and matrices that are faster and
more memory-efficient than Python lists. Operations on NumPy arrays are vectorized, avoiding
loops and speeding up computation. Example: ```python import numpy as np arr =
np.array([1,2,3,4]) print(arr*2) ``` This multiplies all elements by 2 without iteration. NumPy also
provides linear algebra, Fourier transforms, and random number capabilities. In graphics, images
are treated as arrays of pixels, making NumPy fundamental for image manipulation and filtering in
libraries like OpenCV. **Pandas:** Pandas builds on NumPy and provides two main data
structures: `Series` (1D) and `DataFrame` (2D). DataFrames allow easy tabular data
manipulation—filtering, grouping, and merging. Example: ```python import pandas as pd data =
{"Name":["A","B","C"], "Marks":[85,90,78]} df = pd.DataFrame(data) print(df) ``` Pandas is excellent
for managing structured datasets from CSV, Excel, or SQL. It integrates seamlessly with
visualization tools such as Matplotlib, allowing quick plotting directly from data. **Applications in
Graphics:** NumPy arrays can hold image pixel data, enabling scaling, rotations, and color
adjustments. Pandas is crucial in visualization when datasets need preprocessing before plotting
charts and graphs. Together, they form the foundation for machine learning pipelines, image
processing, and scientific research. **Conclusion:** NumPy provides speed and efficiency in
numerical computation, while Pandas adds powerful, user-friendly data structures. Both libraries
are indispensable in graphics, data handling, and modern computational tasks.