0% found this document useful (0 votes)
30 views

Python Interview Questions

Python is a powerful, general purpose, high level, object oriented programming language developed by Guido Van Rossum in 1991. It is very simple and easy to learn, powerful, fast and secure. Python has applications in web development, software development, database applications, scientific computing, business applications and more. It is an interpreted language that uses indentation to specify blocks of code and is case sensitive. Common data types in Python include lists, dictionaries, strings and tuples.

Uploaded by

Hafsa Lateef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Python Interview Questions

Python is a powerful, general purpose, high level, object oriented programming language developed by Guido Van Rossum in 1991. It is very simple and easy to learn, powerful, fast and secure. Python has applications in web development, software development, database applications, scientific computing, business applications and more. It is an interpreted language that uses indentation to specify blocks of code and is case sensitive. Common data types in Python include lists, dictionaries, strings and tuples.

Uploaded by

Hafsa Lateef
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PYTHON

INTERVIEW QUESTIONS

Top Most Important Python Interview Questions & Answers

PREPARED BY WORKING PROFESSIONAL

1000+ DOWNLOADS

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 1

1.What is Python?
Answer:

ECH
It is powerful, general purpose, high level, object oriented programming language.
It’s developed by Guido Van Rossum in 1991..

2. Why we use Python?


Answer:
It is very simple and easy to learn.
It powerful, fast and secure.
It has very simple syntax.
It is powerful scripting language.
It can be run on different kind of platform like Windows , Mac, Linux, etc.,

3.What are the applications developed using Python?


Answer:
Web Application
Software Development
Database GUI application
Scientific and Numeric Computing
SYST
Business Applications
Console Based Application

4.What are the features in Python?


Answer:
Independent Platform
Object oriented
Flexible
Structure oriented
Portable
Simple

5.Is python, case sensitive language or not?


Answer:
Yes, python is a case sensitive language

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 2

6.Why indentation is required in Python?


Answer:

ECH
Indentation is necessary for Python. It specifies a block of code. All code within loops,
classes, functions, etc. And also it is specified within the indented block. It is usually done
by using four space characters. If your code is not indented necessarily, it will not execute
accurately and will throw errors as well.

7.What is an iterator in Python?


Answer:

An iterator is an object which implements the iterator protocol.


It has a __next__() method which returns the next item in iteration, also iterators are
objects which can iterate objects like list, string, etc.

8.Why python is called an interpreted language?


Answer:
An interpreted language is any programming language which is not in machine level
code before run time. Therefore, python is an interpreted language.
SYST
9.What is comment?
Answer:
Python comments are statements that are not executed by the compiler.
It is not considered as a part of program.
.

10.What are the types of comments in Python?


Answer:
There are two types of comment in Python.
Single line comment
Multi line comment

11.What is single line comment?


Answer:
It is used to comment only one line.
Hash (#) is used for single line comment.

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 3

12.What is multiline comment?


Answer:

ECH
It is used to comment a block of code.
Triple quotes are used to multiline comment starts with ‘’’ and ends with ‘’’.

13.What is Keyword?
Answer:
The word which is predefined in the library is called keyword.
Keywords cannot use as a variable function name, class name or any other identifier.

14.What is Float function?


Answer:
Float () is a predefined function which is used to convert given data into float.

15.What is int() function?


Answer:
int() is a predefined function which is used to convert string into integer.
SYST
16.What is print in Python?
Answer:
It is a predefined function which is used to print data or information..

17.How can you convert a number to a string?


Answer:
In order to convert a number into a string, use the inbuilt function str(). If you want a
octal or hexadecimal representation, use the inbuilt function oct() or hex().

18. What is Global variable?


Answer:
Variable are only referenced outside of that function known as global variable

19.What is an Operator?
Answer:
It is a special symbol which is used to perform logical or mathematical operation on
data or variable

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 4

20.What are the types of operator in Python?

ECH
Answer:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Bit-wise operators
Membership operators
Identity operators

21. What is Operand?


Answer:
It is a data or variable on which the operation is to be performed

22. What is the use of // operator in Python?


Answer:
It is a Floor Division operator, which is used for dividing two operands with the result
as a quotient showing only digits before the decimal point. For instance, 10//5 = 2 and
10.0//5.0 = 2.0
SYST
23.What is the purpose of relational operators in Python?
Answer:
The purpose of relational operators in Python is to compare values.

24.What are assignment operators in Python?


Answer:
The assignment operators in Python can help in combining all the arithmetic operators
with the assignment symbol.

25.What are membership operators?


Answer:
With the operators ‘in’ and ‘not in’
print('me' in 'disappointment')
o/p: True

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 5

26.How to differentiate Identity operators & Membership operators?

ECH
Answer:
Unlike membership operators, the identity operators compare the values to find out if
they have the same value or not

27.What is import method?


Answer:
Import is a keyword, to access the script from another python file or module.

28.What is Input?
Answer:
Input () is a predefined function which is used to take user input in python.
Default user input is of type string

29.What is “if statement”?


Answer:
If the condition is true its body will execute otherwise does not execute

30.What is meant by for loop?


SYST
Answer:
For loop is used for sequential traversal.it can be used to traverse string or array. For
loop is used to iterate over a sequence list, string, tuple, etc., Iterating over a sequence
is called traversal.

31.How to print the star (*) pattern without newline and space?
Answer:
Code to print the star(*) pattern without newline and space:
for i in range(0, 20):
print('*', end="")

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 6

32.How will you create the following pattern using Python?


Answer:

ECH
*
**
***
****
o/p:
for i in range(1,6):
for j in range(1,i+1):
print('*',end='')
print(‘\n’)

33.What is list in Python?


Answer:
It is a collection of data of different data type.
It is used to store list of values.
A list is created by comma separated values between square brackets.

34. What is clear () function in list?


Answer:
SYST
This function is used to empty the list.

35.What are the built-in -type does python provides?

Answer:
Mutable:
List
Sets
Dictionaries
Immutable:
Strings
Tuples
Numbers

36.How can you count duplicate elements in a given list?


Answer:
Count duplicate elements in a given list
list1 = [2, 3, 4, 3, 10, 3, 5, 6, 3]
e = list1.count(3)
print('The count of element: 3 is ', e)

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 7

37.What is meant by continue statement?

ECH
Answer:
It is used to skip the next statement and continue the loop. This mostly used with loop.

38.What is copy () function in list?


Answer:
This function copies the elements one list into another.

39.What is count function in list?


Answer:
This method counts the number of occurrence of particular item in a list.

40.What is docstring in Python?


Answer:
Documentation string or docstring is a multiline string used to document a specific
code segment.
The docstring should describe what the function or method does.

41.What is meant by extend in list?


SYST
Answer:
This function is used to join two lists

42.What is append() function in list?


Answer:
It is used add the new element at the end of the list.

43.What is break statement?


Answer:
It terminates the current loop. Mostly used in for and while loop.

44.What is negative index in python?


Answer:
Python sequence can be index in positive and negative numbers.
For positive index, starts with 0 an soon. And negative index starts with (-1) in the last
index.

45.What is meant by Pass Python?


Answer:
Pass means no operation python statement

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 8

46.What is reverse function in list?


Answer:

ECH
This function reverses elements of the list

47.What is pop () function in list?


Answer:
This function deletes the element of given index.it deletes last item if we do not pass
index.

48.What is insert () function in list?


Answer:
Insert function is used to add new items into list at particular index.

49.What is meant by jump statement?


Answer:
It is used to transfer the control from one point to another point in the program.

50.What is sort function in list?


Answer:
This function sorts the list in ascending order or descending order.
SYST
51.What does len() do?
Answer:
It is used to determine the length of a string, a list, an array, etc.

Example:
S="ABCD"
print(len(S))

52. What is meant by Tuple?


Answer:
It is a collection of data of different data types.
We cannot change the value of tuples.
A tuple is created using parentheses.

53.Explain the output of the following piece of code?


Answer:
tuple=(123,'mani',20,'trichy')
tuple*=2
print(tuple)
o/p: (123, 'mani', 20, 'trichy', 123, 'mani', 20, 'trichy')
stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 9

54. What does the following code give us?


Answer:

ECH
b=(1)
print(type(b))
b=(1,)
print(type(b))

55.What is difference between python Arrays and Lists?


Answer:

Arrays:
Arrays in python can only contain same data type of elements.
Homogeneous
Consumes far less memory that lists
Lists:
List in python can contain elements of different data types.
Heterogeneous
Consuming large memory

56.What will the following code output?


SYST
Answer:
word=’abcdefghij’
word[:3]+word[3:]
The output is ‘abcdefghij’. The first slice gives us ‘abc’, the next gives us ‘defghij’.

57.What is the difference between List and Tuple?


Answer:
The difference between list and tuple is that list is mutable while tuple is not. Tuple can
be hashed,
For example, as a key for dictionaries

58.Describe about Slicing?


Answer:
A mechanism to select a range of items from sequence types like list, tuple, strings etc.,
is known as slicing.

59.What are sets in python?


Answer:
It is an unordered collection of data of different data types.
Set does not contain duplicate elements. A set is created using curly brackets

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 10

60.What is string in python?


Answer:

ECH
String is a collection of characters.it is created by using single quotes or double quotes.

61. How will you capitalize the first letter of string?


Answer:
In Python, capitalize () method capitalizes the first letter of a string. If the string
already consists of a capital letter at the beginning, then, it returns the original string..

62. How will you convert a string to all lowercase


Answer:
To convert a string to lowercase, lower() function can be used.
S="ABCD"
print(S.lower())

63.What if you want to toggle case for a Python string?


Answer:
print('SystECh'.swapcase())

o/p:
sYSTecH
SYST
64.How does break, continue and pass works in python?
Answer:

Allows loop termination when some condition is met and the control is
Break
transferred to the next statement.

Allows skipping some part of a loop when some specific condition is


Continue
met and the control is transferred to the beginning of the loop

Used when you need some block of code syntactically, but you want
Pass to skip its execution. This is basically a null operation. Nothing
happens when this is executed.

65.What is function in python?


Answer:
It is a collection of statement that performs on a specific task.
It executes when it is called by its name
stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 11

66.What is function overriding?

ECH
Answer:
Function with same name and same parameters is called function overriding.

67.What is the split function used for?


Answer:
The split function breaks the string into shorter strings using the defined separator.
It returns the list of all the words present in the string.

68.What is meant by Parameters and Arguments?


Answer:
Parameters are the names listed in the function definition.
Arguments are the values passed to the function while invoking.

69.What are packing operators in Python? How to use them?


Answer:
The packing operators are used to collect multiple arguments in functions.
They are known as arbitrary arguments.
SYST
70.What is Local variable?
Answer:
If a variable is assigned anywhere within the function’s body its assumed to be local.

71.What are python libraries?


Answer:
Python libraries are a collection of python packages.

Some of majorly used python libraries are;


NumPy
Pandas
Matplotlib
scikit-learn,
PyTorch and many more.

72.What are modules in python?


Answer:
A module is a collection of statement in which we store functions, classes and
variables.

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 12

73.What are some of the most commonly used built-in modules in Python?

ECH
Answer:
Python modules are the files having python code which can be functions, variables or
classes. These go by .py extension.
The most commonly available built-in modules are:

Os
Random
Datetime
Sys
Math

74.What is a lambda function?


Answer:
A lambda is an anonymous function. This function can have any number of
parameters but, can have just one statement.

Example: X=lambda a,b:a8b


Print(X(2,4))
SYST
75.Does Python have Oops concepts?
Answer:
Python is an object-oriented programming language .This means that any program can
be solved in python by creating an object model. However, python can be treated as
well as structural language.

76.What is class in Python?


Answer:
It is a collection of data members and member’s functions.
Data members are the variable used inside class
Member functions are the function used inside class
It is also called user defined data type

77.What is Parent class?


Answer:
The class which is inherited by another class is called parent or base class.

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 13

78.What is Child class?


Answer:

ECH
The class which inherits the property of another class is called child or sub or derived
class.

79.What is self in python?


Answer:
Self is an instance or an object of a class .it helps to differentiate between the methods
and attributes of a class with local variables.

80.Does python support multiple inheritances?


Answer:
Multiple inheritances mean that a class can be derived from more than one parent
classes. Python does support multiple inheritances.

81.What are the class variables in python?


Answer:
Private variable
Public variable
Protected variable
SYST
82.What is inheritance?
Answer:
The process of getting property of one class into another class is called inheritance

83.What is __init__ in python?


Answer:
__init__ is a method or constructor in python. This method is automatically called to
allocate memory when a new object is created. All classes have the __init__ method.

84.What is operator overloading?


Answer:
Operator overloading in python is a single operation based on the class (type) of
operands. Python operators work for built –in classes, the same operator behaves
differently with different types.

85..What is Encapsulation in python?


Answer:
Encapsulation is one of the fundamental concepts in object-oriented programming
(OOP). It describes the idea of covering data and the methods that work on data within
one unit.
stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page 14

86.What is polymorphism in python?


Answer:

ECH
Polymorphism means the ability to take multiple forms. So, for instance, if the parent
class has a method named ABC then the child class also has a method with the same
name ABC having its own parameters and variables. Python allows polymorphism.

87.What is Data Abstraction?


Answer:
Abstraction means data hiding
If we want to perform data hiding then it can be done by using (__) prefix with variables
then they cannot be accessed outside that function

88.What is file handling in python?


Answer:
File handling is a mechanism to store the data on the disk permanently.
There are several functions for creating reading, writing, updating and deleting files.

89.What is file operation in python?


Answer:
1. Opening of file.
2. Writing into a file
SYST
3. Appending data into a file.
4. Reading from a file.
5. Closing of file.

90.What is read () function in file handling?


Answer:
read () function is used read content of a file.

91.How do you open a file for writing?


Answer:
_file=open('filename.txt','w')

This opens the file in writing mode. You should close it once you’re done.

file.close()

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page15

92.Give example of sleep() function in Python


Answer:

ECH
Example of sleep() function in Python

Import time
print("Welcome to SYSTECH GROUP")
time.sleep(5)
print("This message will be printed after a wait of 5 seconds")

93.What are Reasons of Exception?


Answer:
Mismatched input:

Suppose that we are entering our name in place of age, causing exception because age
is data type int and name will be string.
Defined Data type:

Class keyword is used to create class

94.What is try block?


Answer:
SYST
It is the place where actual code is written and exception occurs.

95.What is except block?


Answer:
Except block is intended to catch the error and handle the exception condition. We can
have multiline except blocks to handle different types of exception and perform
different actions when the exceptions occur.

96.What is finally block?


Answer:
This block executes either exception occurs or does not occurs.

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page16

97. Explain important Python errors?


Answer:

ECH
The important Python errors are
1) Arithmetic Error, 2) Import Error, and 3) Index Error.

Arithmetic Error:
Arithmetic Error acts as a, base class for all arithmetic exceptions. It is raised for errors
in arithmetic operations.

Import Error:
Import Error is raised when you are trying to import a module which does not present.
This kind of exception occurs if you have made a typing mistake in the module name or
the module which is not present in the standard path.

Index Error:
An Index Error is raised when you try to refer a sequence which is out of range.

98.What is the output of the following code?


Answer:
SYST
first = [1, 2, 3, 4, 5]
second = first
second.append(6)
print(first)
print(second)
o/p:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

99.How a file is deleted in Python?


Answer:
The file can be deleted by either of these commands:
os.remove(filename)
os.unlink(filename)

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
Page17

100.What is difference between python and other programming languages?

Answer:

. C C++ PYTHON

Longer lines of code as 3-5 times shorter than


Longer lines of code as
compared to python. C/C++ programs.
compared to python.

Longer lines of code as Python has no


Longer lines of code as
compared to python. declaration.
compared to python.

C is a compiled C++ is a compiled Python is an


language. language. interpreted language.

Python supports
C++ supports both procedural, object-
C supports procedural procedural and object oriented, and
programming. oriented programming. functional
programming.

C++ support both single


Python supports5 types
C supports procedural and multiple
of inheritance
programming. inheritance

stechgroup.in
sy
roup contact@
systechg 7502202555
www.systechgroup.in
group
systech

Trichy Coimbatore
Aruvi Block, 1st Floor, St. Paul’s complex, Ajay building,
Bharathiyar Salai, Cantonment, 9th street, Cross Cut Road, Gandhipuram,
Trichy – 620001 Coimbatore – 641012

+91 431 241 0960 +91 422 2493658

75022 02555​ 9080432873

[email protected] [email protected]

https://systechgroup.in/ https://systechgroup.in/

Chennai Coming Soon...!

You might also like