Python Flashcards
declare a list object
my_list = [1, 2, 3, 4, 5]
Declare a dictionary object
my_dict = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
Pull a value from a dictionary by key
value_from_dict = my_dict[‘key2’]
Pull a value from a list
value_from_list = my_list[2]
What do class decorators do
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.
Explain the difference between == and is in Python
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.
What is a decorator in Python?
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.
What is a generator in Python?
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.
What is the purpose of the __init__ method in Python classes?
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.
Explain the difference between lists and tuples in Python
lists are mutable (modifiable) while tuples are immutable (unchangeable). Lists are defined using square brackets [ ], and tuples are defined using parentheses ( ).
Declare a tuple object
my_tuple = (1, 2, 3, 4, 5)
What is the difference between var= (42) and var= (42,)
The first is the value 42 and the second is a tuple because of the comma
of dictionaries, tuples and lists – which can not be changed
Tuples
List the 3 types of “list” objects with the characters that declare them
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])
of dictionaries, tuples and lists – which is not ordered
Dictionary
of dictionaries, tuples and lists – which can have duplicates
Tuples and lists
Show me a count of null values per column for a dataframe
df.isna().sum()
Define a function that takes either an array or a PD series as an input
def hampel(data: Union[np.ndarray, pd.Series]) -> Result:
Cycle through items in a list that has 30 elements
for num in range(29):
print(num)
Is python 0 based or 1 based
0 based
Return the top 10 rows of a dataframe named dataset
dataset.head(10)
What character do you add in front of a path string to make windows \ work?
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.
What method on a dataframe summarizes the data with mean?
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.
What does a while loop look like?
i = 1
while i < 6:
print(i)
i += 1
Divide a from b and drop the remainder
a//b
get the remainder from a / b
a % b
% is the modulo operator
What is the Python bitwise operator for and
&
What is the Python bitwise operator for OR
|
What is the Python bitwise operator for not
~
Show me a for loop that goes from 1 to 1000
for i in range(1, 1001):
print(i)
In a CI/CD environment what to Github, Terraform, K8 and Jenkins do
GitHub: Manages source code and collaboration.
Jenkins: Automates CI/CD pipelines, building, testing, and deploying applications.
Terraform: Provisions and manages infrastructure as code.
Kubernetes: Orchestrates and manages containerized applications.
Name 4 key parts of a matplotlib chart
1 - plt.figure(figsize=(17,7))
2 - plt.plot(df_ticker[‘Date’], df_ticker[‘Wealth_Index’], label=’Wealth_Index’)
3 - plt.legend()
4 plt.show()
What libraries do you import for a chart
import matplotlib.pyplot as plt
import seaborn as sns