Introduction to Python (Question and Answers)
Q.1 What is the use of * Operator in a list?
Ans. The Operator * is used to replicate a list by a specific number of times. With * operator
one operand has to be list and other should be an integer otherwise it will give an error.
Eg. COMMAND OUTPUT
A= [ 1, 2, 3] [1,2,3,1,2,3,1,2,3]
B= A*3
Print(B)
Q.2 what are Iterations? Explain two different ways of using Iterations?
Ans. Repetitive execution of the same block of the code over and over are referred to as
Iterations. The two different ways of using Iterations are in Python are While loop and for loop.
For loop – It is used to repeat a set of instructions for a fixed number of times. It is called
definite loop.
Eg. COMMAND OUTPUT
For i in[1,2,3,4]: 1
print i 2
While loop – It is used to repeat a set of instructions as long as the condition is true.It means
number of
Iterations are not fixed. It is also called Entry controlled loop.
Eg. Count =1 OUTPUT
While Count <=4 hello
Print (“hello’) hello
Count +=1 hello
Print (“Program ends”) hello
Program ends
Q.3 What is Type Conversion ? Explain the types of type conversion with the help of example.
Ans. Type Conversion in Python refers to the direct conversion of object one data type to
another data type.
For Example – the conversion of an integer value into a floating value or its textual
representation as a string and vice versa.
Example # declare a variable with OUTPUT
# integer value x is a type :< class ‘int’ =””>
x=10
# check type of x
Print (“x” is a type “, type (x))
Q.4 Explain three different types of Errors in Python .
Ans. The three different types of Errors in Python are :
1. Syntax error – It means writing a code following the rules of Python language. It occurs
when we violate the rules of Python.
Eg. a + b = c
Syntax error : cannot assign operator.
2. Logical Error – This type of error occurs when we give a wrong formula for the
calculation to be done. , write a wrong logic for the problem to be solved through the
code.
Eg. To calculate the average :
P= marks1 + marks 2 / 2 #instead of (marks1+marks2) /2
3. Runtime error - It occurs during the execution pof program like wrong input or output
errors , undefined object division by zero errors.
Eg. a = int (input (“ enter first number”))
b = int (input (“enter second number”))
c = a/b
If a=5 and b=0 , then it will display:
ZeroDivisionError: division by zero
Q.5 What is the difference between pop() and remove()?
Ans. The pop() function removes the last element or the element based on the index given
remove()function removes the first occurrence of the specified element.
Eg. marks = [ 23,34,23,45,56,78,56 ] OUTPUT
marks.remove(23) marks = [34,23,45,56,78,56]
print (marks)
vowels = [ ‘a’ ,’e ’,’i’ ,’o’ ,’u’ ] OUTPUT
val=vowels.pop(2) ‘I’
print (val)
Q.6 What is nested if statement?
A nested if statement is an if-else statement with another if statement as if the body or else the
body.
The syntax of if..elif..else statement is as follows :
If (condition ):
Statement 1
elif (condition):
Statement 2
elif( condition ):
Statement 3
…
Else:
Statements executed when all above conditions are false.