Python Interview Questions and Answers

Q.1. What is python and name some key features of it?
A.Python is an interpreter-based programming language, interactive and object-oriented scripting language. Python is designed to be highly readable.

  • It is an interpreter based language which means that, unlike other languages like C and variants, the compilation doesn’t require before running.
  • It’s dynamically typed, which means you need not to define the datatypes of the declared variables and anything like that.

Eg: You can declare variable x=10 and then x=”Hello World” without error it will define the datatype by default depending on its value.

  • Functions are first-class objects in python.
  • Python can be used for different cross-platform applications like web-apps, scientific models, big data applications and many more.
Q.2. Differentiate between tuples and lists in python?

Tuples List
A tuple is a sequence of immutable objects List are versatile datatype which are Mutable
The syntax for Tuples are shown by parenthesis {} Syntax for List is shown by square brackets []
They are of fixed length List can be of variable length
Eg: tup_1 = { 10,’john’,5} Eg :list_1 = [10, ‘john’, 5]
Q.3.What will the maximum length of an identifier?
A.There is no certain length for Identifier, Identifier can be of any length.Let move on to the next Python Interview Questions.
Q.4. What are types of Operator are used in Python?
A.The types of operators which are used by Python are listed below:

  • Arithmetic Operators
Operator Name Description
Subtract It Subtracts the right-hand value from left side value.
* Multiply It multiplies.
/ Divide It divides left-hand value by right side value.
% Module It divides left-hand side operand by right-hand side operand and gives back the remainder.
** Exponent Performs exponential calculation on values.


  • Assignment Operators
Operator Name
Q.5. What do you mean by Decorators?
A.For modifying or injecting code in functions or classes we use Decorators in Python. By the help of decorators, we can check for permissions and for logging the calls.
Q.6. What do you mean by the dictionary in Python?
A.The built-in data types of Python known as Dictionary. For e.g. {“Name” : “Tom”,”Age” : 30}
Q.7. Explain the Memory Management in python?
A.Python memory is management is done by python private heap space. All the objects and data structures of pythons are located in private heap.
Q.8. Explain Python is one Line?
A.Python is a modern powerful interpreted language with threads, objects, modules, exceptions and also have the property of automatic memory management.
Q.9. Explain the interpretation in Python?
A.Programs in python run directly from the source code.
Q.10. Explain the rules for local and global variables in Python?
A. Global variable: If the variable is defined outside function then it is Global.
Local Variable: If a variable is assigned new value inside the function then it is local
Q.11. How to share global variable in Python?
A. By creating a config file and store the global variable to be shared across modules.
Q.12. How to pass optional or keyword parameters from one function to another in Python?
A. We can arrange arguments using the * and ** specifiers in the function’s parameter list.
Q.13. What are the different types of sequences in Python?
A. Different types of sequences in Python are Strings, Unicode strings, lists, tuples, buffers and range objects.
Q.14. What is Lambda form in Python?
A. Lambda keyword is used to create small random anonymous throw away functions.
for example :-
sum = lambda x, y : x + y
x = sum(3,4)
Q.15. What is Pickling in Python?
A. Pickle is a standard module which serializes and de-serializes a python object structure.
Q.16. How can an object be copied in Python?
A. By using two ways objects can be copied in python: Shallow copy & Deep copy.Let us move to the next Python Interview Questions.
Q.17. How do I convert a string to a number?
A. There are different built-in functions by which we can convert values from one data type to another in python we can use int() , float()
for example :-
x = “2”
Q.18. What is the command used for exiting help command prompt?
A. The command name is “quit”
Q.19.What does the split(), sub() and subn() methods do?
A.Split()  its uses a regex pattern to split any given string into a created list.
Sub()  It will find all the substring where this regex pattern will match and then replace the string. Subn()  It is similar to a sub(), it will return the new string along with the other no. of replacements.
Q.20. Mention the way to display the text contents of the files in the reverse order?
A. First, convert the file into the list and after that reverse this list by utilizing reversed ().
Q.21. What are ODBS modules for Python?
A. 1. PythonWin ODBC module 2. MxODBC 3. Pyodbc
Q.22. What will append() and extend methods do?
A. append()  adds the element at the end.
extend()  adds the elements of a different list at the end.
Q.23. What is TKIner?
A. The Tklner is the library of Python. It is one of the toolkits for developing the GUI.
Q.24.What Native Data Structures Can You Name in Python?
A. Common native data structures in Python are as follows:

  • Dictionaries
  • Lists
  • Sets
  • Strings
  • Tuples

Of These, Which Are Mutable, and Which Are Immutable?

Lists, dictionaries, and sets are mutable. This means that you can change their content without changing their identity. Strings and tuples are immutable, as their contents can’t be altered once they’re created.

Q.25.What’s the Difference Between a List and a Dictionary?
A. A list and a dictionary in Python are essentially different types of data structures. Lists are the most common data types that boast significant flexibility. Lists can be used to store a sequence of objects that are mutable (so they can be modified after they are created).However, they have to be stored in a particular order that can be indexed into the list or iterated over it (and it can also take some time to apply iterations on list objects).For example:>>> a = [1,2,3]>>> a[2]=4>>> a[1, 2, 4]In Python, you can’t use a list as a “key” for the dictionary (technically you can hash the list first via your own custom hash functions and use that as a key). A Python dictionary is fundamentally an unordered collection of key-value pairs. It’s a perfect tool to work with an enormous amount of data since dictionaries are optimized for retrieving data (but you have to know the key to retrieve its value).
Q.26.In a List and in a Dictionary, What Are the Typical Characteristics of Elements?
A. Elements in lists maintain their ordering unless they are explicitly commanded to be re-ordered. They can be of any data type, they can all be the same, or they can be mixed. Elements in lists are always accessed through numeric, zero-based indices.In a dictionary, each entry will have a key and a value, but the order will not be guaranteed. Elements in the dictionary can be accessed by using their key.Lists can be used whenever you have a collection of items in an order. A dictionary can be used whenever you have a set of unique keys that map to values.
Q.27.Is There a Way to Get a List of All the Keys in a Dictionary? If So, How Would You Do It?
A. If the interviewer follows up with this question, try to make your answer as specific as possible. To obtain a list of all the keys in a dictionary, we have to use function keys(): mydict={‘a’:1,’b’:2,’c’:3,’e’:5} mydict.keys()dict_keys([‘a’, ‘b’, ‘c’, ‘e’])
Q.28. What’s the Difference between a For Loop and a While Loop?
A. For LoopIn Python (and in almost any other programming language), For Loop is the most common type of loop. For Loop is often leveraged to iterate through the elements of an array.For Loop can also be used to perform a fixed number of iterations and iterate by a given (positive or even negative) increment. It’s important to note that by default, the increment will always be one.While LoopWhile Loop can be used in Python to perform an indefinite number of iterations as long as the condition remains true.When using the While Loop, you have to explicitly specify a counter to keep track of how many times the loop was executed. However, While Loop can’t define its own variable. Instead, it has to be previously defined and will continue to exist even after you exit the loop.When compared to For Loop, While Loop is inefficient because it’s much slower. This can be attributed to the fact that it checks the condition after each iteration. However, if you need to perform one or more conditional checks in a For Loop, you will want to consider using While Loop instead (as these checks won’t be required).
#Example file for working with loops
#define a while loop
#	while(x <4):
#		print x
#		x = x+1

#Define a for loop 
for x in range(2,7):
Q.29.Do You Know Any Additional Data Structures Available in the Standard Library?
A. Python’s standard library has a wealth of data structures, including:

  • Bisect
  • Boolean
  • Deque
  • Float
  • Heapq
  • Integers
Q.30.In Python, How is Memory Managed?
A. In Python, memory is managed in a private heap space. This means that all the objects and data structures will be located in a private heap. However, the programmer won’t be allowed to access this heap. Instead, the Python interpreter will handle it. At the same time, the core API will enable access to some Python tools for the programmer to start coding.The memory manager will allocate the heap space for the Python objects while the inbuilt garbage collector will recycle all the memory that’s not being used to boost available heap space.
Q.31.Can You Explain What a List or Dict Comprehension Is?
A. When you need to create a new list from other iterables, you have to use list comprehensions. As lists comprehensions return list results, they will be made up of brackets that contain the expressions that need to be executed for each element. Along with the loop, these can be iterated over each element.
Q.32. What are the advantages of Python?

  • Interpreted
  • Free and open source
  • Extensible
  • Object-oriented
  • Built-in data structure
  • Readability
  • High-Level Language
  • Cross-platform
    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.
  • Portable: Python programs can run on cross platforms without affecting its performance.
  • 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.
Q.33. How to create a Unicode string in Python?
A. 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.
Q.34. What are iterators in Python?
A. 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).