Python Questions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is __init__() used for?

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the Python “with” statement designed for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain List, Dictionary, and Tuple comprehension with an example.

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between a mutable data type and an immutable data type?

A

Mutable:
list, dictionary, Set
Immutable: numeric, string, and tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is monkey patching in Python?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are context managers in Python and how are they implemented?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why use else in try/except construct in Python?

A

Is it used for exception handling in Python
else is handy when no exception is raised

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are decorators in Python?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the advantages of NumPy over regular Python lists?

A

+ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between merge, join and concatenate?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you identify and deal with missing values?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which libraries in Python are used for visualization?

A
  • Matplotlib
  • Seabordn
  • Plotly – for interactive and more complex appllications
  • Bokeh – used for detailed graphics with a high level of interactivity across large datasets
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
12
Q
A