50,000+ Students Trained, 19+ Our Offices, 18+ Courses Offered,

Python Interview Question

Most Important Question:-

1. What is Python?

Ans. Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.

2. Name some of the features of Python.

Ans.

• It supports functional and structured programming methods as well as OOP.

• It can be used as a scripting language or can be compiled to byte-code for building large applications.

• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

3. What is the purpose of PYTHONPATH environment variable?

Ans. PYTHONPATH – It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.

4. What is the purpose of PYTHONSTARTUP environment variable?

Ans. PYTHONSTARTUP – It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.

5. What is the purpose of PYTHONCASEOK environment variable?

Ans. PYTHONCASEOK − It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.

6. What is the purpose of PYTHONHOME environment variable?

Ans. PYTHONHOME − It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.

7. Is python a case sensitive language?

Ans. Yes! Python is a case sensitive programming language.

8. What are the supported data types in Python?

Ans. Python has five standard data types −

• Numbers

• String

• List

• Tuple

• Dictionary

9. What are Python’s dictionaries?

Ans. Python’s dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

10. How will you get all the keys from the dictionary?

Ans. Using dictionary.keys() function, we can get all the keys from the dictionary object.

11. How will you get all the values from the dictionary?

Ans. Using dictionary.values() function, we can get all the values from the dictionary object.

12. Explain Inheritance in Python with an example.

Ans. Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class.
They are different types of inheritance supported by Python:

1. Single Inheritance – where a derived class acquires the members of a single super class.

2. Multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 is inherited from base2.

3. Hierarchical inheritance – from one base class you can inherit any number of child classes

4. Multiple inheritance – a derived class is inherited from more than one base class.

• split() – uses a regex pattern to “split” a given string into a list.

• sub() – finds all substrings where the regex pattern matches and then replace them with a different string

• subn() – it is similar to sub() and also returns the new string along with the no. of replacements.

14. What is pickling and unpickling?

Ans. Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.

15. What is map function in Python?

Ans. map function executes the function given as the first argument on all the elements of the iterable given as the second argument. If the function given takes in more than 1 arguments, then many iterables are given. #Follow the link to know more similar functions

16. What is TkInter?

Ans. TkInter is Python library. It is a toolkit for GUI development. It provides support for various GUI tools or widgets (such as buttons, labels, text boxes, radio buttons, etc) that are used in GUI applications. The common attributes of them include Dimensions, Colors, Fonts, Cursors, etc.

17. Is Python object oriented? what is object oriented programming?

Ans. Yes. Python is Object Oriented Programming language. OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:
Encapsulation, Data Abstraction, Inheritance, Polymorphism.

18. What is multithreading? Give an example.

Ans.
It means running several different programs at the same time concurrently by invoking multiple threads. Multiple threads within a process refer the data space with main thread and they can communicate with each other to share information more easily.Threads are light-weight processes and have less memory overhead. Threads can be used just for quick task like calculating results and also running other processes in the background while the main program is running.

19. Does Python supports interfaces like in Java? Discuss.

Ans. Python does not provide interfaces like in Java. Abstract Base Class (ABC) and its feature are provided by the Python’s “abc” module. Abstract Base Class is a mechanism for specifying what methods must be implemented by its implementation subclasses. The use of ABC’c provides a sort of “understanding” about methods and their expected behaviour. This module was made available from Python 2.7 version onwards.

20. Differentiate between append() and extend() methods.?

Ans. Both append() and extend() methods are the methods of list. These methods a re used to add the elements at the end of the list.

append(element) – adds the given element at the end of the list which has called this method.

extend(another-list) – adds the elements of another-list at the end of the list which is called the extend method.

21. Name few methods that are used to implement Functionally Oriented Programming in Python?

Ans. Python supports methods (called iterators in Python3), such as filter(), map(), and reduce(), that are very useful when you need to iterate over the items in a list, create a dictionary, or extract a subset of a list.

filter() – enables you to extract a subset of values based on conditional logic.

map() – it is a built-in function that applies the function to each item in an iterable.

reduce() – repeatedly performs a pair-wise reduction on a sequence until a single value is computed.

22. What is the output of the following?

Ans.

x = [‘ab’, ‘cd’]

print(len(map(list, x)))

A TypeError occurs as map has no len().

23. What is the output of the following?

Ans.

x = [‘ab’, ‘cd’]

print(len(list(map(list, x))))

Explanation: The length of each string is 2.

24. Which of the following is not the correct syntax for creating a set?

Ans.

a) set([[1,2],[3,4]])

b) set([1,2,2,3,4])

c) set((1,2,3,4))

d) {1,2,3,4}

Answer : a
Explanation : The argument given for the set must be an iterable.

25. How to retrieve data from a table in MySQL database through Python code?