copy()
: Creates a shallow copy of the object, meaning if the object contains nested objects, only references to those objects are copied.deepcopy()
: Creates a deep copy of the object, recursively copying all nested objects, so the original and copied objects are completely independent.Python is a high-level, interpreted, and general-purpose programming language. It emphasizes code readability with its use of significant indentation. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
Python includes several built-in data types:
int
, float
, complex
list
, tuple
, range
str
set
, frozenset
dict
bool
bytes
, bytearray
, memoryview
NoneType
[]
.()
.Decorators in Python are a way to modify or enhance the functionality of functions or methods without changing their actual code. They are applied with the @decorator_name
syntax and are often used for logging, access control, caching, etc.
==
checks for value equality (whether two objects have the same value).is
checks for identity equality (whether two objects are the same in memory).A generator is a function that returns an iterator, allowing you to iterate over data without storing the entire data in memory. It is defined using the yield
keyword.
self
represents the instance of the class. It allows access to the instance’s attributes and methods. It must be the first parameter in every method defined in a class.
__str__()
is used to define the "informal" string representation of an object, primarily used for human-readable output.__repr__()
is used to define the "formal" string representation, often used for debugging or in the Python shell. Ideally, __repr__()
should return a string that can be used to recreate the object.List comprehension provides a concise way to create lists by applying an expression to each element in an iterable.
Example:
squares = [x**2 for x in range(10)]
math.py
is a module.__init__.py
file.range()
returns a list in Python 2.x and a generator in Python 3.x.xrange()
only exists in Python 2.x and behaves like range()
in Python 3.x (i.e., it returns a generator-like object).The GIL is a mutex that allows only one thread to execute Python bytecode at a time, even in multi-threaded programs. This can be a bottleneck for CPU-bound tasks but doesn't affect I/O-bound tasks much.
The with
statement simplifies exception handling and resource management, ensuring that resources (e.g., files) are properly cleaned up after use. It is commonly used for working with files:
with open('file.txt', 'r') as file:
content = file.read()
print
is a function in Python 3 (print()
) and a statement in Python 2.5/2 == 2.5
), while in Python 2, it returns an integer (5/2 == 2
).The pass
statement is a placeholder that does nothing. It’s used where syntactically some code is required but you don't want to execute anything.
Example:
def empty_function():
pass
The yield
keyword is used in a function to return a generator. Instead of returning a value and exiting, yield
returns a value and suspends the function’s state, allowing it to resume where it left off when the next value is requested.
You can handle exceptions using the try
and except
blocks:
try:
# code that might throw an exception
except SomeException as e:
# code to handle the exception
Python's built-in data structures include:
An iterator is an object that can be iterated upon, meaning that it has a __next__()
method that returns the next value in the sequence. Iterators must implement two methods: __iter__()
and __next__()
.
A closure is a function that captures the variables from its enclosing scope even after the outer function has finished executing.
Example:
def outer(x):
def inner(y):
return x + y
return inner
add_five = outer(5)
print(add_five(3)) # Output: 8
Some common built-in functions in Python include:
len()
, max()
, min()
, sorted()
, sum()
, input()
, type()
, range()
, str()
, etc.sort()
modifies the list in place and doesn’t return a value.sorted()
returns a new sorted list and does not modify the original list.An abstract class is a class that cannot be instantiated directly. It can contain abstract methods that must be implemented by subclasses. It is defined using the abc
module.
Example:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
del
: Deletes an item at a specific index or deletes an entire object.remove()
: Removes the first occurrence of a specified value from a list.Common file modes include:
'r'
– read'w'
– write'a'
– append'b'
– binary (e.g., rb
for reading binary files)__init__()
is the constructor method in Python classes. It is automatically called when a new object is created and initializes the object’s attributes.
cProfile
.
This list covers a wide range of topics to help you get ready for your Python interview. Best of luck!
A lambda function is an anonymous function defined using the lambda
keyword. It can have any number of arguments but only one expression.
Example:
square = lambda x: x**2