- Notifications
You must be signed in to change notification settings - Fork67
Devinterview-io/python-interview-questions
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
You can also find all 100 answers here 👉Devinterview.io - Python
Python is a versatile and popular programming language known for its simplicity,elegant syntax, and a vast ecosystem of libraries. Let's look at some of the key features that make Python stand out.
Python uses an interpreter, allowing developers to run codeline-by-line, making it ideal for rapid prototyping and debugging.
Python'sclean, readable syntax, often resembling plain English, reduces the cognitive load for beginners and experienced developers alike.
Python is versatile, running on various platforms, such as Windows, Linux, and macOS, without requiring platform-specific modifications.
Developers can organize their code into modular packages and reusabale functions.
The Python Package Index (PyPI) hosts over 260,000 libraries, providing solutions for tasks ranging from web development to data analytics.
From web applications to scientific computing, Python is equally proficient in diverse domains.
Python seamlessly allocates and manages memory, shielding developers from low-level tasks, such as memory deallocation.
Python infers the data type of a variable during execution, easing the declartion and manipulation of variables.
Python supports object-oriented paradigms, where everything is anobject, offering attributes and methods to manipulate data.
With its C-language API, developers can integrate performance-critical tasks and existing C modules with Python.
Python source code is processed through various steps before it can be executed. Let's explore the key stages in this process.
Python code goes through bothcompilation andinterpretation.
Bytecode Compilation: High-level Python code is transformed into low-level bytecode by the Python interpreter with the help of a compiler. Bytecode is a set of instructions that Python's virtual machine (PVM) can understand and execute.
On-the-fly Interpretation: The PVM reads and executes bytecode instructions in a step-by-step manner.
This dual approach known as "compile and then interpret" is what sets Python (and certain other languages) apart.
While some programming languages compile directly to machine code, Python compiles to bytecode. This bytecode is then executed by the Python virtual machine. This extra step of bytecode executioncan make Python slower in certain use-cases when compared to languages that compile directly to machine code.
The advantage, however, is that bytecode is platform-independent. A Python program can be run on any machine with a compatible PVM, ensuring cross-platform support.
- Lexical Analysis: The source code is broken down into tokens, identifying characters and symbols for Python to understand.
- Syntax Parsing: Tokens are structured into a parse tree to establish the code's syntax and grammar.
- Semantic Analysis: Code is analyzed for its meaning and context, ensuring it's logically sound.
- Bytecode Generation: Based on the previous steps, bytecode instructions are created.
While Python typically uses a combination of interpretation and compilation,JIT boosts efficiency by selectively compiling parts of the program that are frequently used or could benefit from optimization.
JIT compiles sections of the program to machine code on-the-fly. This direct machine code generation for frequently executed parts can significantly speed up those segments, blurring the line between traditional interpreters and compilers.
importdisdefexample_func():return15*20# Disassemble to view bytecode instructionsdis.dis(example_func)
Disassembling code using Python'sdis
module can reveal the underlying bytecode instructions that the PVM executes. Here's the disassembled output for the above code:
4 0 LOAD_CONST 2 (300) 2 RETURN_VALUE
PEP 8 is a style guide for Python code that promotes code consistency, readability, and maintainability. It's named after Python Enhancement Proposal (PEP), the mechanism used to propose and standardize changes to the Python language.
PEP 8 is not a set-in-stone rule book, but it provides general guidelines that help developers across the Python community write code that's visually consistent and thus easier to understand.
PEP 8 emphasizes:
- Readability: Code should be easy to read and understand, even by someone who didn't write it.
- Consistency: Codebase should adhere to a predictable style so there's little cognitive load in reading or making changes.
- One Way to Do It: Instead of offering multiple ways to write the same construct, PEP 8 advocates for a single, idiomatic style.
- Indentation: Use 4 spaces for each level of logical indentation.
- Line Length: Keep lines of code limited to 79 characters. This number is a guideline; longer lines are acceptable in certain contexts.
- Blank Lines: Use them to separate logical sections but not excessively.
- Class Names: Prefer
CamelCase
. - Function and Variable Names: Use
lowercase_with_underscores
. - Module Names: Keep them short and in
lowercase
.
- Use triple quotes for documentation strings.
- Comments should be on their own line and explain the reason for the following code block.
- Operators: Surround them with a single space.
- Commas: Follow them with a space.
Here is thePEP8
compliant code:
importosdefwalk_directory(path):fordirpath,dirnames,filenamesinos.walk(path):forfilenameinfilenames:file_path=os.path.join(dirpath,filename)print(file_path)walk_directory('/path/to/directory')
In Python,both memory allocation andgarbage collection are handled discretely.
The "heap" is the pool of memory for storing objects. The Python memory manager allocates and deallocates this space as needed.
In latest Python versions, the
obmalloc
system is responsible for small object allocations. This system preallocates small and medium-sized memory blocks to manage frequently created small objects.The
allocator
abstracts the system-level memory management, employing memory management libraries likeGlibc
to interact with the operating system.Larger blocks of memory are primarily obtained directly from the operating system.
Stack andHeap separation is joined by "Pool Allocator" for internal use.
Python employs a method calledreference counting along with acycle-detecting garbage collector.
Every object has a reference count. When an object's count drops to zero, it is immediately deallocated.
This mechanism is swift, often releasing objects instantly without the need for garbage collection.
However, it can be insufficient in handlingcircular references.
Python has a separate garbage collector that periodically identifies and deals with circular references.
This is, however, a more time-consuming process and is invoked less frequently than reference counting.
Python handles memory management quite differently from languages like C or C++:
In Python, the developer isn't directly responsible for memory allocations or deallocations, reducing the likelihood of memory-related bugs.
The memory manager in Python is what's known as a"general-purpose memory manager" that can be slower than the dedicated memory managers of C or C++ in certain contexts.
Python, especially due to the existence of a garbage collector, might have memory overhead compared to C or C++ where manual memory management often results in minimal overhead is one of the factors that might contribute to Python's sometimes slower performance.
The level of memory efficiency isn't as high as that of C or C++. This is because Python is designed to be convenient and easy to use, often at the expense of some performance optimization.
Python offers numerousbuilt-in data types that provide varying functionalities and utilities.
Represents a whole number, such as 42 or -10.
Represents a decimal number, like 3.14 or -0.01.
Comprises a real and an imaginary part, like 3 + 4j.
Represents a boolean value, True or False.
A sequence of unicode characters enclosed within quotes.
An ordered collection of items, often heterogeneous, enclosed within parentheses.
A set of unique, immutable objects, similar to sets, enclosed within curly braces.
Represents a group of 8-bit bytes, often used with binary data, enclosed within brackets.
Resembles the 'bytes' type but allows mutable changes.
Indicates the absence of a value.
A versatile ordered collection that can contain different data types and offers dynamic sizing, enclosed within square brackets.
Represents a unique set of objects and is characterized by curly braces.
A versatile key-value paired collection enclosed within braces.
Points to the memory used by another object, aiding efficient viewing and manipulation of data.
Offers storage for a specified type of data, similar to lists but with dedicated built-in functionalities.
A double-ended queue distinguished by optimized insertion and removal operations from both its ends.
The base object from which all classes inherit.
Grants the capability to assign attributes to it.
Represents a module body containing attributes.
Defines a particular kind of function.
Let's look at the difference betweenmutable andimmutable objects.
- Mutable Objects: Can be modified after creation.
- Immutable Objects: Cannot be modified after creation.
- Mutable: Lists, Sets, Dictionaries
- Immutable: Tuples, Strings, Numbers
Here is the Python code:
# Immutable objects (int, str, tuple)num=42text="Hello, World!"my_tuple= (1,2,3)# Trying to modify will raise an errortry:num+=10text[0]='M'# This will raise a TypeErrormy_tuple[0]=100# This will also raise a TypeErrorexceptTypeErrorase:print(f"Error:{e}")# Mutable objects (list, set, dict)my_list= [1,2,3]my_dict= {'a':1,'b':2}# Can be modified without issuesmy_list.append(4)delmy_dict['a']# Checking the changesprint(my_list)# Output: [1, 2, 3, 4]print(my_dict)# Output: {'b': 2}
Immutability offers benefits such assafety in concurrent environments and facilitatingpredictable behavior.
Mutability, on the other hand, often improvesperformance by avoiding copy overhead and redundant computations.
Reading and Writing: Immutable objects typically favorreading overwriting, promoting a more straightforward and predictable code flow.
Memory and Performance: Mutability can be more efficient in terms of memory usage and performance, especially concerning large datasets, thanks to in-place updates.
Choosing between the two depends on the program's needs, such as the required data integrity and the trade-offs between predictability and performance.
Exception handling is a fundamental aspect of Python, and it safeguards your code against unexpected errors or conditions. Key components of exception handling in Python include:
Try: The section of code where exceptions might occur is placed within a
try
block.Except: Any possible exceptions that are
raised
by thetry
block are caught and handled in theexcept
block.Finally: This block ensures a piece of code always executes, regardless of whether an exception occurred. It's commonly used for cleanup operations, such as closing files or database connections.
It's good practice tohandle specific exceptions. However, a moregeneral approach can also be taken. When doing the latter, ensure the general exception handling is at the end of the chain, as shown here:
try:risky_operation()exceptIndexError:# Handle specific exception types first.handle_index_error()exceptExceptionase:# More general exception must come last.handle_generic_error()finally:cleanup()
Use this mechanism totrigger and manage exceptions under specific circumstances. This can be particularly useful when building custom classes or functions where specific conditions should be met.
Raise a specific exception:
defdivide(a,b):ifb==0:raiseZeroDivisionError("Divisor cannot be zero")returna/btry:result=divide(4,0)exceptZeroDivisionErrorase:print(e)
Raise a general exception:
defsome_risky_operation():ifcondition:raiseException("Some generic error occurred")
Thewith
keyword provides a more efficient and clean way to handle resources, like files, ensuring their proper closure when operations are complete or in case of any exceptions. The resource should implement acontext manager
, typically by having__enter__
and__exit__
methods.
Here's an example using a file:
withopen("example.txt","r")asfile:data=file.read()# File is automatically closed when the block is exited.
There are times when not raising an exception is appropriate. You can usepass
orcontinue
in an exception block when you want to essentially ignore an exception and proceed with the rest of your code.
pass
: Simply does nothing. It acts as a placeholder.try:risky_operation()exceptSomeSpecificException:pass
continue
: This keyword is generally used in loops. It moves to the next iteration without executing the code that follows it within the block.foriteminmy_list:try:perform_something(item)exceptExceptionType:continue```
else
withtry-except
blocks: Theelse
block after atry-except
block will only be executed if no exceptions are raised within thetry
blocktry:some_function()exceptSpecificException:handle_specific_exception()else:no_exception_raised()
Python 3 introduced the better handling of uncaught exceptions by providing an optional function for printing stack traces. Thesys.excepthook
can be set to match any exception in the module as long as it has ahook
attribute.
Here's an example for this test module:
# test.pyimportsysdefexcepthook(type,value,traceback):print("Unhandled exception:",type,value)# Call the default exception hooksys.__excepthook__(type,value,traceback)sys.excepthook=excepthookdeftest_exception_hook():throw_some_exception()
When run, callingtest_exception_hook
will print "Unhandled exception: ..."
Note:sys.excepthook
will not capture exceptions raised as the result of interactive prompt commands, such as SyntaxError or KeyboardInterrupt.
Lists andTuples in Python share many similarities, such as being sequences and supporting indexing.
However, these data structures differ in key ways:
Mutability: Lists are mutable, allowing you to add, remove, or modify elements after creation. Tuples, once created, are immutable.
Performance: Lists are generally slower than tuples, most apparent in tasks like iteration and function calls.
Syntax: Lists are defined with square brackets
[]
, whereas tuples use parentheses()
.
Lists are ideal for collections that may change in size and content. They are the preferred choice for storing data elements.
Tuples, due to their immutability and enhanced performance, are a good choice for representing fixed sets of related data.
my_list= ["apple","banana","cherry"]my_list.append("date")my_list[1]="blackberry"
my_tuple= (1,2,3,4)# Unpacking a tuplea,b,c,d=my_tuple
Python dictionaries are versatile data structures, offering key-based access for rapid lookups. Let's explore various data within dictionaries and techniques to create and manipulate them.
- Adictionary in Python contains a collection of
key:value
pairs. - Keys must be unique and are typically immutable, such as strings, numbers, or tuples.
- Values can be of any type, and they can be duplicated.
You can use several methods to create a dictionary:
Literal Definition: Define key-value pairs within curly braces { }.
From Key-Value Pairs: Use the
dict()
constructor or the{key: value}
shorthand.Using the
dict()
Constructor: This can accept another dictionary, a sequence of key-value pairs, or named arguments.Comprehensions: This is a concise way to create dictionaries using a single line of code.
zip()
Function: This creates a dictionary by zipping two lists, where the first list corresponds to the keys, and the second to the values.
Here is a Python code:
# Dictionary literal definitionstudent= {"name":"John Doe","age":21,"courses": ["Math","Physics"]}
Here is the Python code:
# Using the `dict()` constructorstudent_dict=dict([ ("name","John Doe"), ("age",21), ("courses", ["Math","Physics"])])# Using the shorthand syntaxstudent_dict_short= {"name":"John Doe","age":21,"courses": ["Math","Physics"]}
Here is a Python code:
keys= ["a","b","c"]values= [1,2,3]zipped=zip(keys,values)dict_from_zip=dict(zipped)# Result: {"a": 1, "b": 2, "c": 3}
Here is a Python code:
# Sequence of key-value pairsstudent_dict2=dict(name="Jane Doe",age=22,courses=["Biology","Chemistry"])# From another dictionarystudent_dict_combined=dict(student,**student_dict2)
Both the==
andis
operators in Python are used for comparison, but they function differently.
- The
==
operator checks forvalue equality. - The
is
operator, on the other hand, validatesobject identity,
In Python, every object is unique, identifiable by its memory address. Theis
operator uses this memory address to check if two objects are the same, indicating they both point to the exact same instance in memory.
is
: Compares the memory address or identity of two objects.==
: Compares the content or value of two objects.
Whileis
is primarily used forNone checks, it's generally advisable to use==
for most other comparisons.
==
: Use for equality comparisons, like when comparing numeric or string values.is
: Use for comparing membership or when dealing with singletons likeNone.
Python functions are the building blocks of code organization, often serving predefined tasks within modules and scripts. They enable reusability, modularity, and encapsulation.
- Function Signature: Denoted by the
def
keyword, it includes the function name, parameters, and an optional return type. - Function Body: This section carries the core logic, often comprising conditional checks, loops, and method invocations.
- Return Statement: The function's output is determined by this statement. When None is specified, the function returns by default.
- Local Variables: These variables are scoped to the function and are only accessible within it.
When a function is called:
Stack Allocation: A stack frame, also known as an activation record, is created to manage the function's execution. This frame contains details like the function's parameters, local variables, andinstruction pointer.
Parameter Binding: The arguments passed during the function call are bound to the respective parameters defined in the function header.
Function Execution: Control is transferred to the function body. The statements in the body are executed in a sequential manner until the function hits a return statement or the end of the function body.
Return: If a return statement is encountered, the function evaluates the expression following the
return
and hands the value back to the caller. The stack frame of the function is then popped from the call stack.Post Execution: If there's no
return
statement, or if the function ends without evaluating any return statement,None
is implicitly returned.
- Function Parameters: These are a precursor to local variables and are instantiated with the values passed during function invocation.
- Local Variables: Created using an assignment statement inside the function and cease to exist when the function execution ends.
- Nested Scopes: In functions within functions (closures), non-local variables - those defined in the enclosing function - are accessible but not modifiable by the inner function, without using the
nonlocal
keyword.
If a variable is not defined within a function, the Python runtime will look for it in the global scope. This behavior enables functions to access and even modify global variables.
Functions offer a level of encapsulation, potentially reducing side effects by ensuring that data and variables are managed within a controlled environment. Such containment can help enhance the robustness and predictability of a codebase. As a best practice, minimizing the reliance on global variables can lead to more maintainable, reusable, and testable code.
ALambda function, orlambda, for short, is a small anonymous function defined using thelambda
keyword in Python.
While you can certainly use named functions when you need a function for something in Python, there are places where a lambda expression is more suitable.
- Anonymity: Lambdas are not given a name in the traditional sense, making them suited for one-off uses in your codebase.
- Single Expression Body: Their body is limited to a single expression. This can be an advantage for brevity but a restriction for larger, more complex functions.
- Implicit Return: There's no need for an explicit
return
statement. - Conciseness: Lambdas streamline the definition of straightforward functions.
- Map, Filter, and Reduce: Functions like
map
can take a lambda as a parameter, allowing you to define simple transformations on the fly. For example, doubling each element of a list can be achieved withlist(map(lambda x: x*2, my_list))
. - List Comprehensions: They are a more Pythonic way of running the same
map
orfilter
operations, often seen as an alternative to lambdas andmap
. - Sorting: Lambdas can serve as a custom key function, offering flexibility in sort orders.
- Callbacks: Often used in events where a function is needed to be executed when an action occurs (e.g., button click).
- Simple Functions: For functions that are so basic that giving them a name, especially in more procedural code, would be overkill.
- Lack of Verbose Readability: Named functions are generally preferred when their intended use is obvious from the name. Lambdas can make code harder to understand if they're complex or not used in a recognizable pattern.
- No Formal Documentation: While the function's purpose should be apparent from its content, a named function makes it easier to provide direct documentation. Lambdas would need a separate verbal explanation, typically in the code or comments.
In Python,*args
and**kwargs
are often used to pass a variable number of arguments to a function.
*args
collects a variable number of positional arguments into atuple, while**kwargs
does the same for keyword arguments into adictionary.
Here are the key features, use-cases, and their respective code examples.
How it Works: The name
*args
is a convention. The asterisk (*) tells Python to put any remaining positional arguments it receives into a tuple.Use-Case: When the number of arguments needed is uncertain.
defsum_all(*args):result=0fornuminargs:result+=numreturnresultprint(sum_all(1,2,3,4))# Output: 10
How it Works: The double asterisk (**) is used to capture keyword arguments and their values into a dictionary.
Use-Case: When a function should accept an arbitrary number of keyword arguments.
defprint_values(**kwargs):forkey,valueinkwargs.items():print(f"{key}:{value}")# Keyword arguments are captured as a dictionaryprint_values(name="John",age=30,city="New York")# Output:# name: John# age: 30# city: New York
In Python, adecorator is a design pattern and a feature that allows you to modify functions and methods dynamically. This is done primarily to keep the code clean, maintainable, and DRY (Don't Repeat Yourself).
- Decorators wrap a target function, allowing you to execute custom code before and after that function.
- They are typicallyhigher-order functions that take a function as an argument and return a new function.
- This paradigm of "functions that modify functions" is often referred to asmetaprogramming.
- Authorization and Authentication: Control user access.
- Logging: Record function calls and their parameters.
- Caching: Store previous function results for quick access.
- Validation: Verify input parameters or function output.
- Task Scheduling: Execute a function at a specific time or on an event.
- Counting and Profiling: Keep track of the number of function calls and their execution time.
Here is the Python code:
fromfunctoolsimportwraps# 1. Basic Decoratordefmy_decorator(func):@wraps(func)# Ensures the original function's metadata is preserveddefwrapper(*args,**kwargs):print('Something is happening before the function is called.')result=func(*args,**kwargs)print('Something is happening after the function is called.')returnresultreturnwrapper@my_decoratordefsay_hello():print('Hello!')say_hello()# 2. Decorators with Argumentsdefdecorator_with_args(arg1,arg2):defactual_decorator(func):@wraps(func)defwrapper(*args,**kwargs):print(f'Arguments passed to decorator:{arg1},{arg2}')result=func(*args,**kwargs)returnresultreturnwrapperreturnactual_decorator@decorator_with_args('arg1','arg2')defmy_function():print('I am decorated!')my_function()
The@decorator
syntax is a convenient shortcut for:
defsay_hello():print('Hello!')say_hello=my_decorator(say_hello)
When defining decorators, particularly those that return functions, it is good practice to use@wraps(func)
from thefunctools
module. This ensures that the original function's metadata, such as its name and docstring, is preserved.
You cancreate a Python module through one of two methods:
Define: Begin with saving a Python file with
.py
extension. This file will automatically function as a module.Create a Blank Module: Start an empty file with no extension. Name the file using the accepted module syntax, e.g.,
__init__
, for it to act as a module.
Next, useimport to access the module and its functionality.
Save the belowmath_operations.py
file :
defadd(x,y):returnx+ydefsubtract(x,y):returnx-ydefmultiply(x,y):returnx*ydefdivide(x,y):returnx/y
You can usemath_operations
module by using import as shown below:
importmath_operationsresult=math_operations.add(4,5)print(result)result=math_operations.divide(10,5)print(result)
Even though it is not required in the laterversions of Python, you can also use statementfrom math_operations import *
to import all the members such as functions and classes at once:
frommath_operationsimport*# Not recommended generally due to name collisions and readability concernsresult=add(3,2)print(result)
Before submitting the code, let's make sure to follow theBest Practice:
- Avoid Global Variables: Use a
main()
function. - Guard Against Code Execution on Import: To avoid unintended side effects, use:
if__name__=="__main__":main()
This makes sure that the block of code followingif __name__ == "__main__":
is only executed when the module is run directly and not when imported as a module in another program.
Explore all 100 answers here 👉Devinterview.io - Python
About
🟣 Python interview questions and answers to help you prepare for your next technical interview in 2025.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Uh oh!
There was an error while loading.Please reload this page.