Python Flashcards

1
Q

declare a list object

A

my_list = [1, 2, 3, 4, 5]

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

Declare a dictionary object

A

my_dict = {‘key1’: ‘value1’, ‘key2’: ‘value2’}

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

Pull a value from a dictionary by key

A

value_from_dict = my_dict[‘key2’]

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

Pull a value from a list

A

value_from_list = my_list[2]

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

What do class decorators do

A

they are functions that modify the behavior of a class or methods within a class. They allow you to add functionality to classes or methods without modifying their underlying code.

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

Explain the difference between == and is in Python

A

The == operator is used to compare the values of two objects, while the is operator is used to check if two variables refer to the same object in memory.

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

What is a decorator in Python?

A

A decorator in Python is a design pattern that allows you to modify or extend the behavior of functions or classes without directly modifying their code. Decorators are implemented as callable objects that take a function or class as input and return a new function or class with additional functionality.

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

What is a generator in Python?

A

A generator in Python is a special type of iterator that generates values on the fly rather than storing them in memory. It allows you to iterate over a sequence of values one at a time, which is memory-efficient and suitable for handling large datasets or infinite sequences.

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

What is the purpose of the __init__ method in Python classes?

A

a special method in Python classes that is called automatically when a new instance of the class is created. It is used to initialize the object’s attributes or perform any necessary setup operations.

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

Explain the difference between lists and tuples in Python

A

lists are mutable (modifiable) while tuples are immutable (unchangeable). Lists are defined using square brackets [ ], and tuples are defined using parentheses ( ).

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

Declare a tuple object

A

my_tuple = (1, 2, 3, 4, 5)

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

What is the difference between var= (42) and var= (42,)

A

The first is the value 42 and the second is a tuple because of the comma

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

of dictionaries, tuples and lists – which can not be changed

A

Tuples

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

List the 3 types of “list” objects with the characters that declare them

A

1 - Dictionaries: Declared using curly braces {} with key-value pairs separated by colons : ({‘key’: ‘value’}).
2 - Tuples: Declared using parentheses () with comma-separated values ((1, 2, 3)).
3 - Lists: Declared using square brackets [] with comma-separated values ([1, 2, 3])

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

of dictionaries, tuples and lists – which is not ordered

A

Dictionary

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

of dictionaries, tuples and lists – which can have duplicates

A

Tuples and lists

17
Q

Show me a count of null values per column for a dataframe

A

df.isna().sum()

18
Q

Define a function that takes either an array or a PD series as an input

A

def hampel(data: Union[np.ndarray, pd.Series]) -> Result:

19
Q

Cycle through items in a list that has 30 elements

A

for num in range(29):
print(num)

20
Q

Is python 0 based or 1 based

A

0 based

21
Q

Return the top 10 rows of a dataframe named dataset

A

dataset.head(10)

22
Q

What character do you add in front of a path string to make windows \ work?

A

r
In Python, the r before a string literal indicates that the string is a “raw” string. This means that backslashes () within the string are treated as literal backslashes, and not as escape characters. This is particularly useful for Windows file paths, which often contain backslashes.

23
Q

What method on a dataframe summarizes the data with mean?

A

print(dataset.describe())

It shows:

Count: The number of non-null entries in each column.

Mean
Standard Deviation (std)

Minimum (min)

25% (1st quartile): The value below which

25% of the data falls.

50% (median): The middle value, also known as the median.

75% (3rd quartile): The value below which

75% of the data falls.
Maximum (max): The largest value in each column.

24
Q
A