Python Interview Questions and Answers

What is Python? What are the benefits of using Python?

Python is an open-source interpreted, high-level, object-oriented scripting language. It was created by Dutch programmer Guido van Rossum, and released in 1991.

Application development is faster and easy
* It is one of the fastest-growing programming languages in the world
* Its easy to keep the code base readable and application maintainable due to its simple easy-to-use syntax rules
* It doesn’t require explicit memory management as the interpreter itself allocates the memory to new variables and free them automatically.
* It can be used for server-side web development, software development, mathematics, artificial intelligence, data analysis, data science, system scripting, scientific computing etc.,
* It comprises of a huge standard library which includes areas like internet protocols, string operations, web services tools and operating system interfaces. Many high use programming tasks have already been scripted into the standard library which reduces length of code to be written significantly.
* It has a massive support community to get your answers. Thanks to the fact that it is open source and community developed.

Features of Python?

1. Easy to learn and use
2. Expressive language
3. Interpreted language
4. Cross-platform language
5. High level language
6. Portable
7. Free and open source language
8. Object oriented language
9. Extensible
10. Large standard library
11. GUI Programming support
12. Integrated
13. Embeddable
14. Dynamically Typed Language

What is PEP 8?

PEP stands for Python Enhancement Proposal. PEP 8 is Python’s style guide. It’s a set of rules for how to write your Python code more readable.

How is Python script executed ?

When a Python script is executed, it doesn’t convert its code into machine code. It actually converts it into something called byte code. Execution of Python script means execution of the byte code on the Python Virtual Machine (PVM).
Suppose your script is in “D:\myPythonCode\script.py”

— Open Command line: Start menu -> Run and type cmd
— Type: C:\python27\python.exe D:\myPythonCode\script.py

What is the difference between .py and .pyc files ?

.py files are the original text files holding Python code.
.pyc files are the compiled version of the .py files. These are byte code files that is generated by the Python compiler.
Python compiles the .py files and saves it as .pyc files

Is python case sensitive?

Yes. Python is a case sensitive language.

What is pickling and unpickling in Python?

Pickling:
Pickling is a process in which pickle module accepts any Python object and converts it into a string representation and dumps the same into a file by calling the dump method.

Unpickling:
Unpickling is a process in which retrieving original Python objects from a stored string representation. Simply, the reverse process of pickling is known as unpickling.

What is monkey patching in Python?

Monkey Patching in Python refers to the dynamic modifications made to a class or module at runtime

What are python modules? Name some commonly used built-in modules in Python?

Python modules are files consisting of Python code. A Python module can define functions, classes and variables. A Python module is a .py file containing runnable code.

What are local and global variables in Python?

Local variables:
Variables declared inside a function is known as a local variable. These variables are present in the local space and not in the global space. locals() is accessed within the function and it returns all names that can be accessed locally from that function.

Global variables:
Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program. globals() returns all names that can be accessed globally from that function.

What is the difference between a list and a tuple in Python?

Here is the List vs Tuple
List:
Lists are mutable. A mutable data type means that a python object of this type can be modified
Lists is a sequence of elements which are separated by commas and are surrounded by a square brackets or simply brackets “[]”.
List has variable length
Lists operation has more size than that of tuple, which makes it a bit slower

Tupple:
Tuples are immutable. An immutable objects can’t be modified. We can’t modify a tuple object after it’s created
Tuples is a sequence of elements which are separated by commas and are surrounded by a round brackets or parentheses “()”
Tuple has fixed length
Tuples operation has smaller size than that of list, which makes it a bit faster

What does len() do?

It is used to determine the length of a string, a list, an array, etc.
a=’STM’
len(a)

What are *args and **kwargs in Python?

In Python, *args and **kwards are used to pass a variable number of arguments to a function using special symbols.
We use *args and **kwargs as an argument when we aren’t sure how many arguments to pass in the functions.

How is Python an interpreted language?

Python is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed. It converts the source code into an intermediate language, which is again translated into native language / machine language that has to be executed.

What are the built-in types in Python?

Common native data types in Python are as follows.

1. Mutable built-in types: We can change the content without changing the identity
Dictionaries
List
Sets

2. Immutable built-in types: We can’t change the content once it is created
Strings
Tuples
Numbers

How memory is managed in Python?

Memory in Python is managed by Python private heap space. All Python objects and data structres are located in a private heap. The programmer doesn’t have an access to the private heap and this private heap in Python is taken care by Python Interpreter.
Python memory manager is responsible in allocating Python heap space for Python objects.
Python has an inbuilt garbage collector, which recycles all the unused memory and frees the memory and makes it available to the heap space.

What are the tools that help to find bugs or perform static analysis?

PyChecker: PyChecker is a static analysis tool that finds bugs in the Python source code and raises alerts about the issues in the style or code complexity.
Pylint: Pylint checks if a module meets the coding standards.

What are Python decorators?

Python decorators allow programmers to add functionality to an existing code. It is to wrap a function in order to extend the behavior of wrapped function, without permanently modifying it.

What are the different environment variables identified by Python?

Environment variables, which can be recognized by Python are
PYTHONPATH
PYTHONSTARTUP
PYTHONCASEOK
PYTHONHOME

What's the Difference between a List and a Dictionary?

List vs Dictionary:

List:
A Python List can be used to store a sequence of elements that are mutable. So the elements can be modified after they are created. 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.
Lists can be used whenever you have a collection of items in an order

Dictionary:
A Python dictionary is an unordered collection of key-value pairs. Dictionaries are indexed by keys and are optimized to retrieve values when the key is known. The keys are unique whereas values can be duplicate.
Elements in the dictionary can be accessed by using their key.
A dictionary can be used whenever you have a set of unique keys that map to values.

What is slicing in Python?

Slicing is a string operation used to select a range of items from sequence type like list, tuple, and string.
The slice object represents the indices specified by range(start, stop, step). The slice() method allows three parameters ie., start, stop, and step.

start – starting number for the slicing to begin.
stop – the number which indicates the end of slicing.
step – the value to increment after each index (default = 1).

Although we can get elements by specifying an index. In Python, a string (say text) begins at index ‘0’, and the nth character stores at ‘n-1’. We can also do reverse indexing using negative numbers. By doing this we can get only single element whereas using slicing we can get a group of elements. It is beneficial and easy to get elements from a range by using slice.

What statement is used in Python if the statement is required syntactically but there is no execution of code or command?

We use Pass statement.

Are arguments pass by value or pass by reference in Python?

Python neither passes by value nor by reference. Python passes by ‘Assignment‘. Everything in python is an object. The parameter we pass is originally a reference to the object not the reference to a fixed memory location. We cannot change the value of the references but we can change the objects if it is mutable.

When Would You Use a List vs. a Tuple vs. a Set in Python?

A Python List can be used to store a sequence of elements that are mutable. So the elements can be modified after they are created. They can be of any data type. They can all be the same or they can be mixed. It is ideal for projects that demand the storage of objects that can be changed later.

Tuples are immutable. An immutable objects can’t be modified. We can’t modify a tuple object after it’s created. Tuples operation has smaller size than that of list, which makes it a bit faster. It is ideal for projects that demand a list of constants.

A Python Set can be used to store a collection of unique elements. It is ideal for projects that demand no duplicate elements in a list. If we have two lists with common elements between them then we can leverage sets to eliminate them.

How can you copy an object in Python?

There are two ways to create copies in Python

1. Shallow Copy (copy.copy())
2. Deep Copy (copy.deepcopy())