Engineering, Design, Marketing, and More

Python interview questions

The most popular questions

Use our complementary questions and answers to filter and hire the best. Questions crowdsourced by our clients, answers by Punch. We provide these complementary questions to help our clients more quickly create tests for potential hires at their organizations.

Get a question answered
Punch offers four divisions of services: design, engineering, staffing, and demand

Interview questions for your next interview

Question
What are tuples in Python? How they are different than lists?
Answer
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. The main differences between lists and tuples are − Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
Question
What are Python's dictionaries?
Answer
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.

Find developers today

Hire a Punch engineer

Punch offers four divisions of services: design, engineering, staffing, and demand. Our four divisions form the core of our People Forward Approach.

Contact us
Find developers today
Question
What is PEP 8?
Answer
PEP 8 is a coding convention, a set of recommendations, about how to write your Python code more readably.
Question
How is Python interpreted?
Answer
The Python language is an interpreted language. Python programs run directly from the source code. Python converts the source code that is written by the programmer into an intermmediate language, which is again translated into machine language then executed.
Question
How is memory managed in Python?
Answer
  • Python memory is managed by the Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and the interpretor takes care of this Python private heap.
  • The allocation of Python heap space for Python objects is done by the Python memory manager. The core API gives access to some tools for the programmer to code.
  • Python also has an built-in garbage collector, which recycles all the unused memory, frees memory, and makes it available to the heap space.
Question
What are the tools that help to find bugs or perform static analysis?
Answer
PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.
Question
How are arguments passed by value or by reference?
Answer
Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result you cannot change the value of the references. However, you can change the objects if they are mutable.
Question
What is a namespace in Python?
Answer
In Python, every name introduced has a place where it lives and can be looked for. This is known as namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get the corresponding object.
Question
What is pass in Python?
Answer
Pass means, no-operation Python statement, or in other words it is a placeholder in compound statement, where there should be a blank left and nothing has to be written there.
Question
How can you share global variables across modules?
Answer
To share global variables across modules within a single program, create a special module. Import the config module in all modules of your application. The module will be available as a global variable across modules.

Find developers today

Hire a Punch engineer

Punch offers four divisions of services: design, engineering, staffing, and demand. Our four divisions form the core of our People Forward Approach.

Contact us
Find developers today
Question
What is the difference between deep and shallow copy?
Answer
Shallow copy is used to copy the reference pointers just like it copies the values. These references point to the original objects and the changes made in any member of the class will also affect the original copy of it. Whereas, deep copy doesn’t copy the reference pointers to the objects. Deep copy makes the reference to an object and the new object that is pointed by some other object gets stored. The changes made in the original copy won’t affect any other copy that uses the object.
Question
Explain delegation in Python.
Answer
Delegation is an object oriented technique (also called a design pattern). Let's say you have an object x and want to change the behaviour of just one of its methods. You can create a new class that provides a new implementation of the method you're interested in changing and delegates all other methods to the corresponding method of x. The example shows a class that captures the behavior of the file and converts data from lower to uppercase.
Question
What is the function of “self”?
Answer
“Self” is a variable that represent the instance of the object to itself. In most of the object oriented programming languages, this is passed to the methods as a hidden parameter that is defined by an object. But, in Python it is declared and passed explicitly. It is the first argument that gets created in the instance of the class A and the parameters to the methods are passed automatically. It refers to a separate instance of the variable for individual objects. This is the first argument that is used in the class instance and the “self” method is defined explicitly to all the methods that are used and present. The variables are referred as “self.xxx”.
Question
How is global value mutation used for thread-safety?
Answer
The global interpreter lock is used to allow the running of threads one at a time. This is internal to the program only and used to distribute the functionality along all the virtual machines that are used. Python allows the switching between the threads to be performed by using the byte code instructions that are used to provide platform-independence. The sys.setcheckinterval() method is used to allow switching to occur during the implementation of the program and the instruction. This provides the understanding in the field of accounting to use the byte code implementation that makes it portable to use. The atomicity can be provided such that the shared variables can be given as built-in data types.
Question
What is the process to run sub-process with pipes that connect both input and output?
Answer
The popen2() module is used to run sub-processes, but due to some difficulty in processing, for example, deadlock that keeps a process blocked by waiting for output from a child and the child is waiting for the input. The dead lock occurs due to the fact that parent and child don't have the synchronization and both are waiting to get the processor to provide the resources to one another. Use of the popen3() method allows the reading of stdout and stderr to take place where the internal buffer increases and there is no read() to share resources. popen2() take care of the deadlock by providing the methods like wait() and waitpid() that finish a process first and when a request comes it hands over the responsibility to the process that is waiting for the resources.
Question
Explain how Python does Compile-time and Run-time code checking?
Answer
Python performs some amount of compile-time and run-time code checking, but most of the checks 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. In fact, the code will fail with an exception only when the code execution path references the function which does not exist.

Ask a question

Ask a question, and one of our engineers will answer it.

We keep our questions nice and simple to be useful for everyone, and we may not answer or publish every question.

Your number is stored privately and never shared.
By submitting a question, you agree to our terms.
Request sent. Thank you!
Send another one