Python Viva Q-ANS
Python Viva Q-ANS
1. What Advantage Does the Numpy Array Have over a Nested List?
Ans. NumPy arrays have several advantages over nested lists.
NumPy arrays are more memory-efficient than nested lists. This is because NumPy
arrays store data in contiguous blocks of memory, whereas nested lists store references to
objects, which requires more memory.
NumPy arrays offer faster computation than nested lists. This is because NumPy is
implemented in C and can take advantage of optimized libraries such as BLAS and
LAPACK for mathematical operations.
NumPy arrays allow for easy element-wise operations and broadcasting. For example,
adding two NumPy arrays of the same shape is a simple operation that can be done
efficiently.
NumPy arrays provide a large number of built-in functions for mathematical operations,
statistics, linear algebra, and more. This makes it easy to perform complex operations on
arrays without having to write custom code.
Overall, the advantages of NumPy arrays make them a preferred choice for numerical
computing tasks over nested lists.
2. What are Pickling and Unpickling?
Pickling and unpickling are serialization and deserialization processes, respectively, used in
Python to store and retrieve objects in a binary format.
Pickling is the process of converting a Python object into a byte stream, which can be stored
in a file or transferred over a network. This process is used to save the state of an object, so it
can be easily retrieved later on. Pickling is achieved using the `pickle` module in Python.
Unpickling, on the other hand, is the process of converting the byte stream back into a Python
object. This process is used to retrieve the state of the object that was previously pickled.
Unpickling is also done using the `pickle` module.
The pickling and unpickling processes can be useful when you need to save the state of an
object for future use, or when you need to transfer an object between different Python
programs.
It's worth noting that not all objects can be pickled, only objects that are pickleable, i.e.,
objects that can be serialized into a byte stream. Some examples of objects that cannot be
pickled are file objects, network sockets, and database connections, among others.
3. How is Memory managed in Python?
Memory management in Python is handled through a technique called reference
counting, which keeps track of the number of references to an object. When the
number of references to an object reaches zero, the memory occupied by the object is
automatically freed.
In addition to reference counting, Python also has a garbage collector that periodically
scans the memory for objects that are no longer referenced by any variable. When
such objects are found, they are marked as garbage and their memory is released.
Finally, Python provides a `sys` module that allows developers to control certain
aspects of memory management, such as the maximum size of the heap and the
behavior of the garbage collector.
On the other hand, when we pass a mutable object like a list or a dictionary as an
argument, it behaves like it is being passed by reference. This is because these objects
can be modified, so any changes made to them inside the function are reflected
outside the function as well.
7. How Would You Replace All Occurrences of a Substring with a New String?
The string type provides you with the replace() and replaceAll() methods that allow you to
replace all occurrences of a substring in a string and return the new version of the string.
Note that both method don’t change the original string, but return the new string with the
substrings replaced by a new substring.
Both `del` and `remove()` are used to remove elements from a Python list, but they differ in
the way they work.
`del` is a Python keyword used to delete an element or slice from a list by its index. It
modifies the original list in place and does not return any value.
`remove()` is a method of Python lists used to remove the first occurrence of a specified
element from the list. It modifies the original list in place and returns `None`. Here's an
example:
In summary, `del` is used to remove elements by index, while `remove()` is used to remove
elements by their value. If you want to remove all occurrences of a particular value from a list,
you can use a loop that iterates over the list and removes each occurrence of the value using
`remove()`.
To display the contents of a text file in reverse order, you can read the file into a list, reverse
the order of the list, and then print out the reversed list.
10. What Is the Difference Between a List and a Tuple?
In Python, both lists and tuples are used to store a collection of items, but they have some
differences in their properties and use cases.
The split() method is a built-in string method in Python that is used to split a string into a list
of substrings based on a specified separator. The syntax for the split() method is as follows:
string.split(separator, maxsplit)
where string is the string to be split, separator is the delimiter to be used for splitting the
string, and maxsplit is an optional integer that specifies the maximum number of splits to be
performed.
In Python, you can define a function that takes a variable number of arguments using the
*args syntax in the function definition. Here's an example function prototype:def
my_function(*args):
In this example, the function my_function() takes a variable number of arguments and
stores them in a tuple called args. You can pass any number of arguments to this function,
and they will be packed into a tuple automatically
A NumPy array is a data structure in the NumPy library of Python that represents a grid of
values, all of the same data type. They are a powerful and flexible tool for working with
numerical data in Python, and they are widely used in scientific computing, machine learning,
data analysis, and other fields that require fast and efficient numerical computations.
16. Discuss the use of pandas, numpy, matplotlib, and seaborn library.
Pandas, NumPy, Matplotlib, and Seaborn are some of the most widely used libraries in
Python for data analysis and visualization. Here's a brief overview of each library and its use:
1. Pandas: Pandas is a library for data manipulation and analysis. It provides data structures
for efficiently storing and manipulating large datasets, such as the DataFrame and Series.
Pandas is particularly useful for cleaning, preprocessing, and transforming data, as well as for
working with missing or incomplete data. It also has powerful capabilities for data
aggregation, grouping, and filtering.
2. NumPy: NumPy is a library for numerical computing in Python. It provides an array data
structure for efficient storage and manipulation of large numerical datasets, as well as a
collection of mathematical functions and operations for working with arrays. NumPy arrays
are faster and more memory-efficient than Python lists, making them a popular choice for
scientific computing, machine learning, and other applications that require large-scale
numerical calculations.
4. Seaborn: Seaborn is a library for statistical data visualization in Python. It is built on top of
Matplotlib and provides a higher-level interface for creating statistical graphics. Seaborn
includes several built-in themes and color palettes for creating aesthetically pleasing and
informative visualizations. It also has specialized functions for creating statistical plots, such
as distribution plots, regression plots, and categorical plots.
Together, these libraries form a powerful toolkit for data analysis and visualization in Python.
They are commonly used in scientific computing, data analysis, machine learning, and other
fields that require efficient and flexible data manipulation, numerical computing, and
visualization capabilities.
Django is a high-level web framework for Python that follows the Model-View-Controller
(MVC) architectural pattern, although it refers to it as Model-View-Template (MVT). Here's
a brief overview of the main components of Django architecture:
1. Model: The Model is the data access layer of Django. It defines the data structure of the
application and provides an interface for interacting with the database. Django models are
typically defined as Python classes that inherit from the `django.db.models.Model` base class.
2. View: The View is the business logic layer of Django. It processes incoming requests from
the client and returns responses, typically in the form of HTML pages. Views are defined as
Python functions or methods that take a request object as input and return a response object.
3. Template: The Template is the presentation layer of Django. It defines the structure and
layout of the HTML pages that are returned by the View. Django templates are written in a
markup language that allows for the dynamic insertion of data into the HTML page.
4. URL Dispatcher: The URL Dispatcher is responsible for mapping incoming requests to the
appropriate View function based on the requested URL pattern. It provides a way to define
URL patterns and associate them with View functions.
7. Admin Site: The Django Admin Site is a built-in application that provides an interface for
managing the database records through a web-based interface.
Overall, the Django architecture provides a clear separation of concerns between the data
access, business logic, and presentation layers of an application. This separation makes it
easier to develop, test, and maintain complex web applications in Python.
In Python, a class is a blueprint or template for creating objects, which are instances of
the class. A class defines a set of attributes and methods that are shared by all objects of
that class.