Python Interview Questions
A list of frequently asked Python interview questions with answers for freshers and experienced are given below.
1) What is Python?
Python was created by Guido van Rossum, and released in 1991.
It is a general-purpose computer programming language. It is a high-level, object-oriented language which can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh. Its high-level built-in data structures, combined with dynamic typing and dynamic binding. It is widely used in data science, machine learning and artificial intelligence domain.
It is easy to learn and require less code to develop the applications.
It is widely used for:
2) Why Python?
3) What are the applications of Python?
Python is used in various software domains some application areas are given below.
Python provides various web frameworks to develop web applications. The popular python web frameworks are Django, Pyramid, Flask.
Python's standard library supports for E-mail processing, FTP, IMAP, and other Internet protocols.
Python's SciPy and NumPy helps in scientific and computational application development.
Python's Tkinter library supports to create a desktop based GUI applications.
4) What are the advantages of Python?
Advantages of Python are:
Interpreted: Python is an interpreted language. It does not require prior compilation of code and executes instructions directly.
Free and open source: It is an open-source project which is publicly available to reuse. It can be downloaded free of cost.
Extensible: It is very flexible and extensible with any module.
Object-oriented: Python allows to implement the Object-Oriented concepts to build application solution.
Built-in data structure: Tuple, List, and Dictionary are useful integrated data structures provided by the language.
Portable: Python programs can run on cross platforms without affecting its performance.
5) What is PEP 8?
PEP 8 stands for Python Enhancement Proposal, it can be defined as a document that helps us to provide the guidelines on how to write the Python code. It is basically a set of rules that specify how to format Python code for maximum readability. It was written by Guido van Rossum, Barry Warsaw and Nick Coghlan in 2001.
6) What do you mean by Python literals?
Literals can be defined as a data which is given in a variable or constant. Python supports the following literals:
String literals are formed by enclosing text in the single or double quotes. For example, string literals are string values.
JavaTpoint JavaTpoint Java T point
Python supports three types of numeric literals integer, float and complex.
10 12.3 3.14j
Boolean literals are used to denote Boolean values. It contains either True or False.
p is True q is False r: 4 s: 7
Python contains one special literal, that is, 'None'. This special literal is used for defining a null variable. If 'None' is compared with anything else other than a 'None', it will return false.
7) Explain Python Functions?
A function is a section of the program or a block of code that is written once and can be executed whenever required in the program. A function is a block of self-contained statements which has a valid name, parameters list, and body. Functions make programming more functional and modular to perform modular tasks. Python provides several built-in functions to complete tasks and also allows a user to create new functions as well.
There are three types of functions:
Example: A general syntax of user defined function is given below.
8) What is zip() function in Python?
Python zip() function returns a zip object, which maps a similar index of multiple containers. It takes an iterable, convert into iterator and aggregates the elements based on iterables passed. It returns an iterator of tuples.
iterator1, iterator2, iterator3: These are iterator objects that are joined together.
It returns an iterator from two or more iterators.
Note: If the given lists are of different lengths, zip stops generating tuples when the first list ends. It means two lists are having 3, and 5 lengths will create a 3-tuple.
9) What is Python's parameter passing mechanism?
There are two parameters passing mechanism in Python:
By default, all the parameters (arguments) are passed "by reference" to the functions. Thus, if you change the value of the parameter within a function, the change is reflected in the calling function as well. It indicates the original variable. For example, if a variable is declared as a = 10, and passed to a function where it's value is modified to a = 20. Both the variables denote to the same value.
The pass by value is that whenever we pass the arguments to the function only values pass to the function, no reference passes to the function. It makes it immutable that means not changeable. Both variables hold the different values, and original value persists even after modifying in the function.
Python has a default argument concept which helps to call a method using an arbitrary number of arguments.
10) How to overload constructors or methods in Python?
Python's constructor: _init__ () is the first method of a class. Whenever we try to instantiate an object __init__() is automatically invoked by python to initialize members of an object. We can't overload constructors or methods in Python. It shows an error if we try to overload.
Name: rahul Email id: [email protected]
11) What is the difference between remove() function and del statement?
The user can use the remove() function to delete a specific object in the list.
[3, 5, 7, 3, 9, 3] After removal: [5, 7, 3, 9, 3]
If you want to delete an object at a specific location (index) in the list, you can either use del or pop.
[3, 5, 7, 3, 9, 3] After deleting: [3, 5, 3, 9, 3]
Note: You don't need to import any extra module to use these functions for removing an element from the list.
We cannot use these methods with a tuple because the tuple is different from the list.
12) What is swapcase() function in the Python?
It is a string's function which converts all uppercase characters into lowercase and vice versa. It is used to alter the existing case of the string. This method creates a copy of the string which contains all the characters in the swap case. If the string is in lowercase, it generates a small case string and vice versa. It automatically ignores all the non-alphabetic characters. See an example below.
it is in lowercase. IT IS IN UPPERCASE.
13) How to remove whitespaces from a string in Python?
To remove the whitespaces and trailing spaces from the string, Python providies strip([str]) built-in function. This function returns a copy of the string after removing whitespaces if present. Otherwise returns original string.
javatpoint javatpoint javatpoint After stripping all have placed in a sequence: Javatpoint javatpoint javatpoint
14) How to remove leading whitespaces from a string in the Python?
To remove leading characters from a string, we can use lstrip() function. It is Python string function which takes an optional char type parameter. If a parameter is provided, it removes the character. Otherwise, it removes all the leading spaces from the string.
javatpoint javatpoint After stripping all leading whitespaces: javatpoint javatpoint
After stripping, all the whitespaces are removed, and now the string looks like the below:
15) Why do we use join() function in Python?
The join() is defined as a string method which returns a string value. It is concatenated with the elements of an iterable. It provides a flexible way to concatenate the strings. See an example below.
16) Give an example of shuffle() method?
This method shuffles the given string or an array. It randomizes the items in the array. This method is present in the random module. So, we need to import it and then we can call the function. It shuffles elements each time when the function calls and produces different output.
Original LIST1: ['Z', 'Y', 'X', 'W', 'V', 'U'] After the first shuffle of LIST1: ['V', 'U', 'W', 'X', 'Y', 'Z'] After the second shuffle of LIST1: ['Z', 'Y', 'X', 'U', 'V', 'W']
17) What is the use of break statement?
The break statement is used to terminate the execution of the current loop. Break always breaks the current execution and transfer control to outside the current block. If the block is in a loop, it exits from the loop, and if the break is in a nested loop, it exits from the innermost loop.
2 X 11 X 22 X 33 Y 11 Y 22 Y 33 BREAK
Python Break statement flowchart.
18) What is tuple in Python?
A tuple is a built-in data collection type. It allows us to store values in a sequence. It is immutable, so no change is reflected in the original data. It uses () brackets rather than  square brackets to create a tuple. We cannot remove any element but can find in the tuple. We can use indexing to get elements. It also allows traversing elements in reverse order by using negative indexing. Tuple supports various methods like max(), sum(), sorted(), Len() etc.
To create a tuple, we can declare it as below.
(2, 4, 6, 8) 6
It is immutable. So updating tuple will lead to an error.
tup=22 TypeError: 'tuple' object does not support item assignment (2, 4, 6, 8)
19) Which are the file related libraries/modules in Python?
The Python provides libraries/modules that enable you to manipulate text files and binary files on the file system. It helps to create files, update their contents, copy, and delete files. The libraries are os, os.path, and shutil.
Here, os and os.path - modules include a function for accessing the filesystem
while shutil - module enables you to copy and delete the files.
20) What are the different file processing modes supported by Python?
Python provides four modes to open files. The read-only (r), write-only (w), read-write (rw) and append mode (a). 'r' is used to open a file in read-only mode, 'w' is used to open a file in write-only mode, 'rw' is used to open in reading and write mode, 'a' is used to open a file in append mode. If the mode is not specified, by default file opens in read-only mode.
21) What is an operator in Python?
An operator is a particular symbol which is used on some values and produces an output as a result. An operator works on operands. Operands are numeric literals or variables which hold some values. Operators can be unary, binary or ternary. An operator which requires a single operand known as a unary operator, which require two operands known as a binary operator and which require three operands is called ternary operator.
# Unary Operator -12 # Binary Operator 25 156 # Ternary Operator 12
22) What are the different types of operators in Python?
Python uses a rich set of operators to perform a variety of operations. Some individual operators like membership and identity operators are not so familiar but allow to perform operations.
Arithmetic operators perform basic arithmetic operations. For example "+" is used to add and "?" is used for subtraction.
35 -11 276 0.5217391304347826
Relational Operators are used to comparing the values. These operators test the conditions and then returns a boolean value either True or False.
# Examples of Relational Operators
False True True True
Assignment operators are used to assigning values to the variables. See the examples below.
12 14 12 24 576
Logical operators are used to performing logical operations like And, Or, and Not. See the example below.
False True True
Membership operators are used to checking whether an element is a member of the sequence (list, dictionary, tuples) or not. Python uses two membership operators in and not in operators to check element presence. See an example.
Identity Operators (is and is not) both are used to check two values or variable which are located on the same part of the memory. Two variables that are equal does not imply that they are identical. See the following examples.
Bitwise Operators are used to performing operations over the bits. The binary operators (&, |, OR) work on bits. See the example below.
8 14 6 -11
23) How to create a Unicode string in Python?
In Python 3, the old Unicode type has replaced by "str" type, and the string is treated as Unicode by default. We can make a string in Unicode by using art.title.encode("utf-8") function.
24) is Python interpreted language?
Python is an interpreted language. The Python language program runs directly from the source code. It converts the source code into an intermediate language code, which is again translated into machine language that has to be executed.
Unlike Java or C, Python does not require compilation before execution.
25) How is memory managed in Python?
Memory is managed in Python in the following ways:
26) What is the Python decorator?
Decorators are very powerful and a useful tool in Python that allows the programmers to add functionality to an existing code. This is also called metaprogramming because a part of the program tries to modify another part of the program at compile time. It allows the user to wrap another function to extend the behaviour of the wrapped function, without permanently modifying it.
Functions vs. Decorators
A function is a block of code that performs a specific task whereas a decorator is a function that modifies other functions.
27) What are the rules for a local and global variable in Python?
28) What is the namespace in Python?
The namespace is a fundamental idea to structure and organize the code that is more useful in large projects. However, it could be a bit difficult concept to grasp if you're new to programming. Hence, we tried to make namespaces just a little easier to understand.
A namespace is defined as a simple system to control the names in a program. It ensures that names are unique and won't lead to any conflict.
Also, Python implements namespaces in the form of dictionaries and maintains name-to-object mapping where names act as keys and the objects as values.
29) What are iterators in Python?
In Python, iterators are used to iterate a group of elements, containers like a list. Iterators are the collection of items, and it can be a list, tuple, or a dictionary. Python iterator implements __itr__ and next() method to iterate the stored elements. In Python, we generally use loops to iterate over the collections (list, tuple).
In simple words: Iterators are objects which can be traversed though or iterated upon.
30) What is a generator in Python?
In Python, the generator is a way that specifies how to implement iterators. It is a normal function except that it yields expression in the function. It does not implements __itr__ and next() method and reduce other overheads as well.
If a function contains at least a yield statement, it becomes a generator. The yield keyword pauses the current execution by saving its states and then resume from the same when required.
31) What is slicing in Python?
Slicing is a mechanism used to select a range of items from sequence type like list, tuple, and string. It is beneficial and easy to get elements from a range by using slice way. It requires a : (colon) which separates the start and end index of the field. All the data collection types List or tuple allows us to use slicing to fetch elements. Although we can get elements by specifying an index, we get only single element whereas using slicing we can get a group of elements.
vaTpoint, Python Interv
32) What is a dictionary in Python?
The Python dictionary is a built-in data type. It defines a one-to-one relationship between keys and values. Dictionaries contain a pair of keys and their corresponding values. It stores elements in key and value pairs. The keys are unique whereas values can be duplicate. The key accesses the dictionary elements.
Keys index dictionaries.
The following example contains some keys Country Hero & Cartoon. Their corresponding values are India, Modi, and Rahul respectively.
Country: India Hero: Modi Cartoon: Rahul
33) What is Pass in Python?
Pass specifies a Python statement without operations. It is a placeholder in a compound statement. If we want to create an empty class or functions, the pass keyword helps to pass the control without error.
34) Explain docstring in Python?
The Python docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. It provides a convenient way to associate the documentation.
String literals occurring immediately after a simple assignment at the top are called "attribute docstrings".
String literals occurring immediately after another docstring are called "additional docstrings".
Python uses triple quotes to create docstrings even though the string fits on one line.
Docstring phrase ends with a period (.) and can be multiple lines. It may consist of spaces and other special chars.
35) What is a negative index in Python and why are they used?
The sequences in Python are indexed and it consists of the positive as well as negative numbers. The numbers that are positive uses '0' that is uses as first index and '1' as the second index and the process go on like that.
The index for the negative number starts from '-1' that represents the last index in the sequence and '-2' as the penultimate index and the sequence carries forward like the positive number.
The negative index is used to remove any new-line spaces from the string and allow the string to except the last character that is given as S[:-1]. The negative index is also used to show the index to represent the string in correct order.
36) What is pickling and unpickling in Python?
The Python pickle is defined as a module which accepts any Python object and converts it into a string representation. It dumps the Python object into a file using the dump function; this process is called Pickling.
The process of retrieving the original Python objects from the stored string representation is called as Unpickling.
37) Which programming language is a good choice between Java and Python?
Java and Python both are object-oriented programming languages. Let's compare both on some criteria given below:
38) What is the usage of help() and dir() function in Python?
Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.
Help() function: The help() function is used to display the documentation string and also facilitates us to see the help related to modules, keywords, and attributes.
Dir() function: The dir() function is used to display the defined symbols.
39) What are the differences between Python 2.x and Python 3.x?
Python 2.x is an older version of Python. Python 3.x is newer and latest version. Python 2.x is legacy now. Python 3.x is the present and future of this language.
The most visible difference between Python2 and Python3 is in print statement (function). In Python 2, it looks like print "Hello", and in Python 3, it is print ("Hello").
String in Python2 is ASCII implicitly, and in Python3 it is Unicode.
The xrange() method has removed from Python 3 version. A new keyword as is introduced in Error handling.
40) How Python does Compile-time and Run-time code checking?
In Python, some amount of coding is done at compile time, but most of the checking such as type, name, etc. are postponed until code execution. Consequently, if the Python code references a user-defined function that does not exist, the code will compile successfully. The Python code will fail only with an exception when the code execution path does not exist.
41) What is the shortest method to open a text file and display its content?
The shortest way to open a text file is by using "with" command in the following manner:
"The data of the file will be printed."
42) What is the usage of enumerate () function in Python?
The enumerate() function is used to iterate through the sequence and retrieve the index position and its corresponding value at the same time.
43) Give the output of this example: A if A=[1,4,6,7,9,66,4,94].
Since indexing starts from zero, an element present at 3rd index is 7. So, the output is 7.
44) What is type conversion in Python?
Type conversion refers to the conversion of one data type iinto another.
int() - converts any data type into integer type
float() - converts any data type into float type
ord() - converts characters into integer
hex() - converts integers to hexadecimal
oct() - converts integer to octal
tuple() - This function is used to convert to a tuple.
set() - This function returns the type after converting to set.
list() - This function is used to convert any data type to a list type.
dict() - This function is used to convert a tuple of order (key,value) into a dictionary.
str() - Used to convert integer into a string.
complex(real,imag) - This functionconverts real numbers to complex(real,imag) number.
45) How to send an email in Python Language?
To send an email, Python provides smtplib and email modules. Import these modules into the created mail script and send mail by authenticating a user.
It has a method SMTP(smtp-server, port). It requires two parameters to establish SMTP connection.
A simple example to send an email is given below.
46) What is the difference between Python Arrays and lists?
Arrays and lists, in Python, have the same way of storing data. But, arrays can hold only a single data type elements whereas lists can hold any data type elements.
array('i', [1, 2, 3, 4]) [1, 'abc', 1.2]
47) What is lambda function in Python?
The anonymous function in python is a function that is defined without a name. The normal functions are defined using a keyword "def", whereas, the anonymous functions are defined using the lambda function. The anonymous functions are also called as lambda functions.
48) Why do lambda forms in Python not have the statements?
Lambda forms in Python does not have the statement because it is used to make the new function object and return them in runtime.
49) What are functions in Python?
A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used.
Hi, Welcome to JavaTpoint
50) What is __init__?
The __init__ is a method or constructor in Python. This method is automatically called to allocate memory when a new object/ instance of a class is created. All classes have the __init__ method.
pqr 20 25000
51) What is self in Python?
Self is an instance or an object of a class. In Python, this is explicitly included as the first parameter. However, this is not the case in Java where it's optional. It helps to differentiate between the methods and attributes of a class with local variables.
The self-variable in the init method refers to the newly created object while in other methods, it refers to the object whose method was called.
52) How can you generate random numbers in Python?
Random module is the standard module that is used to generate a random number. The method is defined as:
The statement random.random() method return the floating point number that is in the range of [0, 1). The function generates random float numbers. The methods that are used with the random class are the bound methods of the hidden instances. The instances of the Random can be done to show the multi-threading programs that creates a different instance of individual threads. The other random generators that are used in this are:
randrange(a, b): it chooses an integer and define the range in-between [a, b). It returns the elements by selecting it randomly from the range that is specified. It doesn't build a range object.
uniform(a, b): it chooses a floating point number that is defined in the range of [a,b).Iyt returns the floating point number
normalvariate(mean, sdev): it is used for the normal distribution where the mu is a mean and the sdev is a sigma that is used for standard deviation.
The Random class that is used and instantiated creates independent multiple random number generators.
53) What is PYTHONPATH?
PYTHONPATH is an environment variable which is used when a module is imported. Whenever a module is imported, PYTHONPATH is also looked up to check for the presence of the imported modules in various directories. The interpreter uses it to determine which module to load.
54) What are python modules? Name some commonly used built-in modules in Python?
Python modules are files containing Python code. This code can either be functions classes or variables. A Python module is a .py file containing executable code.
Some of the commonly used built-in modules are:
55) What is the difference between range & xrange?
For the most part, xrange and range are the exact same in terms of functionality. They both provide a way to generate a list of integers for you to use, however you please. The only difference is that range returns a Python list object and x range returns an xrange object.
This means that xrange doesn't actually generate a static list at run-time like range does. It creates the values as you need them with a special technique called yielding. This technique is used with a type of object known as generators. That means that if you have a really gigantic range you'd like to generate a list for, say one billion, xrange is the function to use.
This is especially true if you have a really memory sensitive system such as a cell phone that you are working with, as range will use as much memory as it can to create your array of integers, which can result in a Memory Error and crash your program. It's a memory hungry beast.
56) What advantages do NumPy arrays offer over (nested) Python lists?
57) Mention what the Django templates consist of.
The template is a simple text file. It can create any text-based format like XML, CSV, HTML, etc. A template contains variables that get replaced with values when the template is evaluated and tags (% tag %) that control the logic of the template.
58) Explain the use of session in Django framework?
Django provides a session that lets the user store and retrieve data on a per-site-visitor basis. Django abstracts the process of sending and receiving cookies, by placing a session ID cookie on the client side, and storing all the related data on the server side.
So, the data itself is not stored client side. This is good from a security perspective.
Interview based Multiple Choice Questions on Python
1) Which of the following statements is/are TRUE in respect of the Python programming language?
Statement 1: Python is an interpreted, high-level, general-purpose programming language.
Statement 2: Python provides high-level data structures along with dynamic binding and typing for Rapid Application Development and deployment.
Statement 3: Python is a Statically typed Programming language.
Answer: B: Statement 1 and 2
Explanation: Python is an interpreted, high-level, general-purpose programming language. Being an interpreted language, it executes every block of code line by line, and thus type-checking is done while executing the code. Hence, it is a dynamically typed language.
Moreover, Python offers high-level data structures, together with dynamic binding and typing, allows a large community of developers for Rapid Application Development and Deployment.
2) What is the full form of PEP?
Answer: A: Python Enhancement Proposal
Explanation: A PEP, also known as Python Enhancement Proposal, is an official design document offering information to the community of Python developers or depicting a new feature for Python or its methods.
3) Which of the following statements is/are NOT correct regarding Memory Management in Python?
Statement 1: Python Memory Manager handles the management regarding memory in Python
Statement 2: Python uses CMS (Concurrent Mark Sweep) approach as its Garbage Collection technique.
Statement 3: Python offers a core garbage collection to recycle the vacant memory for the private heap space.
Answer: D: Only Statement 2
Explanation: Python employs the Reference Counting algorithm as its Garbage Collection technique. This technique is highly efficient and straightforward; however, it can't detect the reference cycle. Hence, Python has an additional algorithm known as Generational Cyclic (GC), which only deals with the reference cycle.
4) Which of the following statements is/are NOT correct in respect to Python namespaces?
Statement 1: Python implements the namespace in the form of Array.
Statement 2: Python namespaces are classified into three types - local, global and built-in.
Statement 3: A Python namespace ensures that the names of the objects in a program are unique and can be utilized, deprived of any inconsistency.
Answer: A: Only Statement 1
Explanation: The namespaces in Python are implemented in the form of Dictionaries where 'key' denotes the 'name', mapped to a corresponding 'value' denoting the 'object'.
5) Which of the following is invalid in terms of Variable Names?
Answer: D: None of the mentioned
Explanation: All Statements will be executed successfully. However, the code readability will also be reduced.
6) In respect to the scope in Python, which of the following statements is/are TRUE?
Statement 1: A variable created within a function belongs to the local scope and can be used outside that function.
Statement 2: A local scope is referred to the object present through the execution of code since its inception.
Statement 3: A local scope is referred to the local object present in the current function.
Answer: C: Only Statement 3
Explanation: Local scope, also known as function scope, is the block of code or body of any function in Python. This scope consists of the names that we define within the function. These names will only be observable from the function code. It is created at the function call, not at the definition of the function, so that we will have as many distinct local scopes as the calls in function. This is true even when we call the same function more than one time or recursively. Every call will return a new local scope.
7) What will the following snippet of code print?
Answer: A: 10
Explanation: 10 is printed.
Whenever we reallocate a global variable in the local scope of a function, the relocation only holds inside the local scope. The variable goes back to the global value when the code returns the global scope.
8) What will the following snippet of code yield?
Answer: C: 21
Explanation: 21 is printed.
The variable, myint, is initially assigned to 21. The if False statement estimates to False, so the myint = 34 reassigned never occurs. The reassignment within the myfunction() function does occur inside the local scope, not the global one. When we try to access the variable myint in the global scope, the variable's value remains unchanged at 21.
9) Among the following statements based on the difference between lists and tuples, which one statement is TRUE?
Statement 1: List is a sequence data structure, whereas Tuple is not.
Statement 2: Lists are immutable; however, Tuples are mutable.
Statement 3: Tuple is a sequence data structure, whereas List is not.
Statement 4: Tuples are immutable; however, Lists are mutable.
Answer: B: Statement 4
Explanation: Apart from many similarities, one of the significant differences between the two is that the Lists are mutable, whereas Tuples are immutable. This statement implies that we can modify or change the values of a list; however, we can't alter the values of a Tuple.
10) What will be the output of the following snippet of code?
Answer: A: [7, 9, 2, 5, 0, 1, 3, 6]
Explanation: The pop() function is used to remove the element from the list. The function's parameter is the index value, n, of the data element to be removed from the list.
Thus, the pop(n) function removes the nth element from the list. As in the above case, the index number is 2. Hence, the pop(2) function removes the second element from my_list, i.e., 8.