What is Python?
- Python is a high-level, interpreted programming language known for its readability and versatility. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
What are Python’s built-in data types?
- Common built-in data types include integers, floats, strings, lists, tuples, sets, and dictionaries.
What is the difference between a list and a tuple?
- Lists are mutable (can be changed), while tuples are immutable (cannot be changed). Lists use square brackets
[]
, and tuples use parentheses()
.
How do you handle exceptions in Python?
- Exceptions are handled using
try
andexcept
blocks. You can also usefinally
for cleanup actions andelse
for code that should run if no exceptions occur.
What are list comprehensions?
- List comprehensions provide a concise way to create lists by iterating over an iterable and applying an expression. For example:
[x**2 for x in range(10)]
.
What is a lambda function?
- A lambda function is an anonymous function defined using the
lambda
keyword. It can take any number of arguments but has a single expression. For example:lambda x: x**2
.
What are decorators in Python?
- Decorators are functions that modify the behavior of another function. They are used to wrap another function, allowing you to add functionality before or after the original function runs.
Explain the difference between deep copy and shallow copy.
- A shallow copy creates a new object but inserts references into it to the objects found in the original. A deep copy creates a new object and recursively adds copies of nested objects found in the original.
What is the purpose of the self
keyword in classes?
- The
self
keyword represents the instance of the class. It is used to access variables that belong to the class and differentiate between instance attributes and local variables.
How do you read and write files in Python?
- You can use the built-in
open()
function to read or write files. For example,with open('file.txt', 'r') as f:
opens a file for reading.
What is the difference between ==
and is
?
==
checks for value equality (whether the values are the same), whileis
checks for reference equality (whether both variables point to the same object).
How do you manage packages in Python?
- Python uses
pip
as a package manager. You can install packages usingpip install package_name
.
What is a Python module?
- A module is a file containing Python code that can define functions, classes, and variables. It can be imported and used in other Python scripts.
What are Python’s built-in functions?
- Some common built-in functions include
len()
,type()
,print()
,sum()
, andsorted()
.
What is the Global Interpreter Lock (GIL)?
- The GIL is a mechanism that prevents multiple native threads from executing Python bytecodes at once. It ensures thread safety but can limit performance in CPU-bound multi-threaded programs.
How do you handle command-line arguments in Python?
- Command-line arguments can be handled using the
sys
module (sys.argv
) or theargparse
module for more complex argument parsing.
What is the purpose of __init__
in Python classes?
- The
__init__
method is the constructor of a class. It is called when an instance of the class is created, allowing you to initialize instance attributes.
Explain the difference between Python 2 and Python 3.
- Python 3 introduced many improvements and new features, including print as a function, better Unicode support, and changes to integer division. Python 2 is no longer supported.
What are generators in Python?
- Generators are a type of iterable, created using functions with the
yield
keyword. They allow for lazy evaluation and can be more memory efficient than regular functions.
What is the purpose of the with
statement?
The with
statement is used for resource management and exception handling. It ensures that resources are properly acquired and released, such as file handles.