100% found this document useful (4 votes)
21K views9 pages

Viva Questions For Python Lab

Python lab questions were summarized as follows: 1. Python is a general-purpose programming language that can be used for web development, games, science, image processing and more. 2. Lists are mutable while tuples are immutable; lists are slower. NumPy provides multi-dimensional arrays that allow fast mathematical operations while Pandas allows analysis of structured data. 3. Lambda functions allow small anonymous functions with one expression, reindexing changes indexes in Pandas, and vectorization applies NumPy operations to all elements for speed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (4 votes)
21K views9 pages

Viva Questions For Python Lab

Python lab questions were summarized as follows: 1. Python is a general-purpose programming language that can be used for web development, games, science, image processing and more. 2. Lists are mutable while tuples are immutable; lists are slower. NumPy provides multi-dimensional arrays that allow fast mathematical operations while Pandas allows analysis of structured data. 3. Lambda functions allow small anonymous functions with one expression, reindexing changes indexes in Pandas, and vectorization applies NumPy operations to all elements for speed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

VIVA QUESTIONS FOR PYTHON LAB

1.What is Python?
Python is a high-level, interpreted, general-purpose programming language. Being a
general-purpose language, it can be used to build almost any type of application with
the right tools/libraries.
2. What are the applications of Python?
Web and Internet Development
Games
Scientific and computational applications
Language development
Image processing and graphic design applications
3. What is the difference between a list and a tuple?

List:
Lists are enclosed with in square []
Lists are mutable, that is their elements and size can be changed.
Lists are slower than tuples.
Example: [‘A’, 1, ‘i’]
Tuple:
Tuples are enclosed in parentheses ()
Tuples are immutable i.e cannot be edited.
Tuples are faster than lists.
Tuples must be used when the order of the elements of a sequence matters.
Example: (‘Twenty’, 20, ‘XX’)
6.Define Lambda function?
A lambda function is a small anonymous function. A lambda function can takes any
no of arguments but can have only one expression.
.eg: x= lambda a:a+10
print(x)
5. What is the difference between del(), clear(), remove(), and pop()?
del(): deletes the with respect to the position of the value. It does not return which
value is deleted. It also changes the index towards the right by decreasing one
value. It can also be used to delete the entire data structure.
clear(): clears the list.
remove(): it deletes with respect to the value hence can be used if you know which
particular value to delete.
pop(): by default removes the last element and also returns back which value is
deleted. It is used extensively when we would want to create referencing. In sense,
we can store this deleted return value in a variable and use in future
6. What is the difference between pass, continue and break?
Pass: It is used when you need some block of code syntactically, but you want to
skip its execution. This is basically a null operation. Nothing happens when this is
executed.

Continue: It allows to skip some part of a loop when some specific condition is met,
and the control is transferred to the beginning of the loop. The loop does not
terminate but continues with the next iteration.

Break: It allows the loop to terminate when some condition is met, and the control of
the program flows to the statement immediately after the body of the loop. If the
break statement is inside a nested loop (the loop inside another loop), then the break
statement will terminate the innermost loop.
7. What is Regex? List some of the important Regex functions in Python.
Regular Expression or RegEx is a sequence of characters that are used to create
search patterns. In Python, the following RegEx functions are mostly used:

match(): it checks for a match only at the beginning of the string.


search(): it locates a substring matching the RegEx pattern anywhere in the string
sub(): searches for the pattern and replaces with a new value
split(): it is used to split the text by the given RegEx pattern.
findall(): it is used to find all the sub-strings matching the RegEx pattern
8. What is a class and object?
A class is a user-defined prototype which basically is a blueprint that defines the
nature of a future object. An object is an instance of the class. Therefore, classes
can construct instances of objects. This is known as instantiation.
9. What is a docstring?
Python document strings (or docstrings) describe what the function does. These are
within the triple quotes. Also, it can be accessed using two ways: one via _doc_
attribute and via by adding a period (.) next to the function name and pressing tab. It
is a way to associate documentation with Python modules, functions, classes, and
methods
10. How do map, reduce and filter functions work?
Map function applies the given function to all the iterable and returns a new modified
list. It applies the same function to each element of a sequence.

Reduce function applies the same operation to items of a sequence. It uses the
result of operations as the first param of the next operation. It returns an item and not
a list.

Filter function filters an item out of a sequence. It is used to filter the given iterable
(list, sets, tuple) with the help of another function passed as an argument to test all
the elements to be true or false. Its output is a filtered list.
NUMPY:

1.What is NumPy? Why should we use it?

NumPy (also called Numerical Python) is a highly flexible, optimized, open-source


package meant for array processing.

It provides tools for delivering high-end performance while dealing with N-


dimensional powerful array objects.

It is also beneficial for performing scientific computations, mathematical, and logical


operations, sorting operations, I/O functions, basic statistical and linear algebra-
based operations along with random simulation and broadcasting functionalities.

2.Where is NumPy used?

Answer:

NumPy is an open source numerical Python library. NumPy contains a multi-


dimentional array and matrix data structures. It can be utilised to perform a number
of mathematical operations on arrays such as trigonometric, statistical and algebraic
routines. NumPy is an extension of Numeric and Numarray.

3. Why is NumPy preferred over Matlab, Idl ?

Powerful functions for performing complex mathematical operations on multi-


dimensional matrices and arrays.
The operations on ndarrays of NumPy are approximately up to 50% faster than the
list in python. ·

Provides indexing syntax to access portions of data easily in a large array.

· Provides built-in functions ·

It takes only a few lines of code to achieve complex computations using NumPy

4. How are NumPy arrays better than Python’s lists?

Answer:

Python lists support storing heterogeneous data types whereas NumPy arrays can
store datatypes of one nature itself. NumPy provides extra functional capabilities that
make operating on its arrays easier which makes NumPy array advantageous in
comparison to Python lists as those functions cannot be operated on heterogeneous
data.

5. What are the types of sorting in numpy with syntax?

sorting types:-

sort - np.sort()

argsort - np.argsort()-returns index sorting value

lexsort-np.lexsort(a,b)-two array as given as argument and it returns indces


that describes the sort order by multiple columns

6. How is np.mean() different from np.average() in NumPy?

Answer:

· np.mean() method calculates the arithmetic mean and provides additional options
for input and results. For example, it has the option to specify what data types have
to be taken, where the result has to be placed etc.

· np.average() computes the weighted average if the weights parameter is specified.


In the case of weighted average, instead of considering that each data point is
contributing equally to the final average, it considers that some data points have
more weightage than the others (unequal contribution)

7. What are ndarrays in NumPy?

Answer:

· When the size of ndarrays is changed, it results in a new array and the original
array is deleted.
· The ndarrays are bound to store homogeneous data.

· They provide functions to perform advanced mathematical operations in an efficient


manner

8.What is numpy structured array?

Numpy's Structured Array is similar to Struct in C. It is used for grouping data of


different types and sizes. Structure array uses data containers called fields. Each
data field can contain data of any type and size.hat is structured array?

9. What is the difference between ndarray and array in NumPy?

Answer:

1. Using array(), zeros() or empty() methods: Arrays should be constructed using


array, zeros or empty (refer to the See Also section below). The parameters are
given here refer to a low-level method (ndarray(…)) for instantiating an array.

2. From ndarray class directly: There are two modes of creating an array using
__new__: If buffer is None, then only shape, dtype, and order are used. If buffer is
an object exposing the buffer interface, then all keywords are interpreted.

10. what is Vectorization in NumPy

Answer:

Vectorization takes one elemental operation and applies to all the elements in that
array for you. Underneath, the implementatiosn are in C, hence providing substantial
speed gains. NumPy already has a large number of operations vectorized, for eg: all
arithmetic operators, logical operators, etc. Numpy also provides a way for you to
vectorize your function.

PANDAS:
1) What is pandas in python?
Pandas is an open source python package that is most widely used for data
science/data analysis and machine learning tasks. It is built on top of another
package named Numpy which provides support for multi-dimensional arrays.
2) Mention the different types of Data Structures in Pandas?
1.Series
2.DataFrames
A Series is a one-dimensional data structure in pandas, whereas the DataFrame is
the two-dimensional data structure in pandas.
3) Define Reindexing in Pandas?

Reindexing is used to change the index of the rows and columns of the
DataFrame. We can reindex the single or multiple rows by using the reindex()
method. Default values in the new index are assigned NaN if it is not present
in the DataFrame.

DataFrame.reindex(labels=None, index=None, columns=None,


axis=None, method=None, copy=True, level=None, fill_value=nan,
limit=None, tolerance=None)

4) List out the features of Pandas Library?

  The pandas library has a number of features, some of which are shown here.

 Memory Efficient
 Time Series
 Reshaping
 Data Alignment
 Merge and join

5) How to create empty dataFrame in Pandas?

 To make a Pandas data frame that is fully empty, perform the following:
 
import pandas as pd 
MyEmptydf ()= pd.DataFrame
 
This will result in a data frame that has no columns or rows.
 
We do the following to construct an empty dataframe with three empty columns
(columns A, B, and C): df= pd.DataFrame(columns=['A', 'B', 'C'])

6) What type of inputs are accepted by pandas?

  Like Series, DataFrame accepts many different kinds of input:

 Dict of 1D ndarrays, lists, dicts, or Series.


 2-D numpy. ndarray.
 Structured or recorded ndarray.
 A Series.
 Another DataFrame.
7) What does axis in pandas?

axis=0 (or axis='rows' is horizontal axis. axis=1 (or axis='columns') is


vertical axis.

8) What is groupby() function in pandas?

In Pandas, groupby () function allows the programmers to rearrange


data by using them on real-world sets. The primary task of the function
is to split the data into various groups.

9) Define iloc() and loc() in pandas?

The iloc() function is an indexed-based selecting method which means that


we have to pass an integer index in the method to select a specific
row/column. This method does not include the last element of the range
passed in it unlike loc(). Iloc() does not accept the Boolean data unlike loc().

The loc() function is label based data selecting method which means that we
have to pass the name of the row or column which we want to select. This
method includes the last element of the range passed in it, unlike iloc(). loc()
can accept the Boolean data unlike iloc().

10)How to iterate over a Pandas DataFrame?


You can iterate over the rows of the DataFrame by using for loop in
combination with an iterrows() call on the DataFrame.
import pandas as pd
import numpy as np

df = pd.DataFrame([{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}])


for index, row in df.iterrows():
print(row['c1'], row['c2'])

Output:
10 100
11 110
12 120
MATPLOTLIB:
1.what are magic functions in matplotlib?
ans:%matplotlib notebook & %matplotlib inline
2. What is a Pairplot?
Ans:A Pair plot shows the relationships for a pair of columns of the data.
It creates a matrix of axes where the diagonals show the distribution of each variable
with itself.
3.what is use of markers in matplotplib?
ans:it is module which can be used in both scatter and plot.
It consist of facecolor,edgecolor,edgewidth etc..(circle,point,pixel,triangle-up,triangle-
down etc..,)
4.what is the module to import 3d matplotlib?
ans:from mpl_toolkits import mplot3d
5.what is legend and use of it?
ans:legend is the element to describe the graph and
the location can be given to place the legend ex:upperleft,upper roght,lower
left,lower right
6.what is cmap?
ans:cmap stands for colormap and it's a colormap instance or registered colormap
name
7.what is the use of magic function?
ans:Magic methods are most frequently used to define overloaded behaviours of
predefined operators in Python
8.what is the use of figure function?
ans:The figure() function in pyplot module of matplotlib library is used to create a
new figure
9.what are the arguments to be given in the linspace function?
ans: (range,collection of points).
10.what is the property of alpha?
ans:it used for color value transparancy.
11.what are the methods of visulaiztion tools in maytplotlib?
ans:plot(linear polt),scatterplot,contour polt,histogram,etc..

You might also like