Python Flashcards

1
Q

How do you type cast in Python?

A

desired_type(x)
ex: int(5.0)

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

What are the operators for division vs integer division?

A

/ - division
// - integer division

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

What is the method to insert a value at the end of a list in Python?

A

list.append(value)

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

The range method has the format range(start, stop, step); what does the following iterate through? range(2, 12, 2)

A

2, 4, 6, 8, 10

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

What is the structure of a List Comprehension in Python?

A

List_Name = [(expression(variable)) for (variable) in (List) if (condition)]

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

What does the following List Comprehension code yield?

newList = [x**2 for x in range(6) if x%2==0]
print(newList)

A

[0, 4, 16]

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

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

A

[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]

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

In Python are lists passed in functions passed by Value or by Reference?

A

Pass by Reference

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

What is Pass by Reference?

A

Reference to variable is passed, allowing direct modification

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

What is Pass by Value

A

A copy of a variable is passed, so modification does not persist outside of local scope.

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

How is Function Documentation formatted?

A

’'’Message … ‘’’

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

In Python, how do you import a library with an alias?

A

import “library” as “alias”

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

In Python, what is the difference between (5,) and (5)

A

(5,) is a tuple, while (5) is just the int 5

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

What is the NumPy function to find the norm of an Array?

A

np.linalg.norm(np.array([v1, v2,…, vk]))

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

What is the NumPy function for calculating the dot product of two vectors?

A

np.dot(np.array([v1,v2,…,vk]),np.array([u1,u2,…,uk]))

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

What is the java equivalent of ‘self’?

A

‘this’

17
Q

What is the __init__ function’s java equivalent?

A

A class constructor

18
Q

What is the benefit of opening a file using a ‘with’ statement?

A

It automatically closes the file afterwards.

19
Q

Given the file data.csv, how would you open it with a ‘with’ statment?

A

with open(data.csv) as var:

20
Q

what is the python version of a try-catch block?

A

try:
except:

21
Q

Given an open file stored in variable data_file, how would you read lines via a for loop?

A

for line in data_file:

22
Q

What does “for line in data_file” return?

A

a string of a line from the file data_file.

23
Q

Given the function open(“file_name”, mode = ?), what are the options for mode?

A

“r”: read only
“w”: write. Create if not found
“a”: append. Create if not found
“x”: create

24
Q
A