If you are following this course and do not know how to obtain the above requirements, see Setup Instructions.
__init__
, self
print(Hello world!)
Lists are:
Tuples are:
Numpy Arrays are not included by default.
To use numpy array, you need to include the following line of code:
import numpy as np
For a tutorial on modules, visit: https://docs.python.org/3/tutorial/modules.html
Numpy arrays are are:
NOT able to have mixed data types - numpy arrays are homogeneous and contain elements of the same dtype.
dtypes are data type objects used within numpy. There are built in dtypes representing
- np.int8, np.int16, np.int32, np.uint8, np.float32, np.bool_, np.complex_, etc...
Also you can create a dtype to suit your needs, e.g. structures of fields of the built in dtypes. For more information, see the documentation: https://docs.scipy.org/doc/numpy-1.12.0/reference/arrays.dtypes.html
See user guide: https://docs.scipy.org/doc/numpy-dev/user/
See other documentation: https://docs.scipy.org/doc/
If you're a MATLAB user, you may find this page useful, which sets out the difference between MATLAB and NumPy syntax:
https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html
There are many mathematical functions that can be applied to arrays which come built into the numpy package.
A list of mathematical functions available within numpy is found here: https://docs.scipy.org/doc/numpy-1.12.0/reference/routines.math.html
Numpy arrays are also useful for working with matrices and other 2d arrays and displaying these.
Numpy can also be used to create numpy arrays from data in a file using np.loadtxt(). The file must contain data in rows with equal numbers of entries.
Dictionaries are:
def
followed by function name and parameters in a parenthesisreturn output_var(s)
ends the functionThe most common plotting module is matplotlib.
It's simple to use but can be also be used to produce publication quality plots.
It can be used with LaTex formatting.
The best way to understand is to see some examples:
For a tutorial on matplotlib.pyplot, try: https://matplotlib.org/users/pyplot_tutorial.html
This has examples of line graphs, scatter graphs and histograms.
For more details and examples, visit: https://matplotlib.org/contents.html
The function open() can be used to read from, write to or append a file. It creates a file object
It takes two arguments, the filename and the mode indicating what you want to do with the file:
Once a file object is created, you can then use many functions including the following:
For a tutorial on input and output, visit: https://docs.python.org/3/tutorial/inputoutput.html
So far we have imported numpy and matplotlib. In python there is lots of pre-written code to take advantage of, usually by importing modules.
Other common modules you may come across are :
- math: for common maths functions and numerical constants
- scipy: a library of algorithms and mathematical tools.
For example there are built in algorithms for minimising/maximising functions
For a tutorial on modules, visit: https://docs.python.org/3/tutorial/modules.html