- Information
- AI Chat
Top 50 Python Interview Questions And answers
Fundamentals of Computer and IT Laboratory (UGCA1906)
I. K. Gujral Punjab Technical University
Preview text
CONTENTS
How will you improve the performance of a program in Python?
What are the benefits of using Python?
How will you specify source code encoding in a Python source file?
What is the use of PEP 8 in Python?
What is Pickling in Python?
How does memory management work in Python?
How will you perform Static Analysis on a Python Script?
What is the difference between a Tuple and List in Python?
What is a Python Decorator?
How are arguments passed in a Python method? By value or by
reference?
- What is the difference between List and Dictionary data types in
Python?
What are the different built-in data types available in Python?
What is a Namespace in Python?
How will you concatenate multiple strings together in Python?
What is the use of Pass statement in Python?
What is the use of Slicing in Python?
What is the difference between Docstring in Python and Javadoc in
Java?
How do you perform unit testing for Python code?
What is the difference between an Iterator and Iterable in Python?
What is the use of Generator in Python?
What is the difference between ‘is’ and ‘==’ in Python?
How will you share variables across modules in Python?
How can we do Functional programming in Python?
What is the improvement in enumerate() function of Python?
How will you execute a Python script in Unix?
What are the popular Python libraries used in Data analysis?
What is the output of following code in Python?
What is the output of following code in Python?
If you have data with name of customers and their location, which
data type will you use to store it in Python?
ACKNOWLEDGMENTS
We thank our readers who constantly send feedback
and reviews to motivate us in creating these useful
books with the latest information!
INTRODUCTION
This book contains basic to expert level Python interview questions that an interviewer asks. Each question is accompanied with an answer so that you can prepare for job interview in short time.
We have compiled this list after attending dozens of technical interviews in top-notch companies like- Google, Facebook, Netflix, Amazon etc.
Often, these questions and concepts are used in our daily programming work. But these are most helpful when an Interviewer is trying to test your deep knowledge of Python.
The difficulty rating on these Questions varies from a Fresher level software programmer to a Senior software programmer.
Once you go through them in the first pass, mark the questions that you could not answer by yourself. Then, in second pass go through only the difficult questions.
After going through this book 2-3 times, you will be well prepared to face a technical interview on Python for an experienced programmer.
Python Interview Questions
2. What are the benefits of using Python?
Python is strong that even Google uses it. Some of the benefits of using Python are as follows:
i. Efficient: Python is very efficient in memory management. For a large data set like Big Data, it is much easier to program in Python. ii. Faster: Though Python code is interpreted, still Python has very fast performance. iii. Wide usage: Python is widely used among different organizations for different projects. Due to this wide usage, there are thousands of add-ons available for use with Python. iv. Easy to learn: Python is quite easy to learn. This is the biggest benefit of using Python. Complex tasks can be very easily implemented in Python.
3. How will you specify source code
encoding in a Python source file?
By default, every source code file in Python is in UTF-8 encoding. But we can also specify our own encoding for source files. This can be done by adding following line after #! line in the source file.
-- coding: encoding --In the above line we can replace encoding with the encoding that we want to use.
5. What is Pickling in Python?
Pickling is a process by which a Python object hierarchy can be converted into a byte stream. The reverse operation of Pickling is Unpickling.
Python has a module named pickle. This module has the implementation of a powerful algorithm for serialization and de-serialization of Python object structure.
Some people also call Pickling as Serialization or Marshalling.
With Serialization we can transfer Python objects over the network. It is also used in persisting the state of a Python object. We can write it to a file or a database.
6. How does memory management work in
Python?
There is a private heap space in Python that contains all the Python objects and data structures. In CPython there is a memory manager responsible for managing the heap space.
There are different components in Python memory manager that handle segmentation, sharing, caching, memory pre-allocation etc.
Python memory manager also takes care of garbage collection by using Reference counting algorithm.
8. What is the difference between a Tuple
and List in Python?
In Python, Tuple and List are built-in data structures.
Some of the differences between Tuple and List are as follows:
I. Syntax: A Tuple is enclosed in parentheses: E. myTuple = (10, 20, “apple”); A List is enclosed in brackets: E. myList = [10, 20, 30];
II. Mutable: Tuple is an immutable data structure. Whereas, a List is a mutable data structure.
III. Size: A Tuple takes much lesser space than a List in Python.
IV. Performance: Tuple is faster than a List in Python. So it gives us good performance.
V. Use case: Since Tuple is immutable, we can use it in cases like Dictionary creation. Whereas, a List is preferred in the use case where data can alter.
9. What is a Python Decorator?
A Python Decorator is a mechanism to wrap a Python function and modify its behavior by adding more functionality to it. We can use @ symbol to call a Python Decorator function.
11. What is the difference between List and
Dictionary data types in Python?
Main differences between List and Dictionary data types in Python are as follows:
I. Syntax: In a List we store objects in a sequence. In a Dictionary we store objects in key-value pairs.
II. Reference: In List we access objects by index number. It starts from 0 index. In a Dictionary we access objects by key specified at the time of Dictionary creation.
III. Ordering: In a List objects are stored in an ordered sequence. In a Dictionary objects are not stored in an ordered sequence.
IV. Hashing: In a Dictionary, keys have to be hashable. In a List there is no need for hashing.
12. What are the different built-in data
types available in Python?
Some of the built-in data types available in Python are as follows:
Numeric types: These are the data types used to represent numbers in Python.
int: It is used for Integers
long: It is used for very large integers of non-limited length.
float: It is used for decimal numbers.
complex: This one is for representing complex numbers
Sequence types: These data types are used to represent sequence of characters or objects.
str: This is similar to String in Java. It can represent a sequence of characters.
bytes: This is a sequence of integers in the range of 0-255.
byte array: like bytes, but mutable (see below); only available in Python 3
list: This is a sequence of objects.
tuple: This is a sequence of immutable objects.
Sets: These are unordered collections.
set: This is a collection of unique objects.
frozen set: This is a collection of unique immutable objects.
Mappings: This is similar to a Map in Java.
13. What is a Namespace in Python?
A Namespace in Python is a mapping between a name and an object. It is currently implemented as Python dictionary.
E. the set of built-in exception names, the set of built-in names, local names in a function
At different moments in Python, different Namespaces are created. Each Namespace in Python can have a different lifetime.
For the list of built-in names, Namespace is created when Python interpreter starts.
When Python interpreter reads the definition of a module, it creates global namespace for that module.
When Python interpreter calls a function, it creates local namespace for that function.
14. How will you concatenate multiple
strings together in Python?
We can use following ways to concatenate multiple string together in Python:
I. use + operator:
E. >>> fname="John" >>> lname="Ray" >>> print fname+lname JohnRay
II. use join function:
E. >>> ''.join(['John','Ray']) 'JohnRay'
Top 50 Python Interview Questions And answers
Course: Fundamentals of Computer and IT Laboratory (UGCA1906)
University: I. K. Gujral Punjab Technical University
- Discover more from: