Chapter 12 Flashcards
What is a docstring?
A comment at the start of every function
What is a package?
A group of modules
How do you access a function from a module?
module_name.function_name(arguments)
How do you import a specific function instead of a whole module?
from <module_name> import <function_name></function_name></module_name>
How do you rename a module
import <module_name> as <new_name>
e.g.
- from math import sqrt as sr</new_name></module_name>
What is a vector?
An array of elements of one direction,
- can be up or down
What are he two types of vectors?
- Row
- Column
Can you add vectors or matrices together?
If they have the same dimensions yes
What does multiplying a matrix by a scalar constant do?
Multiplies all values in a matrix by the scalar value
How does matrix subtraction work?
Parallel values subtract,
- 2nd value is subtracted from 2nd value
What is the noation for a column vector?
[n,0]
N rows, one column
What is notation for a row vector?
[0,n]
one down, n wide
What is a diagonal matrix?
A square matrix where all values = 0 besides the diagonal values where m = n
a_mn
What is a triangular matrix
A matric where one side of the square matrix is 0 and the rest has values, split diagonally into a triangle
What is a tridiagonal matrix?
When the square matrix has 3 diagonals with values, the rest is zero
What is a transpose matrix?
When a matrix (m x n) changes to (n x m)
What happens when you transpose a row vector?
You get a column vector because m and n are getting swapped
Is matrix division possbile?
Yes
A / B = A / B^-1
I think check this out
Does matrix A x matrix B = matrix B x matrix A?
No
How do you divide two matrixes?
What is a module?
A file containing code that can be imported and used in other modules or scripts
When importing a module how do you access variables within that module?
dot notation,
module_name.varibale
e.g
names.py:
first = ‘Larry’
last = ‘David’
write_names.py:
import names
print(names.first, names.last)
What is a script?
A file containing python code that is passed as an input to the interpreter
What is the console used for?
Coding line by line and receiving an output each time,
- typically used for very short codes or to test syntax
What is dot notation?
module.object
What does python use to determine whether the program is executed as a script or as an imported module?
execute code as script
if __name__ == ‘__main__’:
#indented code
# run as script
What happens when a module is imported and called for in a script?
The modules code is run
What file extension must a module have in order to be imported?
.py
Are module imports case sensitive?
Yes
What is it called when a program requires a module to run?
dependency
What happens when you import a module into python?
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
What is a module object?
A namespace that contains definitions from a module
How do you change the value of an object in a module?
module.object() = value
What happens after you change the value of an object in a module?
the module is reloaded with new values, though when another program imports the module, all objects are back to normal
When importing a module, the interpreter first looks for____?
An built-in module, if there is no match, then python looks in sys.path
What are the three initial directories that are contained in sys.path?
- 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 do you import an object from a module?
from module import object1,object2….
This way you don’t have to us dot notation and it saves memory
What directory must a package include in order for its contents to be accessed?
__init__.py
How do you import a module from a package?
from <package> import <module></module></package>
How do you import an object inside a module inside a package?
from <package.module> import <object></object></package.module>
What is the difference between import y.z and from x.y import z
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
What modules are built-in to python?
- math
- os
- sys
- copy
- time
- pdb
- urllib
- random
- datetime
What function in matplotlib displays the graph?
.show()
What function in matplotlib plots the graph?
.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])
What characters represent each variation of graph colour?
b =blue
r = red
g = green
y = yellow
c = cyan
m = magenta
w = white
k = black
What characters represent each line variation for matplotlib graphs?
- = dashed line
– = solid line
-. = dashed dotted line
: = dotted line
What characters represent each variation of point marker of matplotlib graphs?
. = 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
What characters represent each variation of point marker of matplotlib graphs (not polygons)?
. = 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
What are polygon markers for matplotlib?
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
What does the alpha property do for a graph?
enables transparency
- float
What does the antialiased property do for a graph?
enable anti-aliasing of the line
- boolean
What does the color property do for a graph?
changes colour of the line
What does the solid_capstyle property do for a graph?
How the cap of a line appears
What does the solid_joinstyle property do for a graph?
How the join of a line appears
What does the data property do for a graph?
The arrays of x and y coordinates
What does the label property do for a graph?
The label to use for the line
What does the linestyle property do for a graph?
The style of the line
What does the linewidth property do for a graph?
The width of the line when drawn
What does the marker property do for a graph?
The style of marker to use
What does the markersize property do for a graph?
The size of the marker
What does the visible property do for a graph?
Show/hide the line
what does .legend() do for a graph?
Create a key/legend
What does the text(x,y, string) function do?
create text to put on the graph
How is an array full of zeros declared?
import numpy as np
np.zeros((rows,columns))
How do you declare a 1d array?
import numpy as np
np.array([1,2,3])
what does array.linspace(num1,num2,total_nums) do?
take two numbers and create a list of values equally spaced between them, creating a total list of total_num values
What does the figure() function do?
Create multiple windows each with their own graphic plots using matplotlib
What does matplotlib.xlabel do(x/y)?
label the axis of a graph
How do you declare the axis of a plot?
plt.axis(x_min,x_max,y_min,y_max)
What does plt.subplot() do?
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
How do you create multiple axis?
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(‘’)
How do you create a histogram in python?
plt.hist()
What is the default bins parameter value?
10
How do you change the outline of a data point?
ec = ‘character’
What does numpy.linspace(a,b,c) do?
create a range for an axis on a graph, c is the step
What does numpy.arange(d,e,f) do?
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()
How do you create a legend?
plt.plot(x, y, label = ‘graph 1’)
plt.plot(x, y, label = ‘graph 2’)
plt.legend(loc = ‘’)
How do you position a legend?
plt.legend(loc = ‘location’)
What does subplot() do?
Allow for multiple graphs to be shown in a figure
How do you make two graphs in a figure?
plt.figure()
pl.subplot(211)
plt.plot()
plt.subplot(212)
plot.plot()
plt.show()
How do you add a grid to the graph?
plt.grid(True)
How do you remove the steps on an axis?
ax = subplot()
ax.plot(…)
ax.set_xticklabels([])
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.?
Put ‘set_’ in front of the function:
ax.set_ylabel(‘…..’)
How do you create a list using linspace or arange?
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
What happens when you divide an array by a constant?
All values are divided by the constant
Can lists be divided by a constant?
No, produces an error
Do the x and y axis have to have the same number of values in the list for a plot?
yes
How do you dot multiply two matricies?
np.dot(m1,m2)
or
answer = m1@m2
What does .strip() do?
get rid of \n and white space from either side of a string
.lstrip() does from the right
.rstrip() does from the left
What does separator.join(iterator) do?
iterates through each index of a string, list, or tuple and joins the elements together, divided by the separator specified
How do you prevent additional blank lines between writing lines?
open(‘file’, ‘designator’, newline = ‘’)
How do you write a row to a csv file?
reader = csv.reader(file)
row = [‘val’,’val’,’val’]
reader.writerow(row)