Software Engineering - Python Interview Qs Flashcards
What is the difference between a list and a tuple in Python?
Lists are mutable (can be modified) and defined with square brackets [], while tuples are immutable (cannot be modified) and defined with parentheses ().
What is the purpose of the self keyword in Python?
self is a reference to the current instance of a class, used to access its attributes and methods. It is the first parameter of instance methods.
What is the difference between == and is in Python?
== compares the values of two objects, while is compares their identity (memory address).
Explain the concept of list comprehension in Python.
List comprehension is a concise way to create lists based on existing lists or other iterable objects, using a single line of code with a for loop and optional if condition.
What are generators in Python?
Generators are functions that return an iterator object, allowing you to iterate over a sequence of values without storing them all in memory at once. They use the yield keyword instead of return.
What is the Global Interpreter Lock (GIL) in Python?
The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes simultaneously. It can limit the performance of CPU-bound multithreaded programs.
What are decorators in Python? Provide an example.
Decorators are a way to modify or enhance the behavior of functions or classes without directly changing their source code, using the @decorator_name syntax. Example: @staticmethod, @classmethod, or custom decorators.
What is the difference between shallow copy and deep copy in Python?
Shallow copy creates a new object but references the original object’s nested objects, while deep copy creates a completely independent copy of the object and all its nested objects.
How do you handle exceptions in Python?
Exceptions are handled using try/except/finally blocks. The try block contains the code that may raise an exception, the except block handles specific exceptions, and the finally block executes regardless of whether an exception occurred.
What is the purpose of the if name == ‘main’: idiom in Python?
This idiom is used to determine whether a Python script is being run directly (as the main program) or being imported as a module. Code inside the if block will only execute when the script is run directly.
What are lambda functions in Python? Provide an example.
Lambda functions are small, anonymous functions defined with the lambda keyword, often used for short, one-line operations. Example: lambda x: x ** 2.
What is the difference between a module and a package in Python?
A module is a single Python file containing definitions and statements, while a package is a directory of Python modules with a special init.py file, used to organize related modules hierarchically.
Explain the concept of duck typing in Python.
Duck typing is a programming style where the suitability of an object is determined by the presence of certain methods and properties, rather than its actual type. It emphasizes the behavior of objects over their explicit class or type.
What are the differences between Python 2 and Python 3?
Python 3 introduced changes in print syntax (print() function), integer division (/ returns float), Unicode support (all strings are Unicode by default), and some renamed or removed functions. Python 2 is no longer maintained as of January 2020.
How do you create a virtual environment in Python?
Virtual environments can be created using the venv module in Python 3, or the virtualenv package in Python 2. Example: python3 -m venv myenv.