Python Questions Flashcards
What is __init__() used for?
init is a special method in Python also known as constructor
it is automatically called when an object of class is created
it allows the class to initialize the object’s attributes by setting their initial values
it can take arguments which can also be used to set initial values for attributes
class Book:
def __init__(self, title, author, pages):
# Initialize attributes
self.title = title
self.author = author
self.pages = pages
def display_info(self): print(f'Title: {self.title}, Author: {self.author}, Pages: {self.pages}')
Creating an instance of the Book class
my_book = Book(‘To Kill a Mockingbird’, ‘Harper Lee’, 281)
Displaying the book information
my_book.display_info()
What is the Python “with” statement designed for?
It is used for exception handling to make code cleaner and simpler
it’s usually used for the management of common resources like creating, editing, and saving a file
Example:
with open(‘myfile.txt’, ‘w’) as file:
file.write(‘DataCamp black friday sale!’)
This code creates or opens myfile.txt in write mode.
* It writes the string ‘DataCamp Black Friday Sale!!!’ into the file.
* The with statement ensures that the file is properly closed after the writing is complete.
Explain List, Dictionary, and Tuple comprehension with an example.
my_list = [I for I in range(1,10)]
my_list
→ 1,2,…9
my_dict = {I for I in range(1,10)}
my_dict
my_tuple = (I for I in range(1,10))
my_tuple
→ generator object <genexpr> at 0x7fb91b151430></genexpr>
What is the difference between a mutable data type and an immutable data type?
Mutable:
list, dictionary, Set
Immutable: numeric, string, and tuple
What is monkey patching in Python?
Monkey patching is a technique in Python where you dynamically modify or extend the behavior of a class or module at runtime. This means you can change or add methods and attributes to an existing class or module while your program is running, without modifying the original source code.
What are context managers in Python and how are they implemented?
Context managers in Python are used to manage resources like files, network connections, or locks, ensuring that they are properly acquired and released. They are commonly used with the with statement to make resource management easy and error-free.
They are implemented using with statement
Why use else in try/except construct in Python?
Is it used for exception handling in Python
else is handy when no exception is raised
What are decorators in Python?
Decorators in Python are a way to modify or enhance functions or methods without changing their actual code. They allow you to wrap another function in order to extend its behavior. Think of them as a way to “decorate” a function with extra functionality.
A decorator is a function that takes another function as an argument and extends or alters its behavior.
What are the advantages of NumPy over regular Python lists?
+ memory
Numpy arrays consume less memory than regular python lists
+Speed
take less time to perform an operation
Vesititily (Simplicity)
convenient to use as they offer simple array multiple, addition etc
What is the difference between merge, join and concatenate?
Merge is like joining 2 tables in Sql – based on a common column
pd.merge(df1, df2, how =’outer’, on = ‘Id’), by default inner
Join uses unique index rather than columns. By default it’s a left join
df1.join(df2)
Concatenate just glues 2 or more dataframes together either along rows or columns
pd.concat(df1, df2)
How do you identify and deal with missing values?
Identify using df.isnull().sum()
Dealing:
- drop: df.dropna, either entire row or the column – loose too many observations
- fill: fillna() the missing values with the constant, average, backward fill, forward fill, etc
- replace missing values with a constant string, integer, or float using replace()
- fill the missing values using an interpolation method
Which libraries in Python are used for visualization?
- Matplotlib
- Seabordn
- Plotly – for interactive and more complex appllications
- Bokeh – used for detailed graphics with a high level of interactivity across large datasets