[go: up one dir, main page]

0% found this document useful (0 votes)
70 views49 pages

Question Bank With Answers

The document discusses various Python concepts like features, functions, loops, operators, and differences between lists and tuples, modules and packages. It provides examples and explanations of object-oriented features, while, for, and range loops. It also covers arithmetic, comparison, and logical operators in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views49 pages

Question Bank With Answers

The document discusses various Python concepts like features, functions, loops, operators, and differences between lists and tuples, modules and packages. It provides examples and explanations of object-oriented features, while, for, and range loops. It also covers arithmetic, comparison, and logical operators in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

1. (a) Write the features of Python. Give the advantages & disadvantages of it.

(5 Marks)

Features
 Object oriented language : . Python supports object-oriented language and concepts of classes, objects
encapsulation, etc.
 Interpreted language : Python code is executed line by line at a time.
 Supports dynamic data type : A variable is decided at run time not in advance. hence, we don’t need to specify
the type of variable. (for example- int, double, long, etc.)
 Simple and easy to code : Python is very easy code
 High-level Language
 Automatic memory management
 open source: Python language is freely available
advantages & disadvantages
advantages
 Free availability (like Perl, Python is open source).
 Stability (Python is in release 2.6 at this point and, as I noted earlier, is older than Java).
 Very easy to learn and use
 Good support for objects, modules, and other reusability mechanisms.
 Easy integration with and extensibility using C and Java.
disadvantages
 Smaller pool of Python developers compared to other languages, such as Java
 Lack of true multiprocessor support
 Absence of a commercial support point, even for an Open Source project (though this situation is
changing)
 Software performance slow, not suitable for high performance applications

(b) Write a Python function to sum of the numbers in a list (5 Marks)

def sum(numbers):
total = 0
for x in numbers:
total += x
return total
print(sum((8, 2, 3, 0, 7)))

2. Discuss the Looping Statements with an example. (10 Marks)


(i) while (ii) for (iii) range

The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
We generally use this loop when we don't know the number of times to iterate beforehand.
Syntax of while Loop in Python

while test_expression:
Body of while

In the while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates
to True. After one iteration, the test expression is checked again. This process continues until
the test_expression evaluates to False.
In Python, the body of the while loop is determined through indentation.
The body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True. None and 0 are interpreted as False.
Flowchart of while Loop

Flowchart for while loop in Python


Example: Python while Loop

# Program to add natural


# numbers up to
# sum = 1+2+3+...+n

# To take input from the user,


# n = int(input("Enter n: "))

n = 10

# initialize sum and counter


sum = 0
i=1

while i <= n:
sum = sum + i
i = i+1 # update counter

# print the sum


print("The sum is", sum)

When you run the program, the output will be:

Enter n: 10
The sum is 55

In the above program, the test expression will be True as long as our counter variable i is less than or equal to n (10 in
our program).
We need to increase the value of the counter variable in the body of the loop. This is very important (and mostly
forgotten). Failing to do so will result in an infinite loop (never-ending loop).
Finally, the result is displayed.
While loop with else
Same as with for loops, while loops can also have an optional else block.
The else part is executed if the condition in the while loop evaluates to False.
The while loop can be terminated with a break statement. In such cases, the else part is ignored. Hence, a while
loop's else part runs if no break occurs and the condition is false.
Here is an example to illustrate this.

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:


print("Inside loop")
counter = counter + 1
else:
print("Inside else")

Output

Inside loop
Inside loop
Inside loop
Inside else

Here, we use a counter variable to print the string Inside loop three times.
On the fourth iteration, the condition in while becomes False. Hence, the else part is executed.

What is for loop in Python?


The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a
sequence is called traversal.
Syntax of for Loop

for val in sequence:


loop body

Here, val is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code
using indentation.
Flowchart of for Loop

Flowchart of for Loop in Python


Example: Python for Loop

# Program to find the sum of all numbers stored in a list

# List of numbers
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

# variable to store the sum


sum = 0

# iterate over the list


for val in numbers:
sum = sum+val

print("The sum is", sum)

When you run the program, the output will be:

The sum is 48

RANGE()

Range generates a list of integers and there are 3 ways to use it.
The function takes 1 to 3 arguments. Note I’ve wrapped each usage in list comprehension so we can see the values
generated.
i) range(end) : generate integers from 0 to the “end” integer.
[i for i in range(10)]
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

ii) range(start, end) : generate integers from the “start” to the “end” integer.
[i for i in range(2,10)]
#=> [2, 3, 4, 5, 6, 7, 8, 9]
iii) range(start, end, step) : generate integers from “start” to “end” at intervals of “step”.
[i for i in range(2,10,2)]
#=> [2, 4, 6, 8]

3. (a) What is the difference between a list and a tuple? Give an example (5 Marks)

(b) What is the difference between a module and a package? (5 Marks)

Module
A module can contain executable statements as well as function definitions. These statements are intended to
initialize the module. They are executed only the first time the module name is encountered in an import
statement.

Items are imported using from or import.


Syntax :
from module import function
function()
Example :
from math import sqrt
print sqrt(2.0)

Package
The collections of modules organized together and kept into a directory. That directory is known as Package.
That is , a package is a directory of modules.
Inside this directory there will be __init__.py file. This file is the one which will always be recognized and run
by the compiler. Packages are modules, but not all modules are packages.

Example
From sklearn import cross_validation
4. (a) Evaluate the following expression (i) 5//3*2-6/3*5%3 (ii) 5%8 *3+8%3*5 ((5 Marks)
(i) 5//3*2-6/3*5%3
=1*2-6/3*5%3
=2-6/3*5%3
=2-2*5%3
=2-10%3
=2-1
Ans=1

(ii) 5%8*3+8%3*5
=5*3+8%3*5
=15+8%3*5
=15+2*5
=15+10
Ans=25

(b) Write the following in Python (5 Marks)


(ii) a is greater than any one of x,y,z (ii) (logx+sin45)/xy

(i) (a>x) or (a>y) or (a>z)


(ii) math.log2 (x)+math.sin(45)/(x*y)

5. Explain in detail about python operators (10 Marks)


Python Operators in general are used to perform operations on values and variables. These are standard symbols used
for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python
operators.
Arithmetic Operators
Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and
division.
Operator Description Syntax

+ Addition: adds two operands x+y

– Subtraction: subtracts two operands x–y

* Multiplication: multiplies two operands x*y

/ Division (float): divides the first operand by the second x/y

// Division (floor): divides the first operand by the second x // y

Modulus: returns the remainder when the first operand is divided by the
% second x%y

** Power: Returns first raised to power second x ** y


Example: Arithmetic operators in Python

# Examples of Arithmetic Operator


a=9
b=4

# Addition of numbers
add = a + b

# Subtraction of numbers
sub = a - b

# Multiplication of number
mul = a * b

# Division(float) of number
div1 = a / b

# Division(floor) of number
div2 = a // b

# Modulo of both number


mod = a % b

# Power
p = a ** b

# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)

Output
13
5
36
2.25
2
1
6561
Note: Refer to Differences between / and // for some interesting facts about these two operators.
Comparison Operators
Comparison of Relational operators compares the values. It either returns True or False according to the condition.

Operato
r Description Syntax

> Greater than: True if the left operand is greater than the right x>y
Operato
r Description Syntax

< Less than: True if the left operand is less than the right x<y

== Equal to: True if both operands are equal x == y

!= Not equal to – True if operands are not equal x != y

>= Greater than or equal to True if the left operand is greater than or equal to the right x >= y

<= Less than or equal to True if the left operand is less than or equal to the right x <= y

Example: Comparison Operators in Python

# Examples of Relational Operators


a = 13
b = 33

# a > b is False
print(a > b)

# a < b is True
print(a < b)

# a == b is False
print(a == b)

# a != b is True
print(a != b)

# a >= b is False
print(a >= b)

# a <= b is True
print(a <= b)

Output
False
True
False
True
False
True
Logical Operators
Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It is used to combine conditional
statements.
Operato
r Description Syntax

and Logical AND: True if both the operands are true x and y

or Logical OR: True if either of the operands is true x or y

not Logical NOT: True if the operand is false not x

Example: Logical Operators in Python

# Examples of Logical Operator


a = True
b = False

# Print a and b is False


print(a and b)

# Print a or b is True
print(a or b)

# Print not a is False


print(not a)

Output
False
True
False
Bitwise Operators
Bitwise operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.
Operator Description Syntax

& Bitwise AND x&y

| Bitwise OR x|y

~ Bitwise NOT ~x

^ Bitwise XOR x^y

Bitwise right
>> shift x>>

<< Bitwise left shift x<<

Example: Bitwise Operators in Python

# Examples of Bitwise operators


a = 10
b=4

# Print bitwise AND operation


print(a & b)

# Print bitwise OR operation


print(a | b)

# Print bitwise NOT operation


print(~a)

# print bitwise XOR operation


print(a ^ b)

# print bitwise right shift operation


print(a >> 2)

# print bitwise left shift operation


print(a << 2)

Output
0
14
-11
14
2
40
Assignment Operators
Assignment operators are used to assigning values to the variables.
Operato
r Description Syntax

= Assign value of right side of expression to left side operand x=y+z

Add AND: Add right-side operand with left side operand and then assign to left
+= operand a+=b a=a+b

Subtract AND: Subtract right operand from left operand and then assign to left
-= operand a-=b a=a-b

Multiply AND: Multiply right operand with left operand and then assign to left
*= operand a*=b a=a*b

Divide AND: Divide left operand with right operand and then assign to left
/= operand a/=b a=a/b

Modulus AND: Takes modulus using left and right operands and assign the result
%= to left operand a%=b a=a%b
Operato
r Description Syntax

Divide(floor) AND: Divide left operand with right operand and then assign the
//= value(floor) to left operand a//=b a=a//b

Exponent AND: Calculate exponent(raise power) value using operands and assign
**= value to left operand a**=b a=a**b

&= Performs Bitwise AND on operands and assign value to left operand a&=b a=a&b

|= Performs Bitwise OR on operands and assign value to left operand a|=b a=a|b

^= Performs Bitwise xOR on operands and assign value to left operand a^=b a=a^b

>>= Performs Bitwise right shift on operands and assign value to left operand a>>=b a=a>>b

a <<= b a= a <<
<<= Performs Bitwise left shift on operands and assign value to left operand b

Example: Assignment Operators in Python

# Examples of Assignment Operators


a = 10

# Assign value
b=a
print(b)

# Add and assign value


b += a
print(b)

# Subtract and assign value


b -= a
print(b)

# multiply and assign


b *= a
print(b)

# bitwise lishift operator


b <<= a
print(b)

Output
10
20
10
100
102400
Identity Operators
is and is not are the identity operators both are used to check if two values are located on the same part of the memory.
Two variables that are equal do not imply that they are identical.

is True if the operands are identical


is not True if the operands are not identical
Example: Identity Operator

a = 10
b = 20
c=a

print(a is not b)
print(a is c)

Output
True
True
Membership Operators
in and not in are the membership operators; used to test whether a value or variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence

Example: Membership Operator

# Python program to illustrate


# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]

if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")

if (y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")

Output
x is NOT present in given list
y is present in given list
Precedence and Associativity of Operators

6. (a) Write python program to illustrate variable length keyword arguments (5 Marks)
*args and **kwargs are mostly used in function definitions. *args and **kwargs allow you to pass an unspecified
number of arguments to a function, so when writing the function definition, you do not need to know how many
arguments will be passed to your function. *args is used to send a non-keyworded variable length argument list
to the function. Here’s an example to help you get a clear idea:
def test_var_args(f_arg, *argv):
print("first normal arg:", f_arg)
for arg in argv:
print("another arg through *argv:", arg)

test_var_args('yasoob', 'python', 'eggs', 'test')


This produces the following result:
first normal arg: yasoob
another arg through *argv: python
another arg through *argv: eggs
another arg through *argv: test
I hope this cleared away any confusion that you had. So now let’s talk about **kwargs
1.2. Usage of **kwargs
**kwargs allows you to pass keyworded variable length of arguments to a function. You should use **kwargs if you
want to handle named arguments in a function. Here is an example to get you going with it:
def greet_me(**kwargs):
for key, value in kwargs.items():
print("{0} = {1}".format(key, value))

>>> greet_me(name="yasoob")
name = yasoob
So you can see how we handled a keyworded argument list in our function. This is just the basics of **kwargs and you
can see how useful it is. Now let’s talk about how you can use *args and **kwargs to call a function with a list or
dictionary of arguments.
1.3. Using *args and **kwargs to call a function
So here we will see how to call a function using *args and **kwargs. Just consider that you have this little function:
def test_args_kwargs(arg1, arg2, arg3):
print("arg1:", arg1)
print("arg2:", arg2)
print("arg3:", arg3)
Now you can use *args or **kwargs to pass arguments to this little function. Here’s how to do it:
# first with *args
>>> args = ("two", 3, 5)
>>> test_args_kwargs(*args)
arg1: two
arg2: 3
arg3: 5

# now with **kwargs:


>>> kwargs = {"arg3": 3, "arg2": "two", "arg1": 5}
>>> test_args_kwargs(**kwargs)
arg1: 5
arg2: two
arg3: 3
Order of using *args **kwargs and formal args
So if you want to use all three of these in functions then the order is
some_func(fargs, *args, **kwargs)

(b) Write python program to perform linear search (5 Marks)

def search(arr, n, x):

for i in range(0, n):


if (arr[i] == x):
return i
return -1
# Driver Code
arr = [10,50,30,70, 80, 60, 20, 90,40]
x = 20
n = len(arr)

# Function call
result = search(arr, n, x)
if(result == -1):
print("Element is not present in array")
else:
print("Element is present at index", result)

7. Explain any 5 string functions with examples (10 Marks)

swapcase(...)
| S.swapcase() -> string
|
| Return a copy of the string S with uppercase characters
| converted to lowercase and vice versa

strip(...)
| S.strip([chars]) -> string or unicode
|
| Return a copy of the string S with leading and trailing
| whitespace removed.
| If chars is given and not None, remove characters in chars instead.

| startswith(...)
| S.startswith(prefix[, start[, end]]) -> bool
|
| Return True if S starts with the specified prefix, False otherwise.
| With optional start, test S beginning at that position.
| With optional end, stop comparing S at that position.
| prefix can also be a tuple of strings to try.

| split(...)
| S.split([sep [,maxsplit]]) -> list of strings
|
| Return a list of the words in the string S, using sep as the
| delimiter string. If maxsplit is given, at most maxsplit
| splits are done. If sep is not specified or is None, any
| whitespace string is a separator and empty strings are removed
| from the result.

format(...)
| S.format(*args, **kwargs) -> string
|
| Return a formatted version of S, using substitutions from args and kwargs.
| The substitutions are identified by braces ('{' and '}').

8. (a) Write short note on slice operator (5 Marks)


Python slicing is about obtaining a sub-string from the given string by slicing it respectively from start to end.
Python slicing can be done in two ways.
 slice() Constructor
 Extending Indexing

slice(stop)
slice(start, stop, step)
Parameters:
start: Starting index where the slicing of object starts.
stop: Ending index where the slicing of object stops.
step: It is an optional argument that determines the increment between each index for slicing.
Return Type: Returns a sliced object containing elements in the given range only.

slice() Constructor
The slice() constructor creates a slice object representing the set of indices specified by range(start, stop, step).

A segment of a string is called a slice. Selecting a slice is similar to selecting a character:


>>> s = 'Monty Python'
>>> print s[0:5] Monty
>>> print s[6:13] Python
The operator [n:m] returns the part of the string from the “n-eth” character to the “m-eth” character, including the first
but excluding the last. This behavior is counterintuitive, but it might help to imagine the indices pointing between the
characters, as in the following diagram:

fruit ’ b a n a n a ’

index 0 1 2 3 4 5 6
If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the
second index, the slice goes to the end of the string:
>>> fruit = 'banana'
>>> fruit[:3] 'ban'
>>> fruit[3:] 'ana'

If the first index is greater than or equal to the second the result is an empty string, represented by two
quotation marks:

>>> fruit = 'banana'


>>> fruit[3:3] ''
An empty string contains no characters and has length 0, but other than that, it is the same as any other string.

Extending indexing

In Python, indexing syntax can be used as a substitute for the slice object. This is an easy and convenient way to
slice a string both syntax wise and execution wise.

Syntax

string[start:end:step]
start, end and step have the same mechanism as slice() constructor.

Example

# String slicing
String ='ASTRING'

# Using indexing sequence


print(String[:3])
print(String[1:5:2])
print(String[-1:-12:-2])

# Prints string in reverse


print("\nReverse String")
print(String[::-1])

Output:
AST
SR
GITA

Reverse String
GNIRTSA

(b) nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
Write the output (i)nums[2:7] (ii) nums[:5] (iii) nums[-3:] (5 Marks)
(i) [30, 40, 50, 60, 70]
(ii) [10, 20, 30, 40, 50]
(iii) [70, 80, 90]

9. Write a python program using object oriented programming to demonstrate encapsulation, overloading
and inheritance (10 Marks)

class Base:
def __init__(self):
self.a = 10
self._b = 20

def display(self):
print(" the values are :")
print(f"a={self.a} b={self._b}")

class Derived(Base): # Creating a derived class


def __init__(self):
Base.__init__(self) # Calling constructor of Base class
self.d = 30

def display(self):
Base.display(self)
print(f"d={self.d}")

def __add__(self, ob):


return self.a + ob.a+self.d + ob.d
#return self.a + ob.a+self.d + ob.d+self.b + ob.b

obj1 = Base()
obj2 = Derived()
obj3 = Derived()
obj2.display()
obj3.display()

print("\n Sum of two objects :",obj2 + obj3)

10. Write Python program to count words and store in dictionary for the given input text
Input Text : the clown ran after the car and the car ran into the tent and the tent fell down on the clown
and the car
Output : word count : {'and': 3, 'on': 1, 'ran': 2, 'car': 3, 'into': 1, 'after': 1, 'clown': 2, 'down': 1, 'fell': 1,
'the': 7, 'tent': 2}

Method 1

counts = dict()
line = input('Enter a line of text:')
words = line.split()
print('Words:', words)
print('Counting...’)

for word in words:


counts[word] = counts.get(word,0) + 1
print('Counts', counts)

Method 2

def word_count(str):
counts = dict()
words = str.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts

#Driver Code
print( word_count(' the clown ran after the car and the car ran into the tent and the tent fell down on the
clown and the car’))

Output:
----------

Differentiate netween List and Array.


LISľ ARRAY

1 List is used to collect items that usually consist An aííay is also a vital component that collects
of elements of multiple data types. seveíal items of the same data type.

2 List cannot manage aíithmetic opeíations. Aííay can manage aíithmetic opeíations.

4 When it comes to flexibility, the list is When it comes to flexibility, the aííay is not suitable
peífect as it allows easy modification of as it does not allow easy modification of data.
data.

5 It consumes a laígeí memoíy. It consumes less memoíy than a list.

6 In a list, the complete list can be accessed In an aííay, a loop is mandatoíy to access the
without any specific looping. components of the aííay.

Difference between List, Tuples and dictionary:

List Tuples Dictionary


A list is mutable A tuple is immutable A dictionary is mutable
Lists are dynamic Tuples are fixed size in nature In values can be of any
data type and can
repeat, keys must be of
immutable type
List are enclosed in Tuples are enclosed in parenthesis ( ) Dictionaries are enclosed in
brackets[ ] and their and cannot be updated curly braces { } and
elements and size consist of key:value
can be changed
Homogenous Heterogeneous Homogenous
Example: Example: Example:
List = [10, 12, 15] Words = ("spam", "egss") Dict = {"ram": 26, "abi":
Or 24}
Words = "spam", "eggs"
Access: Access: Access:
print(list[0]) print(words[0]) print(dict["ram"])
Can contain duplicate Can contain duplicate Can’t contain
elements elements. Faster compared to duplicate keys, but
lists can contain duplicate
values
Slicing can be done Slicing can be done Slicing can't be done
Usage: Usage: Usage:
  
List is used for a Tuple can be used when data Dictionary is used
collection of data that cannot be changed. when a logical
doesnt need random 
A tuple is used in combination association between
access. key:value pair.

with a dictionary i.e.a tuple 
List is used when When in need of fast
might represent a key.
data can be modified lookup for data, basedon
frequently a custom key.

Dictionary is used
when data is being
constantly
modified.

1 Write any five Basic Array Statistical Methods with an example

NumPy Statistical Functions


1. np.amin()- This function determines the minimum value of the element along a specified axis.
2. np.amax()- This function determines the maximum value of the element along a specified
axis.
3. np.mean()- It determines the mean value of the data set.
4. np.median()- It determines the median value of the data set.
5. np.std()- It determines the standard deviation
6. np.var – It determines the variance.
7. np.percentile()- It determines the nth percentile of data along the specified axis.

np.amin() & np.amax()-


import numpy as np
arr= np.array([[1,23,78],[98,60,75],[79,25,48]])
print(arr)
#Minimum Function
print(np.amin(arr))
#Maximum Function
print(np.amax(arr))

Output
[[ 1 23 78]
[98 60 75]
[79 25 48]]
1
98

Mean
Mean is the sum of the elements divided by its sum and given by the following formula:

1|Page
It calculates the mean by adding all the items of the arrays and then divides it by the number of
elements. We can also mention the axis along which the mean can be calculated.

import numpy as np
a = np.array([5,6,7])
print(a)
print(np.mean(a))

Output
[5 6 7]
6.0

Median
Median is the middle element of the array. The formula differs for odd and even sets.

It can calculate the median for both one-dimensional and multi-dimensional arrays. Median
separates the higher and lower range of data values.

import numpy as np
a = np.array([5,6,7])
print(a)
print(np.median(a))

Output
[5 6 7]
6.0

Standard Deviation
Standard deviation is the square root of the average of square deviations from mean. The formula
for standard deviation is:

import numpy as np
a = np.array([5,6,7])
print(a)

2|Page
print(np.std(a))

Output
[5 6 7]
0.816496580927726

Variance
Variance is the average of the square deviations. Following is the formula for the same:

import numpy as np
a = np.array([5,6,7])
print(a)
print(np.var(a))

Output
[5 6 7]
0.6666666666666666

NumPy Percentile Function


It has the following syntax:
numpy.percentile(input, q, axis)
The accepted parameters are:

 input: it is the input array.


 q: it is the percentile which it calculates of the array elements between 0-100.
 axis: it specifies the axis along which calculation is performed.
a = np.array([2,10,20])
print(a)
print(np.percentile(a,10,0))

Output
[ 2 10 20]
3.6

2 Write python code to interact with database and perform the following task:
i) Create table
ii) Insert 3 records into the table
iii) Display all records

SQL Based relational Databases are widely used to store data. Eg - SQL Server, PostgreSQL,
MySQL, etc. Many alternative databases have also become quite popular.
The choice of DataBase is usually dependant on performance, data integrity and scalability nneds
of the application.
Loading data from SQl to DataFrame is straightforward. pandas has some functions to simplify
the process.
3|Page
In this example, we create a SQLite database using Python's built in sqlite3 driver.
In [ ]:

Most SQL Drivers (PyODBC, psycopg2, MySQLdb, pymssql, etc.) return a list of tuples when
selecting data from table. We can use these list of tuples for the DataFrame, but the column
names are present in the cursor's 'description' attribute.

4|Page
3(a) Write a Python program to write and read the contents of text file.

with open("test.txt", 'w', encoding = 'utf-8') as f:


f.write("my first file\n")
f.write("This file\n")
f.write("contains three lines\n")
f.close()
f = open("test.txt", mode='r', encoding='utf-8')
print(f.read())

3(b Write a Python program to sort integer elements using Bubble sort
)

def bubbleSort(array):

for i in range(len(array)):
for j in range(0, len(array) - i - 1): # loop to compare array elements
if array[j] > array[j + 1]: # compare two adjacent elements change > to < to sort in
descending order
temp = array[j] # swapping elements if elements
array[j] = array[j+1]
array[j+1] = temp

## Main code
x = [-2, 45, 0, 11, -9]
bubbleSort(x)

5|Page
print('Sorted Array in Ascending Order:')
print(x)

Output
Sorted Array in Ascending Order:
[-9, -2, 0, 11, 45]

4 Write python code for the following :


i) read data and write into CSV
ii) read data from JSON
iii) read data and write into EXCEL

6|Page
7|Page
5 Discuss any five methods to handle the missing data with python code

## The possible ways to do this are:


i) Deleting the columns with missing data
ii) Deleting the rows with missing data
iii) Filling the missing data with a value – Imputation- mean , median
iv) Filling the missing data with mode if it’s a categorical value.
v) Filling with a Regression Model

## method -1 - Deleting the columns with missing data

df = pd.DataFrame(np.random.randn(7,3))
df.iloc[:4, 1] = np.nan
df.iloc[:2, 2] =np.nan
df
# Deleting the columns with missing data
df.dropna(axis=1)

8|Page
## method -2 - Deleting the rows with missing data
df = pd.DataFrame(np.random.randn(7,3))
df.iloc[:4, 1] = np.nan
df.iloc[:2, 2] =np.nan
df
# Deleting the columns with missing data
df.dropna(axis=0)

## Drop all NAN rows


df = pd.DataFrame(np.random.randn(7,3))
df.iloc[:4, 1] = np.nan
df.iloc[:2, 2] =np.nan
df
df.dropna()

## fill with zero


df = pd.DataFrame(np.random.randn(7,3))

9|Page
df.iloc[:4, 1] = np.nan
df.iloc[:2, 2] =np.nan
df
df.fillna(0, inplace=True)

In [ ]:
## Threshold -keyword
df = pd.DataFrame(np.random.randn(7,3))
df.iloc[:4, 1] = np.nan
df.iloc[:2, 2] =np.nan
df
df.dropna(thresh=2)

In [ ]:
## ## method -3 Filling the missing data with a value –Imputation - mean
df = pd.DataFrame(np.random.randn(7,3))
df.iloc[:4, 1] = np.nan
df.iloc[:2, 2] =np.nan
df
df.fillna(df.mean(), inplace=True)

10 | P a g e
In [ ]:
## ## method -3 - Filling the missing data with a value – Imputation-median
df = pd.DataFrame(np.random.randn(7,3))
df.iloc[:4, 1] = np.nan
df.iloc[:2, 2] =np.nan
df.info()
d
df.fillna(df.median(), inplace=True

6 What are step in data preprocessing? Explain with an example


• Step 1: Analyze the dataset
• Step 2: Import the libraries
• Step 3 (a): Import the dataset
 Setting current working directory
 Import the dataset
• Step 4: Handling Missing Values
 There are numerous methods to handle missing values in data frame.
• Step 5: Categorical Data
• Step 6: Splitting the dataset into train and test set
• Step 7: Feature Scaling

1. Analyze the dataset

2. Import the libraries

11 | P a g e
3. (a): Import the dataset
a. Setting current working directory
b. Import the dataset

3.(b) Create matrix of features

4. Handling Missing Values


There are numerous methods to handle missing values in data frame.
df.method() description

dropna() Drop missing observations


dropna(how='all') Drop observations where all cells is NA
dropna(axis=1, how='all') Drop column if all the values are missing

fillna(0) Replace missing values with zeros


isnull() returns True if the value is missing

5: Categorical Data
 ML models are based on mathematical equations.
 Country- France, Germany, Spain
 Purcahsed- Yes, No

12 | P a g e
6.Splitting the dataset into train and test set

7 Write a python statement to Remove Row duplicates from the Data frame, null values
(a)
An important part of Data analysis is analyzing Duplicate Values and removing them.
Pandas drop_duplicates() method helps in removing duplicates from the data frame.

Syntax: DataFrame.drop_duplicates(subset=None, keep=’first’, inplace=False)


Parameters:
subset: Subset takes a column or list of column label. It’s default value is none. After passing
columns, it will consider them only for duplicates.
keep: keep is to control how to consider duplicate value. It has only three distinct value and
default is ‘first’.

 If ‘first’, it considers first value as unique and rest of the same values as duplicate.
 If ‘last’, it considers last value as unique and rest of the same values as duplicate.
 If False, it consider all of the same values as duplicates
inplace: Boolean values, removes rows with duplicates if True.
Return type: DataFrame with removed duplicate rows depending on Arguments passed.

Python’s pandas library provides a function to remove rows or columns from a dataframe which
contain missing values or NaN i.e.
DataFrame.dropna(self, axis=0, how='any', thresh=None, subset=None, inplace=False)
Arguments :
axis:
 0 , to drop rows with missing values
 1 , to drop columns with missing values

13 | P a g e
how:
 ‘any’ : drop if any NaN / missing value is present
 ‘all’ : drop if all the values are missing / NaN
thresh: threshold for non NaN values
inplace: If True then make changes in the dataplace itself
It removes rows or columns (based on arguments) with missing values / NaN
7( Discuss loc and iloc functions with an example
b)  loc is label-based, which means that you have to specify rows and columns based on their
row and column labels.
 iloc is integer position-based, so you have to specify rows and columns by their integer
position values (0-based integer position).

Example :

df= pd.read_csv('data/data.csv', index_col=['Day'])

i) Selecting via a single value : Both loc and iloc allow input to be a single value.
Syntax for data selection:

 loc[row_label, column_label]
 iloc[row_position, column_position]

For example, let’s say we would like to retrieve Friday’s temperature value. With loc, we can pass
the row label 'Fri' and the column label 'Temperature'.
# To get Friday's temperature using loc
df.loc['Fri', 'Temperature']

Output:
10.51
The equivalent iloc statement should take the row number 4 and the column number 1 .
# To get Friday's temperature using iloc
df.iloc[4, 1]

Output:
10.51

2) To get all rows / To get all columns


We can also use : to return all data. For example, to get all rows:
# To get all rows using loc
>>> df.loc[:, 'Temperature']

Output:
Day
Mon 12.79
Tue 19.67
Wed 17.51

14 | P a g e
Thu 14.44
Fri 10.51
Sat 11.07
Sun 17.50
Name: Temperature, dtype: float64#

# To get all rows using iloc


>>> df.iloc[:, 1]

And to get all columns:


# To get all columns using loc
>>> df.loc['Fri', :]

Output:
Weather Shower
Temperature 10.51
Wind 26
Humidity 79
Name: Fri, dtype: object

# To get all columns using iloc


>>> df.iloc[4, :]

3. Selecting via a list of values

We can pass a list of labels to loc to select multiple rows or columns:


# Multiple rows using loc
>>> df.loc[['Thu', 'Fri'], 'Temperature']

Output:
Day
Thu 14.44
Fri 10.51
Name: Temperature, dtype: float64# Multiple columns

# Multiple rows using iloc


>>> df.loc['Fri', ['Temperature', 'Wind']]
Output:
Temperature 10.51
Wind 26
Name: Fri, dtype: object

Similarly, a list of integer values can be passed to iloc to select multiple rows or columns. Here are
the equivalent statements using iloc:
>>> df.iloc[[3, 4], 1]
Output:
Day
Thu 14.44
Fri 10.51
Name: Temperature, dtype: float64

All the above outputs are Series because their results are 1-dimensional data.
4) The output will be a DataFrame when the result is 2-dimensional data,

15 | P a g e
# Multiple rows and columns using loc
rows = ['Thu', 'Fri']
cols=['Temperature','Wind']
df.loc[rows, cols]

# Multiple rows and columns using loc


rows = [3, 4]
cols = [1, 2]
df.iloc[rows, cols]

8 Discuss different types of joins can be used in Pandas tools to implement a wide array
of functionality with an example.

These three types of joins can be used with other Pandas tools to implement a wide array of
functionality
The pd.merge() function implements a number of types of joins:
the one-to-one,
many-to-one, and
many-to-many joins

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'employee': ['Bob', 'Jake', 'Lisa', 'Sue'],
'group': ['Accounting', 'Engineering', 'Engineering', 'HR']})
df2 = pd.DataFrame({'employee': ['Lisa', 'Bob', 'Jake', 'Sue'],
'hire_date': [2004, 2008, 2012, 2014]})
display('df1', df1 , 'df2', df2 )

#One-to-one joins
df3 = pd.merge(df1, df2)
df3

16 | P a g e
9 What are the different ways a DataFrame can be created in python? Explain with
an example

DataFrame is a two-dimensional labeled data structures with columns of potentially different


types. In general, DataFrame like a spreadsheet and it contains three components: index, columns
and data. Dataframes can be created by different ways.
1. Create pandas DataFrame from dictionary of lists
The dictionary keys represent the columns names and each list represents a column contents.
# Import pandas library
import pandas as pd # Create a dictionary of list
dictionary_of_lists = {
'Name': ['Emma', 'Oliver', 'Harry', 'Sophia'],
'Age': [29, 25, 33, 24],
'Department': ['HR', 'Finance', 'Marketing', 'IT']}# Create the DataFrame
df1 = pd.DataFrame(dictionary_of_lists)
df1
2. Create pandas DataFrame from dictionary of numpy array.
The dictionary keys represent the columns names and each array element represents a
column contents.
# Import pandas and numpy libraries
import pandas as pd
import numpy as np # Create a numpy array
nparray = np.array(
[['Emma', 'Oliver', 'Harry', 'Sophia'],
[29, 25, 33, 24],
['HR', 'Finance', 'Marketing', 'IT']]) # Create a dictionary of nparray
dictionary_of_nparray = {
'Name': nparray[0],
'Age': nparray[1],
'Department': nparray[2]} # Create the DataFrame
df2 = pd.DataFrame(dictionary_of_nparray)
df2
3. Create pandas DataFrame from list of lists
Each inner list represents one row.
# Import pandas library
import pandas as pd# Create a dictionary of list
dictionary_of_lists = {
'Name': ['Emma', 'Oliver', 'Harry', 'Sophia'],
'Age': [29, 25, 33, 24],
'Department': ['HR', 'Finance', 'Marketing', 'IT']}# Create the DataFrame

17 | P a g e
df1 = pd.DataFrame(dictionary_of_lists)
df1
4. Create pandas DataFrame from list of dictionaries
Each dictionary represents one row and the keys are the columns names.
# Import pandas library
import pandas as pd # Create a list of dictionaries
list_of_dictionaries = [
{'Name': 'Emma', 'Age': 29, 'Department': 'HR'},
{'Name': 'Oliver', 'Age': 25, 'Department': 'Finance'},
{'Name': 'Harry', 'Age': 33, 'Department': 'Marketing'},
{'Name': 'Sophia', 'Age': 24, 'Department': 'IT'}]
# Create the DataFrame
df4 = pd.DataFrame(list_of_dictionaries)
df4
5. Create pandas Dataframe from dictionary of pandas Series
The dictionary keys represent the columns names and each Series represents a column contents.
# Import pandas library
import pandas as pd # Create Series
series1 = pd.Series(['Emma', 'Oliver', 'Harry', 'Sophia'])
series2 = pd.Series([29, 25, 33, 24])
series3 = pd.Series(['HR', 'Finance', 'Marketing', 'IT']) # Create a dictionary of Series
dictionary_of_nparray = {'Name': series1, 'Age': series2, 'Department':series3} # Create the
DataFrame
df5 = pd.DataFrame(dictionary_of_nparray)
df5
o/p:

10 Demonstrate the following using NumPy python code


a) Searching, b) Sorting and c) Splitting

###Sorting
# importing Numpy package
import numpy as np

a = np.array([[1,4],[3,1]])
print("sorted array : ",np.sort(a)) # sort along the last axis
print("\n sorted flattened array:", np.sort(a, axis=0)) # sort the flattened array

x = np.array([3, 1, 2])

print("\n indices that would sort an array",np.argsort(x))

18 | P a g e
print("\n sorting complex number :" ,np.sort_complex([5, 3, 6, 2, 1]))

Output:
sorted array : [[1 4]
[1 3]]

sorted flattened array: [[1 1]


[3 4]]

indices that would sort an array [1 2 0]

sorting complex number : [1.+0.j 2.+0.j 3.+0.j 5.+0.j 6.+0.j]


In [7]:
### Searching

# where( ) : search an array for a certain value, and return the indexes that get a match.

# searchsorted( ) which performs a binary search in the array,


# and returns the index where the specified value would be inserted to maintain the search order.
In [8]:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)

arr = np.array([6, 7, 8, 9])


x = np.searchsorted(arr, 5)
print(x)

arr = np.array([1, 3, 5, 7])


x = np.searchsorted(arr, [2, 4, 6])
print(x)

Output:
(array([3, 5, 6], dtype=int64),)
0
[1 2 3]

###Splitting

#np.split: split an array into multiple sub-arrays as views into ary.

#np.array_split: Split an array into multiple sub-arrays of equal or near-equal size. Does not raise
an exception if an equal division cannot be made.

#np.hsplit: Split array along horizontal axis.

#np.vsplit: Split array along vertical axis.

#np.array_split: Split array along specified axis.


In [10]:
import numpy as np

x = np.arange(9.0)
print(x)

19 | P a g e
print(np.split(x, 3)) # with no of partitions N,
print(np.split(x, [3, 5, 6, 10])) # with indices

#the array will be divided into N equal arrays along axis. If such a split is not possible, an error is
raised.

x = np.arange(9)
np.array_split(x, 4)

#Split an array into multiple sub-arrays of equal or near-equal size. Does not raise an exception if
an equal division cannot be made.

a = np.array([[1, 3, 5, 7, 9, 11],
[2, 4, 6, 8, 10, 12]])

# horizontal splitting
print("Splitting along horizontal axis into 2 parts:\n", np.hsplit(a, 2))

# vertical splitting
print("\nSplitting along vertical axis into 2 parts:\n", np.vsplit(a, 2))

Output:

[0. 1. 2. 3. 4. 5. 6. 7. 8.]
[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]
[array([0., 1., 2.]), array([3., 4.]), array([5.]), array([6., 7., 8.]), array([], dtype=float64)]
Splitting along horizontal axis into 2 parts:
[array([[1, 3, 5],
[2, 4, 6]]), array([[ 7, 9, 11],
[ 8, 10, 12]])]

Splitting along vertical axis into 2 parts:


[array([[ 1, 3, 5, 7, 9, 11]]), array([[ 2, 4, 6, 8, 10, 12]])]

1 What are the Steps in Visualization ? Explain with an example

Data Visualization :Steps involved in plotting a graph


• Define the x-axis and corresponding y-axis values as lists.
• Plot them on canvas using .plot() function.
• Give a name to x-axis and y-axis using .xlabel() and .ylabel() functions.
• Give a title to your plot using .title() function.
• Finally, view the plot, using .show() function.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
cars_data = pd.read_csv('Toyota.csv', index_col=0,na_values=["??","????"])

#removing missing values from the dataframe


cars_data.dropna(axis=0, inplace=True)
## Scatter Plot

20 | P a g e
plt.scatter(cars_data['Age'],cars_data['Price'],c='red')
plt.title('Scatter plot of Price vs Age of the cars')
plt.xlabel('Age(months)')
plt.ylabel('Price(Euros)')
plt.show()

## Histogram
plt.hist(cars_data['KM'])

Out[3]:
(array([ 92., 239., 331., 222., 111., 51., 25., 13., 10., 2.]),
array([1.000000e+00, 2.430090e+04, 4.860080e+04, 7.290070e+04,
9.720060e+04, 1.215005e+05, 1.458004e+05, 1.701003e+05,
1.944002e+05, 2.187001e+05, 2.430000e+05]),
<BarContainer object of 10 artists>)

## Bar Plot (for categorical variables)


In [5]:
counts=[979, 120, 12]
fuelType = ('Petrol', 'Diesel', 'CNG')
index = np.arange(len(fuelType))

plt.bar(index, counts,color=['red','blue', 'cyan'])


plt.title('Bar plot of fuel types')
plt.xlabel('Fuel Types')
plt.ylabel('frequency')
plt.xticks(index, fuelType, rotation = 0)
plt.show()

21 | P a g e
2 What do you mean by Normalization and Standardization? Write any five differences
between them
Feature scaling is one of the most important data preprocessing step in machine learning. Algorithms that
compute the distance between the features are biased towards numerically larger values if the data is not scaled.
Tree-based algorithms are fairly insensitive to the scale of the features. Also, feature scaling helps machine
learning, and deep learning algorithms train and converge faster.
There are some feature scaling techniques such as Normalization and Standardization that are the most popular
and at the same time, the most confusing ones.
Let’s resolve that confusion.
Normalization or Min-Max Scaling is used to transform features to be on a similar scale. The new point is
calculated as:
X_new = (X - X_min)/(X_max - X_min)
This scales the range to [0, 1] or sometimes [-1, 1]. Geometrically speaking, transformation squishes the n-
dimensional data into an n-dimensional unit hypercube. Normalization is useful when there are no outliers as it
cannot cope up with them. Usually, we would scale age and not incomes because only a few people have high
incomes but the age is close to uniform.
Standardization or Z-Score Normalization is the transformation of features by subtracting from mean and
dividing by standard deviation. This is often called as Z-score.
X_new = (X - mean)/Std
Standardization can be helpful in cases where the data follows a Gaussian distribution. However, this does not
have to be necessarily true. Geometrically speaking, it translates the data to the mean vector of original data to
the origin and squishes or expands the points if std is 1 respectively. We can see that we are just changing mean
and standard deviation to a standard normal distribution which is still normal thus the shape of the distribution is
not affected.
Standardization does not get affected by outliers because there is no predefined range of transformed features.
Difference between Normalization and Standardization
S.NO. Normalization Standardization

Minimum and maximum value of Mean and standard deviation is used for
1. features are used for scaling scaling.

It is used when features are of different It is used when we want to ensure zero
2. scales. mean and unit standard deviation.

3. Scales values between [0, 1] or [-1, 1]. It is not bounded to a certain range.

22 | P a g e
4. It is really affected by outliers. It is much less affected by outliers.

Scikit-Learn provides a transformer Scikit-Learn provides a transformer


called MinMaxScaler for called StandardScaler for
5. Normalization. standardization.

This transformation squishes the n- It translates the data to the mean vector
dimensional data into an n- of original data to the origin and squishes
6. dimensional unit hypercube. or expands.

It is useful when we don’t know about It is useful when the feature distribution
7. the distribution is Normal or Gaussian.

It is a often called as Scaling It is a often called as Z-Score


8. Normalization Normalization.
3 Write the importance of self and __init__() method. Give an example

i)Importance of ‘self’
• Explicit reference to refer the current object, i.e the object which invoked the method
• Used to create and initialize instance variables of a class i.e it creates the attribute for the
class
• ‘self’ reference must be used as a first parameter in all instance methods of a class
otherwise the methods are known as simply “class methods”
• Moreover, “self” is not a keyword and has no special meaning in Python. We can use any
name in that place. However, it is recommended not to use any name other than “self”
(merely a convention and for readability)

ii) __init__() method


• __init__() method helps in initializing the variable during the creation of object. Hence, it
is also called as ‘initializer method’ or Default Constructor.

Instance Methods and Static Methods


Instance / Regular methods require an instance (self) as the first argument and when the
method is invoked (bound), self is automatically passed to the method
• Static methods are functions which do not require
instance but are part of class definitions.
When to use Static and Instance methods?
• Instance methods Useful when method needs access to the values that are specific to the
instance and needs to call other methods that have access to instance specific values.

4 Write a Python program to demonstrate Data Visualization using Seaborn

23 | P a g e
24 | P a g e
25 | P a g e
5 Explain the terms histogram, binning and density.

6 Explain with an example “The GroupBy object “-- aggregate, filter, transform, and apply

The GroupBy object is a very flexible abstraction. In many ways, you can simply treat it as if it's a
collection of DataFrames, and it does the difficult things under the hood. Let's see some examples
using the Planets data.

Perhaps the most important operations made available by


a GroupBy are aggregate, filter, transform, and apply. We'll discuss each of these more fully
in "Aggregate, Filter, Transform, Apply", but before that let's introduce some of the other
functionality that can be used with the basic GroupBy operation.

Aggregate, filter, transform, apply

The preceding discussion focused on aggregation for the combine operation, but there are more
options available. In particular, GroupBy objects have aggregate(), filter(), transform(),
and apply() methods that efficiently implement a variety of useful operations before combining
the grouped data.
For the purpose of the following subsections, we'll use this DataFrame:
Code

rng = np.random.RandomState(0)
df = pd.DataFrame({'key': ['A', 'B', 'C', 'A', 'B', 'C'],
'data1': range(6),
'data2': rng.randint(0, 10, 6)},
columns = ['key', 'data1', 'data2'])
df

Aggregation

The aggregate() method allows for even more flexibility. It can take a string, a function, or a list thereof, and compute
all the aggregates at once. Here is a quick example combining all these:

26 | P a g e
7 When to use Static and Instance methods? Explain with an example

Instance / Regular methods require an instance (self) as the first argument and when the method
is invoked (bound), self is automatically passed to the method
• Static methods are functions which do not require instance but are part of class definitions.
• Static mathods Useful when method does not need access to either the class variables or
the instance variables.
• Instance methods Useful when method needs access to the values that are specific to the
instance and needs to call other methods that have access to instance specific values.

• Before init () method, the object is been already constructed


8 Justify the need of Pivot Tables? Explain with and example
We have seen how the GroupBy abstraction lets us explore relationships within a dataset.

27 | P a g e
A pivot table is a similar operation that is commonly seen in spreadsheets and other programs that
operate on tabular data.

The pivot table takes simple column-wise data as input, and groups the entries into a two-
dimensional table that provides a multidimensional summarization of the data.

The difference between pivot tables and GroupBy : pivot tables as essentially
a multidimensional version of GroupBy aggregation. That is, you split-apply-combine, but both
the split and the combine happen across not a one-dimensional index, but across a two-
dimensional grid.
Pivot Table Syntax

Here is the equivalent to the preceding operation using the pivot_table method of DataFrames:

his is eminently more readable than the groupby approach, and produces the same result. As you
might expect of an early 20th-century transatlantic cruise, the survival gradient favors both
women and higher classes. First-class women survived with near certainty (hi, Rose!), while only
one in ten third-class men survived (sorry, Jack!).
Multi-level pivot tables

Just as in the GroupBy, the grouping in pivot tables can be specified with multiple levels, and via
a number of options. For example, we might be interested in looking at age as a third dimension.
We'll bin the age using the pd.cut function:

9 List and Explain in detail different ways of creating contour plots

10 Write a Pandas program to create a data frame with the test data , split the dataframe by school code and get mean,
min, and max value of i) age ii) weight for each school.
Test Data:
school class name age height weight
S1 s001 V Ram 12 173 35
S2 s002 V Kiran 12 192 32
S3 s003 VI Ryan 13 186 33

28 | P a g e
S4 s001 VI Bhim 13 167 30
S5 s002 VI Sita 14 151 31
S6 s004 V Bhavana 12 159 32

Solution

29 | P a g e
Python Revision Questions

1. Write the features of Python. Give the advantages & disadvantages of it.
2. Write a python program to check and print given list contains even numbers or not.
3. Discuss the Looping Statements Syntax and example.
4. Write any three differences between a list, tuples and dictionary ?
5. Illustrate args and kwargs parameters in python programming language with an example
6. How to create dictionary in python. Explain five methods with a brief description with example
7. Explain any five string functions with syntax and example.
8. How to create Constructors, inheritance and operator overloading in Python? Demonstrate with an
example
9. Discuss the usage of the following with respect to the print() function i) sep argument ii) end argument
iii) .format(arguments)
10. Differentiate between
i). pop and remove in List
ii). Aliasing & Cloning in List
iii). append () and insert () methods of list.
11. Write a python program to find the roots of a quadratic equation.

12. How do you implement call by reference or call by value in python? Explain with an example
13. Lists are heterogeneous. Support the statement with an example.
14. Write a Python program to reverse words in a given String in Python.
15. Write a Python program to read the contents of a text file and count the number of words in it.
16. Calculate Area of Rectangle and Triangle using Single Inheritance.
17. Write the features of Python. Give the advantages & disadvantages of it.
18. Discuss the Looping Statements with an example
19. Write a python program to ask user to enter a number and check if entered number is Prime number or
not ?
20. Differentiate between List and Tuple.
21. Discuss any five string functions with examples
22. Write a python program using object-oriented programming to demonstrate encapsulation, overloading
and inheritance
23. Write a program to perform sort elements using Insertion Sort.
24. Discuss any five string functions with examples. Write a Python program to swap cases of a given
string
25. Mention the key applications of Python. Why is Python called an Interpreted language?
26. Explain break, continue and pass statements with examples.
27. Write a Python program to print all prime numbers between a given range.
28. Create a student class and initialize it with name and roll number. Design methods to:
i. Display to display all information of the student
ii. setAge to assign age of student
iii. setMarks to assign marks of student
29. Use concept of inheritance to find total marks for the student
30. Differentiate between the following a List ,Tuple and Dictionary.
31. 6. Explain Variable-length arguments with examples.
32. 7. Consider the following two sets and write the Python code to perform following operations on them.

30 | P a g e
a. Lows = {0,1,2,3,4} Odds = {1,3,5,7,9}
i. Union ii) Difference iii) Symmetric difference iv) Intersection
33. 8. Write a Python program to read the contents of a text file and write into another.
34. What are the Steps in Visualization? Explain with an example
35. Explain the different types of graphs with syntax and examples in matplotlib.
36. Explain the process of Binning in Histograms with examples.
37. Differentiate between Bar graph and Histogram in matplotlib.
38. What do you mean by Normalization and Standardization? Write any five differences between them
39. Explain reshaping and pivoting in Pandas with examples.
40. Discuss Data Transformation techniques in Pandas.
41. Discuss any five string manipulation operations in Pandas.
42. What are the different ways to read and write in text files using Pandas. Give methods and examples.
43. Write python code to interact with database and perform the following task
a. Create table
b. Insert 3 records into the table
c. Display all records
44. Give example to load a database table into a dataframe and find the number of missing values

31 | P a g e

You might also like