Python Question-bank[1] (1)
Python Question-bank[1] (1)
Page 1 of 13
4. cost effective approach
5. support multiple programming paradigms
6. adopt test driven development
4. Explain input command in python with suitable example.
Ans. : In Python, we use input() function to take input from the user.
Whatever you enter as input, the input function converts it into a string. If
you enter an integer value still input() function convert it into a string.
Syntax: input(prompt)
Ex. String= input()
Print (String)
5. Explain print command in python with suitable example.
Ans. : print() function prints the message to the screen or any other
standard output device.
Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
Ex. : print(“Hello”)
6. State which of the following python statements are valid and invalid.
a. print("ksk", + "123") invalid
b. print("ksk" '+' "123") valid
c. print("ksk", '+', "123") valid
d. print('ABC is a "technological" University') valid
e. print(2 + '3') invalid
f. print('2' * 3) valid
7. What will be the output of following print statements?
a. print('abc' * 2) abcabc
b. print(3+4j + 2+1j) (5 + 5j)
c. print(3*2 // 4) 1
8. State which of the following python statements are valid and invalid.
a. x = input("enter a number") valid
b. x = input() valid
c. x = input("") valid
d. x = input(" ' ") invalid
e. x = input(2) valid
f. x=input("2" + "3") valid
Page 2 of 13
9. What will be the value of variables x, y & z after execution of following
python program. Assume that user enters a value 5.
temp=input("enter a number")
x=temp*5
y=int(temp)*5
z=bool(temp*0)
10. What will be the output of following program? Assume that user will enter
only integer value.
temp=input("enter a
number") x=temp*0
y=int(temp)*0
z=bool(temp*0) print(x)
print(y) print(z)
Ans. X= 55555 y = 25 and z = false
if user enter 5 as integer
value
12. Rewrite following code with proper indentation to get the output as
Expected output Code without indentation
Page 3 of 13
cube of 1 is: abc = [1,2,3,4,5] for x
1 square of in abc: if x%2 == 0:
2 is: print("square of ", x ,
4 cube of "is:")
3 is: print(x**2)
27 square else:
of 4 is: print("cube of ", x , "is:")
16 cube print(x**3)
of 5 is:
125
Page 4 of 13
6. Exponentiation:- ** Ex. :- x**y
7. Floor division:- // Ex. :- x//y
15. Explain any six assignment operators of python with suitable examples of
each.
Ans. : Assignment operators are used to assign the values to the variables.
1. Assign (=) : assign values of the right side of expression to the left
side operand. X = y+z
2. Add and Assign(+=) : this operand is used to add the right side
operand to the left side operand and store the result in the left side
operand. Ex. : X += Y
3. Subtract and Assign(-=)This operand is used to subtract right side
operand from the left side operand and store the result in the left
side operand. Ex. : X -= Y
4. Multiply and Assign(*=) this operand is used to multiply right side
operand with the left side operand and store the result in the left
side operand. Ex.: X *= Y
5. Divide and Assign(/=) : this operand is used to divide the left
operand to the right operand and then store the result in the left
operand. Ex. : X /= Y
6. Modulus and Assign (%=) : this operator is used to take the modulus
using the left and right operand and the store the result in the let
operand. Ex. : X %= Y
7. Divide(floor) and Assign(//=) : This operator is used to divide the
left operand with the right operand and then assigning the
result(floor) to the left operand. Ex. X //= Y
8. Exponent and Assign: This operator is used to calculate the
exponent(raise power) value using operands and then assigning
the result to the left operand. Ex. X **= Y
16. Explain all comparison operators of python with suitable examples of each.
Ans. : 1. Greater than: This operator returns True if the left operand is
greater than the right operand. Syntax: x > y
2. Less than : this operator returns true if the left operand is less
than the right operand , Syntax : X < Y
3. Equal to : this operator returns true if the left operand is equal to
the right operand . Syntax : X == Y
4. Not equal to : this operator return true if the left operand is not
equal to the right operand, Syntax : X != Y
Page 5 of 13
5. Greater than or equal to : this operator return true if the left
operand is greater than or equal to the right operand , Syntax : X >= Y
6. less than or equal to : this operator return true if the left operand
is less than or equal to the right operand , Syntax : X <= Y
17. Explain “and, or, not” operators of python with suitable examples of each.
Ans : AND – Logical AND true if both X and Y operands are true, Syntax: X and
Y.
EX. : a=5
b=6
c=1
if a > b and a > c:
print("a is greater")
elif b > a and b > c:
print("b is greater")
else:
print("c is greater")
OR – Logical OR true if either of the operand is true means any one of them
operand is true, Syntax : X or Y
Ex. :
NOT – Logical NOT true if operand is false, Syntax : not x
Page 6 of 13
ANS. : 1. & Bitwise AND operator : Operator copies a bit to the result if it exist
in both operands.
2. | Bitwise or operator: It copies a bit if it exists in either operand.
3. ~ Bitwise not operator: it returns 1’s complement of number.
4. ^ Bitwise xor operator: It copies the bit if it is set in one operand but
not both.
5. << (Binary Left Shift) : The left operand’s value is moved left by the
number of bits specified by the right operand.
6. >> (Binary Right Shift) : The left operand’s value is moved right by the
number of bits specified by the right operand.
EX. a = 0011 1100
b = 0000 1101
1. a & b = 12(0000 1100)
2. a | b = 61(0011 1101)
3. a ^ b = 49(0011 0001)
4. ~a = -61
5. a << 2 = 240(1111 0000)
6. a >> 2 = 15(0000 1111)
Page 7 of 13
ANS : some times it is required to execute group of statements repeatedly
that time for loop is used.
Syntax : for variable in sequence :
Statement 1
Statement 2
EX : s= “orchid”
for x in s:
print(x)
o/p : o
r
c
h
i
d
22. Explain break, continue and pass statement with suitable example of each.
Ans : 1. Break statement :- The break statement is used to terminate the loop
or statement in which it is present. After that, the control will pass to the
statements that are present after the break statement, if available. If the break
statement is present in the nested loop, then it terminates only those loops which
contains break statement.
Syntax : break
EX.
2. continue statement:- Continue is also a loop control statement just like the
break statement. continue statement is opposite to that of break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or execute
the next iteration. When the continue statement is executed in the loop, the code inside
the loop following the continue statement will be skipped and the next iteration of the
loop will begin.
Syntax : continue
EX.
3. Pass statement :- As the name suggests pass statement simply does nothing.
The pass statement in Python is used when a statement is required syntactically but you
do not want any command or code to execute. It is like null operation, as nothing will
Page 8 of 13
happen is it is executed. Pass statement can also be used for writing empty loops. Pass
is also used for empty control statement, function and classes.
Syntax : Pass
EX.
23. Differentiate between List, Tuple & Set with suitable examples of each.
Ans :
Page 9 of 13
24. Explain any three collection data types of Python with suitable examples of
each.
Ans : 1 . Numeric : In Python, numeric data type represent the data which has
numeric value. Numeric value can be integer, floating number or even complex
numbers. These values are defined as int, float and complex class in Python.
• Integers – This value is represented by int class. It contains positive or
negative whole numbers (without fraction or decimal). In Python there is no
limit to how long an integer value can be.
• Float – This value is represented by float class. It is a real number with floating
point representation. It is specified by a decimal point. Optionally, the
character e or E followed by a positive or negative integer may be appended
to specify scientific notation.
• Complex Numbers – Complex number is represented by complex class. It is
specified as (real part) + (imaginary part)j. For example – 2+3j
2. Sequence type : In Python, sequence is the ordered collection of similar or
different data types. Sequences allows to store multiple values in an organized and
efficient fashion. There are several sequence types in Python –
• String - A string is a collection of one or more characters put in a single quote,
double-quote or triple quote. In python there is no character data type, a
character is a string of length one. It is represented by str class.
• List - Lists are just like the arrays, declared in other languages which is a
ordered collection of data. It is very flexible as the items in a list do not need
to be of the same type.
• Tuple - tuple is also an ordered collection of Python objects. tuples cannot be
modified after it is created. It is represented by tuple class.
3. Boolean : Data type with one of the two built-in values, True or False. Boolean
objects that are equal to True are truthy (true), and those equal to False are falsy
(false). But non-Boolean objects can be evaluated in Boolean context as well and
determined to be true or false. It is denoted by the class bool.
3. Set : Set is an unordered collection of data type that is iterable, mutable and has
no duplicate elements. The order of elements in a set is undefined though it may
consist of various elements.
4. Dictionary : in Python is an unordered collection of data values, used to store data
values like a map, which unlike other Data Types that hold only single value as an
element, Dictionary holds key:value pair. Key-value is provided in the dictionary to
make it more optimized. Each key-value pair in a Dictionary is separated by a colon :,
whereas each key is separated by a ‘comma’.
Page 10 of 13
25. Explain append() and copy() methods of list with suitable examples of each.
Ans : 1. append() Used for appending and adding elements to List. It is used to
add elements to the last position of the List in Python.
Syntax: list.append (element)
EX. : List = ['Mathematics', 'chemistry', 1997, 2000]
List.append(20544)
print(List)
2. Copy() It returns a shallow copy of a list
Syntax : list.copy()
EX. : lis1 = [ 1, 2, 3, 4 ]
lis2 = lis1.copy()
print(lis2)
26. Explain count() and index() methods of list with suitable examples of each.
Ans : 1. The count() method is one of the inbuilt functions in Python. As the name implies, it
returns the number of times a specified value appears in a string or a list. This method is also
used to count numbers in the given set of arrays. There are two types of methods for ‘count’ in
Python. They are as follows:
1. String count() method
2.List count() method
EX : myText = "I love Paris, Paris is my favorite tourist destination"
numofCounts = myText.count("Paris")
2. Index() : Python index() is an inbuilt function in Python, which searches for a
given element from the start of the list and returns the index of the first
occurrence.
Syntax: list_name.index(element, start, end)
EX : list2 = ['cat', 'bat', 'mat', 'cat', 'pet']
print(list2.index('bat'))
Page 11 of 13
lis.insert(1, "For")
print(lis)
2. Python List remove() is an inbuilt function in the Python programming
language that removes a given object from the List.
Syntax: list_name.remove(obj)
EX : list1 = [ 1, 2, 1, 1, 4, 5 ]
list1.remove(1)
print(list1)
29. Explain clear() and extend() methods of list with suitable examples of each.
Ans : 1. The clear() method removes all the elements from a list.
Syntax : list.clear()
EX : fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
2. extend() method iterates over an iterable like string, list, tuple, etc., and adds
each element of the iterable to the end of the List, modifying the original list.
Syntax : list.extend(iterable)
EX : l = [1, 2, 3]
Page 12 of 13
l.extend([4, 5, 6])
print(l)
30. Explain pop() and sort() methods of list with suitable examples of each.
Ans : pop() is an inbuilt function in Python that removes and returns the last value
from the List or the given index value.
Syntax : list_name.pop(index)
EX : l = [1, 2, 3, 4]
print("Popped element:", l.pop())
print("List after pop():", l)
2. remove() :- This function is used to delete the first occurrence of number
mentioned in its arguments.
Syntax : list_name.remove(index)
EX : lis = [2, 1, 3, 5, 3, 8]
lis.remove(3)
Ans : False
Page 13 of 13