Q1. What is module? How we create module in python?
Ans. A module is simply a Python file with a .py extension that can be imported
inside another Python program.
To create a module just save the code you want in a file with the file extension .py
Save this code in a file named mymodule.py
def greeting(name):
print("Hello, " + name)
Now we can use the module we just created, by using the import statement
import mymodule
mymodule.greeting("Jonathan")
Q2. Explain any five functions available in the following module:
a. Math module-
pow(x, y) Returns x raised to the power y
sqrt(x) Returns the square root of x
sin(x) Returns the sine of x
tan(x) Returns the tangent of x
exp(x) Returns e**x
b. Random module-
randrange(start, stop[,
Returns a random integer from the range
step])
randint(a, b) Returns a random integer between a and b inclusive
Return the next random floating point number in the range
random()
[0.0, 1.0)
Return a random floating point number between a and b
uniform(a, b)
inclusive
randint(a, b) Returns a random integer between a and b inclusive
choice(seq) Return a random element from the non-empty sequence
Q3. Differentiate between internal sorting and external sorting with example.
In internal sorting the data that has to be sorted will be in the main memory
always, implying faster access. Complete sorting will happen in main memory.
Insertion sort, quick sort, heap sort, radix sort can be used for internal sorting.
In external sorting it will on disks, outside main memory. It can be because the
data is huge and cannot be stored in main memory. While sorting the data will
pulled over in chunks from disk to main memory. Later all the sorted data will be
merged and stored back to disk, where it can fit. External merge sort can be used
here.
Q4. Write a python program for bubble sort. Also find its time complexity.
Ans.
L=[]
n=int(input(“enter the size of list”)
for i in range(0,n):
l.append(int(input(“enter the value”)))
for j in range(n-1):
for k in range(n-1-j):
if l[k]>l[k+1]:
t=l[k]
l[k]=l[k+1]
l[k+1]=t
print(“sorted list is “,l)
Time Complexity: O(N2)
Q5. What is searching? Write algorithm for binary search. Why we prefer binary
search over linear search? Explain.
Searching algorithms are used to retrieve or search any element from a given data
structure set.
1. Sort the list of elements.
2. Find the middle element in the sorted list.
3. Compare the key with the middle element
4. If the key matches the middle element, print message key is found and the
execution is stopped.
5. Else, if the key is greater than the middle element, the key is searched for in
the right sublist and starts again with the step 3.
6. Else, if the key is smaller than the middle element, the key is searched for in
the left sublist and starts again with step 3.
7. If the key is still not matched, return the message key that was not found.
The main advantage of using binary search is that it does not scan each element
in the list. Instead of scanning each element, it performs the searching to the half
of the list. So, the binary search takes less time to search an element as compared
to a linear search.
Q6. Explain the following:
a. Mathplotlib-Matplotlib is a python library used to create 2D graphs and
plots by using python scripts. It has a module named pyplot which makes
things easy for plotting by providing feature to control line styles, font
properties, formatting axes etc. It supports a very wide variety of graphs
and plots namely - histogram, bar charts, power spectra, error charts etc. It
is used along with NumPy to provide an environment that is an effective
open source alternative for MatLab. It can also be used with graphics
toolkits like PyQt and wxPython.
b. NumPy-NumPy is a Python library used for working with arrays. It also
has functions for working in domain of linear algebra, fourier transform,
and matrices. In Python we have lists that serve the purpose of arrays,
but they are slow to process.
NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of
supporting functions that make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and
resources are very important.
c. Pandas-Pandas is an open-source library that is made mainly for working
with relational or labeled data both easily and intuitively. It provides various
data structures and operations for manipulating numerical data and time
series. This library is built on top of the NumPy library. Pandas is fast and it
has high performance & productivity for users.