You are on page 1of 18

PYTHON REVISION TEST

QUESTIONS:
THEORY
1. What are Variables?
Answer:It is a container which is used to contain values in the program
It helps to generate a memory for the values
2. What is the use input( ) in Python?
Answer:To take input from the user whatever you enter as input, the input function
converts it into a string.
3. Difference between sep and end in Python?
Answer:The end parameter basically prints after all the output objects present in one
output statement heve been returned.
The sep parameter diffentiate between the objects.
4. What is the default type of input function?
Answer:Int
5. Write a simple code to format string values?
Answer:
a=5
b=10
print(‘the value of a is {} and value of b is {}.format(a,b))
output;
the value of a is 5 and value of b is 10
6. If a = 5; print(‘a’); What will be the output?
Answer:
Output=a
7. What are Identifiers?
Answer:
It is user defined names which is given to identify variables functions classes modules
or objectives
8. Whether an Identifier starts with number?
Answer:
Identifier name should not starts with numbers
9. What are Keywords and name some in Python?
Answer:
Keywords are the reserved words in python. It is used to perform some specified task.
We cannot use a keyword as a variable, function or class.
Ex: and,or,not,if,elif,true,false,def,break,assert,while
10. Name the Datatypes in Python?
Answer:
Number-int,float,complex numbers
String-slice operator, Concatenation +, Asterisk *
Boolean-True,False
11. What is the use of Slice Operators?
Answer:
The slice operator is used to specify how slice a sequence
It is a feature that enables accessing parts of sequence like strings and lists
12. What is Concatenation?
Answer:Joining strings together and-to-end to create a new string
13. Whether two Integer values can be Concatenated?
Answer:yes
14. Types of Operators?
Answer:Arithmetic, comparison ,logical, assignment, bitwise, identity, membership
operator
15. Difference between Comparison and Logical Operator?
Answer:Comparison operator is used to compare the input or output
Logical operator is used to check the conditions if it is true or false
16. Difference between Arithmetic and Assignment Operator?
Answer:Arithmetic operator is used to perform mathematical operator
Assignment operator is used to assign or modify the values
17. What is Type Conversion?
Answer:The process of converting the value of one data type to another datatype is
called type conversion.
18. Difference Between Implicit and Explicit Conversion?
Answer:Implicit conversion is used to convert the smaller datatype to higher datatype
Explicit conversion is used to convert the higher datetype to smaller datatype
19. Which Type Conversion is build-in in Python?
Answer:Explicit type onversion
20. Name some Types Conversions in Python?
Answer:Int(),float(),str(),ord(),hex(),oct(),complex()
21. What is Comment in Python?
Answer:Comment is reduces figuring time of the program
22. What is Statements in Python?
Answer:A statement is an instruction that the python interpreter can execute
23. What is Control Structure?
Answer:It gives continuous result in python
24. Types in Control Structures?
Answer:
• Decision making statement:if,elif,else
• Looping statement:for while
25. Difference between Decision Making and Looping Statements?
Answer:
decision the statement execute either one or not execute on the basis of the condition
provided.
in loop the statement execute several times until the condition would not get false
26. Difference between for and while Loop?
Answer:
For loop allows a programmer to execute a sequence of statements several times it
abbreviates the code which helps to manage loop variables
While loop allows programmer to repear a single statement or a group of statement
for the true condition
27. What is the use of range( ) function?
Answer:
Used to generates a sequence of numbers over time at its simplest it accepts an integer
and return a range object
28. Various Parameters in range( ) function?
Answer:
It has 3 parameters:
• The range(stop) takes one argument
• Range(start,stop) takes two arguments
• Range(start,stop,stop)takes three arguments
29. What is the default starting range in range( ) function?
Answer:
0(zero)
30. What is the default incrementation value in range( ) function?
Answer:
One
31. Difference between break, continue and pass?
• break
We can stop the loop before it has looped through all the elements.
• continue
We can stop the current iteration of the loop, and continue with the next
• pass
Used to pass the set of block and continue the next

32. What is Data Structure?


Answer:
Organising managing and staring data is important as it enables easier access and
efficient modification.
33. Explain Data Structure Types?
Answer:
Built-in data structure list,tuple,set,dictionary
User -defined data structure
34. Advantages in Data Structure?
Answer:
• Efficient problem solving
• Focus on main problems without getting into details
• Consistent way
• Information is securely stored
35. What is List, Tuples, Sets, Dictionary?
Answer:
List: In order to store multiple number of values, we are using List.
Tuples: We can store collection of both similar and different datatype values.
Sets: Same like List. But stores only unique values.
Dictionary: Storing values in Dictionary will speed up in searching data
36. Difference between List and Tuples?
Answer:
List:
• lists are mutable
• lists have many built in functions
• enclosed by[]
tuple:
• tuples are immutable
• tuple does not have many built in functions
• enclosed by()
37. Difference between List and Sets?
Answer:
List:
• have stored only one value to the variable.
• In order to store multiple number of values, we are using List.
• Due to this, List is called collection of Similar and Different Datatype Values.
• In Simple, List is used for grouping the multiple values together.
Sets:
• Same like List. But stores only unique values.
• It won’t allow duplicate values.
• We can store collection of both similar and different datatype values.
• Since it is Mutable, values can be changed.
• It will not store values in a sequence, it stores in random sequence.

38. Difference between List and Dictionary?


Answer:
List:
• have stored only one value to the variable.
• In order to store multiple number of values, we are using List.
• Due to this, List is called collection of Similar and Different Datatype Values.
• In Simple, List is used for grouping the multiple values together
Dictionary:
• It is a Collection of Key and Values.
• It is Key / Value pair.
• Storing values in Dictionary will speed up in searching data.
• No index consideration, only key consideration.

39. What is Key / Value Pair?


Answer:
A key-value pair (KVP) is a set of two linked data items: a key, which is a
unique identifier for some item of data, and the value, which is either the data that is
identified or a pointer to the location of that data

40. Explain few methods in Data Structure?


Answer:
append, insert, remove,pop, min,max,sort.
41. Difference between append( ) and insert( )?
Answer:
Append is used to add the elements at the end of list whereas insert adds the elements
at the mentioned index location.
42. Difference between remove( ) and pop( )?
Answer:
Remove function removes the particular element present in any location in the list
whereas pop element removes the last item in the list.
43. How to add multiples in a List?
Answer:
Using extend function
44. How to delete all the values in a List?
Answer:
Using clear command we can clear out the entire list.
45. Which method is used to calculate total data in Tuples?
Answer:
Count function
46. Why Set collection has no Indexing?
Answer:
Because in set elements are not stored in fixed manner the position of the elements are
changed during each execution so it has no indexing.
47. Can we add repeated values to the Tuples?
Answer:
No we cannot add or remove values inside the tuple because it is immutable.
48. Can we contain string Key in Dictionary?
Answer:
No
49. Can we Fetch data from Dictionary through Indexing?
Answer:
We can fetch data form the dictionary using get keyword
50. Dis-Advantages in List?
Answer:
No key/value pair is available in list.
51. What is Array?
Answer:
An array is a collection of items stored at contiguous memory locations. The idea is to
store multiple items of the same type together
52. Name some Type Codes in Python?
Answer: I,I,b,B-- for integers
f-float,d-double
53. Difference Between Signed and Unsigned Integer?
Answer: Signed integer consists of values of the datatype from negative value to
positive value whereas unsigned integer consists of only positive range.
54. Does the array library in Python 2D Array?
Answer:No only 1D array if want to use 2D or multi-dimentional array we can use
numpy
55. Types of Build-in Functions in Array and it’s Differences?
Answer:
• To find type code
• To find memory address of an array
56. What is Numpy Array and it’s Advantages?
NumPy stands for Numerical Python. It also has functions for working in domain of
linear algebra, fourier transform, and matrices. NumPy Arrays are very frequently
used in data science, where speed and resources are very important.
57. Why Numpy is faster than List?
Answer:NumPy arrays are stored at one continuous place in memory unlike lists, so
processes can access and manipulate them very efficiently
58. Build-in methods in Numpy Array and it’s differences?
• to find the array dimension- ndim
• to find size of each element in array - - itemsize
• to find datatype of array – dtype
• to find array shape – ary.shap
• to reshape array – ary.reshape
59. Ways to create Numpy Array’s in Python?
• Array
• Linspace
• arrange
60. Difference Between array( ), linspace( ) and arrange( )?
array() is used to create a numpy array
Linspace() consists of start value,end value and dividend.
Arange() has start value, end value and skip into parts.
61. Difference types of copy in Numpy?
• Narmol copy
• Shadow copy
• Deep copy
62. Difference between Shallow copy and Deep copy?
shallow copy -- it gives same values but different memory address
Deep copy – array elements are linked to each other.
63. What is Functions and Advantages?
A function is a block of code which only runs when it is called. You can pass data
into a function and it will return data as a result.
Advantages:
It provide code reusability.
It helps to avoid duplicate memory allocation
It improves program efficiency.
64. Difference between With and Without Arguments?
Without arguments if we call a function it will print whatever is inside the function. With
Argumnets the parameter values can be passed to the function during runtime and we can
get desired result.
65. Difference between Arbitrary and Arbitrary Keyword Arguments?
Arbitrary arguments: If we do not know how many arguments that will be passed into
your function, add * before the parameter name in the function definition
Arbitrary keyword arguments : If you do not know how many keyword arguments
that will be passed into your function, add two asterisk: ** before the parameter name
in the function definition.
66. Types of Variables?
• Local variables
• Global variables
67. Difference between Global and Local Variables?
Global variable is declared outside the function whereas local variable is declared
inside the function.
68. What is Anonymous Function?
Anonymous function means that a function is without a name. def keyword is used to
define the normal functions and the lambda keyword is used to create anonymous
functions.
69. What is Function Recursion?
A Function call by itself is called Recursion Function
70. Difference between Modules and Packages?
Modules consists of variable, function and class. Package consists of bundles of
modules.
PRACTICALS
1. Given a two list. Create a third list by picking an odd-index element from the first list
and even index elements from second.
a. listOne = [3, 6, 9, 12, 15, 18, 21]
b. listTwo = [4, 8, 12, 16, 20, 24, 28]

answer:
listOne = [3, 6, 9, 12, 15, 18, 21]

listTwo = [4, 8, 12, 16, 20, 24, 28]

listThird=listOne[0::2]+listTwo[1::2] print(listThird)
output:
[3, 9, 15, 21, 8, 16, 24]

2. Given an input list removes the element at index 4 and add it to the 2nd position and
also, at the end of the list?
a. List = [54, 44, 27, 79, 91, 41]

Answer:

List = [54, 44, 27, 79, 91, 41] List.pop(4)


List.insert(2,91) List.append(91)
print(List)

OUTPUT:
[54, 44, 91, 27, 79, 41, 91]

3. Given a list iterate it and count the occurrence of each element and create a dictionary
to show the count of each element?
a. list = [11, 45, 8, 11, 23, 45, 23, 45, 89]
b. Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}
Answer:

List = [11, 45, 8, 11, 23, 45, 23, 45, 89] print("Original list ", List)
countDict = dict()
for val in List:

if (val in countDict): countDict[val] += 1

else:
countDict[val] = 1

print("Printing count of each item ", countDict)


output:
Original list [11, 45, 8, 11, 23, 45, 23, 45, 89]
Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}

4. Remove duplicate from a list and create a tuple and find the minimum and
maximum number?
a. sampleList = [87, 45, 41, 65, 94, 41, 99, 94]
answer:

sampleList = [87, 52, 44, 53, 54, 87, 52, 53] print("Main List: ", sampleList)
sampleList = list(set(sampleList)) print("modified list:",sampleList)
tuple = tuple(sampleList)
print("tuple ", tuple)
minimum=min(tuple) maximum=max(tuple)
print("Minimum number is: ",minimum) print("Maximum number is: ",maximum)

output:
Main List: [87, 52, 44, 53, 54, 87, 52, 53] modified list: [44, 52, 53, 54, 87]
tuple (44, 52, 53, 54, 87)
Minimum number is: 44
Maximum number is: 87

5. Reverse the following tuple


a. aTuple = (10, 20, 30, 40, 50)
answer:

aTuple = (10, 20, 30, 40, 50)

new_Tuple = aTuple[::-1] print(new_Tuple)

output:
(50, 40, 30, 20, 10)

6. Swap the following two tuples


a. tuple1 = (11, 22)
b. tuple2 = (99, 88)
answer:

tuple1 = (11, 22)


tuple2 = (99, 88)
(tuple1, tuple2 )= (tuple2, tuple1) print('tuple1:',tuple1) print('tuple2:',tuple2)

output:
tuple1: (99, 88) tuple2: (11, 22)

7. Copy element 44 and 55 from the following tuple into a new tuple
a. tuple1 = (11, 22, 33, 44, 55, 66)
answer:

tuple1 = (11, 22, 33, 44, 55, 66)

new_tuple = tuple1[3:-1] print('new_tuple:',new_tuple)


output:
new_tuple: (44, 55)

8. Count the number of occurrences of item 50 from a tuple


a. tuple1 = (50, 10, 60, 70, 50)
answer:

tuple1 = (50, 10, 60, 70, 50)

print(tuple1.count(50))

output: 2

9. Write a program to store sequence of values using Array?


Answer:
ary=array(‘i’,[1,2,3,4,5])
Print(ary[i])
10. Write a program to copy one array values into another array?
Answer:
From numpy import*
Ary1=array([10,20,30,40,50])
Ary2=ary.copy()
Print(ary1)
Print(ary2)

11. Write a program to find the Last Array element in a given array?
i. array(‘i’,[24,55,67,88,92])
answer:
from array import*
vals=array(‘I’,[4,4,5,6,7])
print(vals[-1])
12. Write a program using Numpy and perform Math Operations in Python?
Answer:
From array import*
Ary1=array([10,0,30])
Ary2=array([2,4,6])
Add=ary1+ary2
Sub=ary1-ary2
Mul=ary1*ary2
Div=ary1/ary2
Print(ary1)
Print(ary2)
Print(add)
Print(sub)
Print(mul)
Print(div)

13. Write a program for 2*3 matrix using Numpy?


Answer:
Import numpy as np
Array1=np.array([10,20,30],[40,50,60])
Print(array1.shape)
Print(array1)
14. Write a program to reshape 2*3 dimension into 3*2 dimension using Numpy?
Answer:
Import numpy as n
Ary1=n.array([1,2,3],[4,5,6])
Print(array1)
Print(arry1.shape)
Print(array1.veshape(3,2))

15. Write a program to do Shallow copy using Numpy?


Answer:
Import numpy as *
Ary1=arrat([10,20,30,40])
Ary2=ary1.view()
Ary1[3]=100
Print(ary1)
Print(ary2)
Print(id(ary1))
Print(id(ary2))

16. Write a program to do Deep copy using Numpy?


Answer:
Import numpy as np
Ary1=([10,20,30,40])
Ary2=ary1.copy()
Print(ary1)
Print(ary2)
17. Write a Python function to sum all the numbers in a list.
18. Sample = [8, 2, 3, 0, 7]
Answer:
def sum(number):
num=0
for x in number:
num += x
return num

print(sum([8,2,3,0,7]))

19. Write a Python function to multiply all the numbers in a list.


Answer:
def sum(number):
num=1
for x in number:
num *= x
return num
print(sum([8,2,3,2,7]))
answer:
672

20. Sample = [8, 2, 3, 2, 7]


21. Write a Python function to calculate the factorial of a number.
Answer:
def fact(n):
if n==0:
return 1
else:
return n * fact(n - 1)
print(fact(10))
output:
3628800

22. Write a Python function to print the even numbers.


Answer:
sample=[0,1,2,3,4,5,6,7,8,9,10]
even=list(filter(lambda sample: (sample % 2==0),sample));
print('even numbers in the list: ',even)
output:
even numbers in the list: [0, 2, 4, 6, 8, 10]
23. Create a function that can accept two arguments name and age and print its value.
Answer:
def function(name,age):
print(name + " is " + age + " years old")
function("vidya","20")
24. Write a function func1() such that it can accept a variable length of argument and
print all arguments value.
ANSWER:
def func1(*args):
for i in args:
print(i)

func1(10, 20, 30)


func1(40, 50)

output:
20
40
60
80
100

25. Write a function calculation() such that it can accept two variables and calculate the
addition and subtraction of it. And also it must return both addition and subtraction in
a single return call.
Answer:
def calculation(a,b):
sum = a+b
sub = a-b
return sum,sub
sum,sub=calculation(20,10)
print('addition:',sum)
print('substraction:',sub)

output:
addition: 30
substraction: 10

26. Create a function showEmployee() in such a way that it should accept employee
name, and it’s salary and display both, and if the salary is missing in function call it
should show it as 9000.
Answer:
def showEmployee(name,salary):
print("The employee name is {} and his/her salary {} ".format(name,salary))
showEmployee("john",9000)

output:
The employee name is john and his/her salary 9000

27. Write a recursive function to calculate the sum of numbers from 0 to 10.
Answer:
def recurSum(n):
if n <= 1:
return n
return n + recurSum(n - 1)
# Driver code
n = 10
print(recurSum(n))
output:
55
28. Assign a same name to two different function and call it through the different
parameters.
Answer:
def area(l, b):
return l * b
def area(r):
import math
return math.pi * r ** 2
print(area(3))
output:
28.274333882308138

29. Print multiplication table of 24, 50 and 29 using loop.


Answer:
num=int(input("Enter the number for multiplication table: \n"))
#get input from user
i=0
while i<=num: #use for loop to iterates 1 times
i+=1
print(num,'x',i,'=',num*i)

30. Take 10 integers from keyboard using loop and print their average value on the
screen.
Answer:
sum=0
count = 1
while (count<=10):
x = int(input("Enter the %d number?"%(count)));
sum = sum + x;
count=count+1;
average = sum / 10;
print("Average using

You might also like