0% found this document useful (0 votes)
118 views

Python Viva Q-ANS

NumPy arrays have several advantages over nested lists including being more memory efficient, offering faster computation, allowing element-wise operations and broadcasting, and providing many built-in mathematical functions. Pickling is the process of converting a Python object into a byte stream for storage, while unpickling converts the byte stream back into a Python object. Memory management in Python is handled through reference counting and a garbage collector, with memory optimization techniques also used. Arguments in Python are passed by reference, but immutable objects behave like pass by value.

Uploaded by

Mit Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Python Viva Q-ANS

NumPy arrays have several advantages over nested lists including being more memory efficient, offering faster computation, allowing element-wise operations and broadcasting, and providing many built-in mathematical functions. Pickling is the process of converting a Python object into a byte stream for storage, while unpickling converts the byte stream back into a Python object. Memory management in Python is handled through reference counting and a garbage collector, with memory optimization techniques also used. Arguments in Python are passed by reference, but immutable objects behave like pass by value.

Uploaded by

Mit Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PYTHON VIVA

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.

Python's memory management system also includes a concept called "memory


pooling," which reuses the memory previously allocated to objects. This improves the
performance of the memory allocation process by avoiding the overhead of frequently
requesting new blocks of memory.

Another important aspect of memory management in Python is the use of memory


optimization techniques such as "generators," "iterators," and "comprehensions."
These techniques allow for the efficient processing of large amounts of data without
having to load all of it into memory at once.

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.

4. Are Arguments in Python Passed by Value or by Reference?


In Python, arguments are passed by reference, but the exact behavior can depend on
the type of the argument being passed.

When we pass an immutable object like an integer, a string, or a tuple as an argument


to a function, it behaves like it is being passed by value. This is because these objects
cannot be modified, so a new object is created and passed to the function, which can
be accessed through the local parameter variable.

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.

However, it's important to note that in Python, everything is an object, including


integers and strings. When we pass an immutable object as an argument, we are
passing a reference to that object, not the actual object itself. Therefore, even in the
case of immutable objects, any changes made to the object inside the function will not
affect the original object, but rather will create a new object.
In summary, while arguments in Python are passed by reference, the behavior can
depend on the type of the argument being passed. Immutable objects behave like they
are passed by value, while mutable objects behave like they are passed by reference.

5. How Would You Generate Random Numbers in Python?


Python defines a set of functions that are used to generate or manipulate random
numbers through the random module.
Functions in the random module rely on a pseudo-random number generator
function random(), which generates a random float number between 0.0 and 1.0. These
particular type of functions is used in a lot of games, lotteries, or any application requiring
a random number generation.

6. What Is the Purpose of the Pass Statement?


The pass statement is used as a placeholder for future code. When the pass statement
is executed, nothing happens, but you avoid getting an error when empty code is not
allowed. Empty code is not allowed in loops, function definitions, class definitions,
or in if statements.

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.

8. What Is the Difference Between Del and Remove() on Lists?

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

9. How Do You Display the Contents of a Text File in Reverse Order?

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.

11. . What Is Docstring in Python?

Python documentation strings (or docstrings) provide a convenient way of


associating documentation with Python modules, functions, classes, and methods.
It’s specified in source code that is used, like a comment, to document a specific
segment of code. Unlike conventional source code comments, the docstring should
describe what the function does, not how.

12. How Do You Use the Split() Function in Python?

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.

13. Is Python Object-oriented or Functional Programming?

 Python is a multi-paradigm programming language that supports several programming


paradigms, including object-oriented programming (OOP) and functional programming
(FP).
 Python is primarily an object-oriented programming language, which means that it
allows you to define classes and create objects from those classes. Python's OOP features
include encapsulation, inheritance, and polymorphism.
 However, Python also supports functional programming concepts, such as first-class
functions, lambda expressions, and higher-order functions. Python's built-in functions,
such as `map()`, `filter()`, and `reduce()`, are examples of functional programming in
Python.
 In recent years, Python has gained popularity as a functional programming language as
well, especially in data science and machine learning domains. The functional
programming paradigm is well-suited for data processing and analysis tasks that involve
working with large datasets, as it provides a concise and expressive syntax for working
with complex data structures.
 So, in summary, Python supports both object-oriented and functional programming
paradigms, and developers can use either or both paradigms depending on their needs
and preferences.

14. Write a Function Prototype That Takes a Variable Number of Arguments.

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

15. What Is a Numpy Array?

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.

3. Matplotlib: Matplotlib is a plotting library for Python. It provides a wide range of


visualization options, including line plots, scatter plots, bar plots, histograms, and more.
Matplotlib allows you to create high-quality, publication-ready visualizations with
customizable settings and styles. It is also highly customizable, making it a popular choice for
creating bespoke visualizations that meet specific requirements.

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.

17. Discuss Django Architecture.

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.

5. Middleware: Middleware is a way to add additional functionality to the request/response


processing pipeline. Middleware components are executed in a sequence and can modify the
request/response objects or perform additional processing.
6. Form: Forms in Django are a way to define and validate user input. They provide a way to
define form fields, specify validation rules, and generate HTML forms. Django forms are
typically defined as Python classes that inherit from the `django.forms.Form` base class.

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.

18. Difference between tuples/list/dictionary/set

19. What is classes and constructor 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.

A constructor is a special method that is called when an object of a class is created. It is


used to initialize the object's attributes with default or user-defined values. In Python, the
constructor is defined using the `__init__()` method.

You might also like