Chapter 12 Flashcards

1
Q

What is a docstring?

A

A comment at the start of every function

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

What is a package?

A

A group of modules

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

How do you access a function from a module?

A

module_name.function_name(arguments)

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

How do you import a specific function instead of a whole module?

A

from <module_name> import <function_name></function_name></module_name>

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

How do you rename a module

A

import <module_name> as <new_name>
e.g.
- from math import sqrt as sr</new_name></module_name>

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

What is a vector?

A

An array of elements of one direction,
- can be up or down

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

What are he two types of vectors?

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

Can you add vectors or matrices together?

A

If they have the same dimensions yes

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

What does multiplying a matrix by a scalar constant do?

A

Multiplies all values in a matrix by the scalar value

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

How does matrix subtraction work?

A

Parallel values subtract,
- 2nd value is subtracted from 2nd value

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

What is the noation for a column vector?

A

[n,0]
N rows, one column

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

What is notation for a row vector?

A

[0,n]
one down, n wide

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

What is a diagonal matrix?

A

A square matrix where all values = 0 besides the diagonal values where m = n
a_mn

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

What is a triangular matrix

A

A matric where one side of the square matrix is 0 and the rest has values, split diagonally into a triangle

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

What is a tridiagonal matrix?

A

When the square matrix has 3 diagonals with values, the rest is zero

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

What is a transpose matrix?

A

When a matrix (m x n) changes to (n x m)

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

What happens when you transpose a row vector?

A

You get a column vector because m and n are getting swapped

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

Is matrix division possbile?

A

Yes
A / B = A / B^-1
I think check this out

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

Does matrix A x matrix B = matrix B x matrix A?

A

No

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

How do you divide two matrixes?

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

What is a module?

A

A file containing code that can be imported and used in other modules or scripts

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

When importing a module how do you access variables within that module?

A

dot notation,
module_name.varibale

e.g
names.py:
first = ‘Larry’
last = ‘David’

write_names.py:
import names
print(names.first, names.last)

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

What is a script?

A

A file containing python code that is passed as an input to the interpreter

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

What is the console used for?

A

Coding line by line and receiving an output each time,
- typically used for very short codes or to test syntax

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

What is dot notation?

A

module.object

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

What does python use to determine whether the program is executed as a script or as an imported module?

A

execute code as script

if __name__ == ‘__main__’:
#indented code
# run as script

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

What happens when a module is imported and called for in a script?

A

The modules code is run

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

What file extension must a module have in order to be imported?

A

.py

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

Are module imports case sensitive?

A

Yes

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

What is it called when a program requires a module to run?

A

dependency

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

What happens when you import a module into python?

A

The interpreter evaluates it immediately
- Checks if it has already been imported
- if it has, use the currently loaded module
- If not, create a new module object for the module

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

What is a module object?

A

A namespace that contains definitions from a module

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

How do you change the value of an object in a module?

A

module.object() = value

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

What happens after you change the value of an object in a module?

A

the module is reloaded with new values, though when another program imports the module, all objects are back to normal

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

When importing a module, the interpreter first looks for____?

A

An built-in module, if there is no match, then python looks in sys.path

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

What are the three initial directories that are contained in sys.path?

A
  • The directory of the executing script
  • A list of directories specified by the environment variable PYTHONPATH (this variable can be set in the OS)
  • The directory where python is installed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do you import an object from a module?

A

from module import object1,object2….
This way you don’t have to us dot notation and it saves memory

38
Q

What directory must a package include in order for its contents to be accessed?

A

__init__.py

39
Q

How do you import a module from a package?

A

from <package> import <module></module></package>

40
Q

How do you import an object inside a module inside a package?

A

from <package.module> import <object></object></package.module>

41
Q

What is the difference between import y.z and from x.y import z

A

The z in the first scenario has to be a package, subpackage, or a module
The z in the second scenario has can also be an object in a module

42
Q

What modules are built-in to python?

A
  • math
  • os
  • sys
  • copy
  • time
  • pdb
  • urllib
  • random
  • datetime
43
Q

What function in matplotlib displays the graph?

A

.show()

44
Q

What function in matplotlib plots the graph?

A

.plot(x,y, step of x axis)
- x representing the x axis, could be a list
- y representing the y axis
if the x axis is left clear then it increase in increments of 1, starting at 0
(0, y[0]) (1, y[1])

45
Q

What characters represent each variation of graph colour?

A

b =blue
r = red
g = green
y = yellow
c = cyan
m = magenta
w = white
k = black

46
Q

What characters represent each line variation for matplotlib graphs?

A
  • = dashed line
    – = solid line
    -. = dashed dotted line
    : = dotted line
47
Q

What characters represent each variation of point marker of matplotlib graphs?

A

. = point marker
, = pixel marker
o = circle marker
+ = plus marker
X = X marker
v = triangle down maker
^ = triangle up marker
> = triangle right marker
* = star marker
p = pentagon marker
1 = tri down marker
2 = tri up marker
3 = tri left marker
4 = tri right marker
h = hexagon marker
H = hexagon marker
D = diamond marker
d = thin diamond marker
| = vertical line
- = horizontal line maker
s = square marker

48
Q

What characters represent each variation of point marker of matplotlib graphs (not polygons)?

A

. = point marker
, = pixel marker
+ = plus marker
- = horizontal line maker
X = X marker
* = star marker
1 = tri down marker
2 = tri up marker
3 = tri left marker
4 = tri right marker

= vertical line

49
Q

What are polygon markers for matplotlib?

A

s = square marker
D = diamond marker
d = thin diamond marker
h = hexagon marker
H = hexagon marker
v = triangle down maker
^ = triangle up marker
> = triangle right marker
p = pentagon marker
o = circle marker

50
Q

What does the alpha property do for a graph?

A

enables transparency
- float

51
Q

What does the antialiased property do for a graph?

A

enable anti-aliasing of the line
- boolean

52
Q

What does the color property do for a graph?

A

changes colour of the line

53
Q

What does the solid_capstyle property do for a graph?

A

How the cap of a line appears

54
Q

What does the solid_joinstyle property do for a graph?

A

How the join of a line appears

55
Q

What does the data property do for a graph?

A

The arrays of x and y coordinates

56
Q

What does the label property do for a graph?

A

The label to use for the line

57
Q

What does the linestyle property do for a graph?

A

The style of the line

58
Q

What does the linewidth property do for a graph?

A

The width of the line when drawn

59
Q

What does the marker property do for a graph?

A

The style of marker to use

60
Q

What does the markersize property do for a graph?

A

The size of the marker

61
Q

What does the visible property do for a graph?

A

Show/hide the line

62
Q

what does .legend() do for a graph?

A

Create a key/legend

63
Q

What does the text(x,y, string) function do?

A

create text to put on the graph

64
Q

How is an array full of zeros declared?

A

import numpy as np
np.zeros((rows,columns))

65
Q

How do you declare a 1d array?

A

import numpy as np
np.array([1,2,3])

66
Q

what does array.linspace(num1,num2,total_nums) do?

A

take two numbers and create a list of values equally spaced between them, creating a total list of total_num values

67
Q

What does the figure() function do?

A

Create multiple windows each with their own graphic plots using matplotlib

68
Q

What does matplotlib.xlabel do(x/y)?

A

label the axis of a graph

69
Q

How do you declare the axis of a plot?

A

plt.axis(x_min,x_max,y_min,y_max)

70
Q

What does plt.subplot() do?

A

create multiple plots on one figure, determined using the parameters:
subplot(rows, columns, location)
the active sub plot/ location must be a plot on the figure, can only be a number between rows*columns

71
Q

How do you create multiple axis?

A

left_axis = figure.adds_subplot(1,2,1)
right_axis = left_axis.twin()
creates a variable right_axis which can now be labeled:
right_axis.set_ylabel(‘’)

72
Q

How do you create a histogram in python?

A

plt.hist()

73
Q

What is the default bins parameter value?

A

10

74
Q

How do you change the outline of a data point?

A

ec = ‘character’

75
Q

What does numpy.linspace(a,b,c) do?

A

create a range for an axis on a graph, c is the step

76
Q

What does numpy.arange(d,e,f) do?

A

create a range for an axis on a graph, f is the step, d and e are not included in the range, unlike in numpy.linspace()

77
Q

How do you create a legend?

A

plt.plot(x, y, label = ‘graph 1’)
plt.plot(x, y, label = ‘graph 2’)
plt.legend(loc = ‘’)

78
Q

How do you position a legend?

A

plt.legend(loc = ‘location’)

79
Q

What does subplot() do?

A

Allow for multiple graphs to be shown in a figure

80
Q

How do you make two graphs in a figure?

A

plt.figure()

pl.subplot(211)
plt.plot()

plt.subplot(212)
plot.plot()

plt.show()

81
Q

How do you add a grid to the graph?

A

plt.grid(True)

82
Q

How do you remove the steps on an axis?

A

ax = subplot()
ax.plot(…)
ax.set_xticklabels([])

83
Q

When setting a subplot = ax/some variable, how do you access the normal functions that could be used by plt?
For example plt.ylabel = ax.?

A

Put ‘set_’ in front of the function:
ax.set_ylabel(‘…..’)

84
Q

How do you create a list using linspace or arange?

A

import numpy as np
a = list(np.linspace(a,b,c))
- create a list between a,b with c number of values
- values are decimals
b = list(np.arange(a,b,c))
- create a list between a,b (not including b) with c as the step

85
Q

What happens when you divide an array by a constant?

A

All values are divided by the constant

86
Q

Can lists be divided by a constant?

A

No, produces an error

87
Q

Do the x and y axis have to have the same number of values in the list for a plot?

A

yes

88
Q

How do you dot multiply two matricies?

A

np.dot(m1,m2)
or
answer = m1@m2

89
Q

What does .strip() do?

A

get rid of \n and white space from either side of a string
.lstrip() does from the right
.rstrip() does from the left

90
Q

What does separator.join(iterator) do?

A

iterates through each index of a string, list, or tuple and joins the elements together, divided by the separator specified

91
Q

How do you prevent additional blank lines between writing lines?

A

open(‘file’, ‘designator’, newline = ‘’)

92
Q

How do you write a row to a csv file?

A

reader = csv.reader(file)
row = [‘val’,’val’,’val’]
reader.writerow(row)