Python Flashcards
How do you type cast in Python?
desired_type(x)
ex: int(5.0)
What are the operators for division vs integer division?
/ - division
// - integer division
What is the method to insert a value at the end of a list in Python?
list.append(value)
The range method has the format range(start, stop, step); what does the following iterate through? range(2, 12, 2)
2, 4, 6, 8, 10
What is the structure of a List Comprehension in Python?
List_Name = [(expression(variable)) for (variable) in (List) if (condition)]
What does the following List Comprehension code yield?
newList = [x**2 for x in range(6) if x%2==0]
print(newList)
[0, 4, 16]
Python List Slicing; what does the following code produce?
values = [x for x in range(11)]
print(values)
print(values[1:3])
print(values[2:-1])
print(values[:2])
print(values[2:])
print(values[::2])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2]
[2, 3, 4, 5, 6, 7, 8, 9]
[0, 1]
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 2, 4, 6, 8, 10]
In Python are lists passed in functions passed by Value or by Reference?
Pass by Reference
What is Pass by Reference?
Reference to variable is passed, allowing direct modification
What is Pass by Value
A copy of a variable is passed, so modification does not persist outside of local scope.
How is Function Documentation formatted?
’'’Message … ‘’’
In Python, how do you import a library with an alias?
import “library” as “alias”
In Python, what is the difference between (5,) and (5)
(5,) is a tuple, while (5) is just the int 5
What is the NumPy function to find the norm of an Array?
np.linalg.norm(np.array([v1, v2,…, vk]))
What is the NumPy function for calculating the dot product of two vectors?
np.dot(np.array([v1,v2,…,vk]),np.array([u1,u2,…,uk]))