3.1.1. Python Quick Reference¶
Python is a high-level open-source language. This page is written with the use of Python in mind, as opposed to computer science definitions. iPython is used here because:
print statement can be avoided (you can just type the variable and get output)
the output is automatically computed by the iPython interpreter (no need for testoutput blocks)
it allows reference to variables beyond the code block
3.1.1.1. Modules¶
3.1.1.1.1. Definition¶
Modules provide useful functions, such as array operations, plotting and much more.
We can import the module to allow access to the functions
In [1]: # comments in python are denoted by the hash tag
In [2]: import numpy as np # for matrix operations
In [3]: import matplotlib.pyplot as plt # for 2D plotting
We have defined the following aliases:
Module
Alias
Purpose
numpy
np
Matrix Operations
matplotlib.pyplot
plt
2D Plotting
3.1.1.1.2. Use¶
This allows reference to the modules via the alias, e.g.
In [4]: myarray = np.linspace(0,5,10)
In [5]: myarray
Out[5]:
array([0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,
2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])
If you don’t preface the linspace function with np python will throw an error:
To learn the new functions available to you, visit: Numpy Reference
Or for Matlab users: Numpy for Matlab Users
3.1.1.2. Variables¶
3.1.1.2.1. Definition¶
Python doesn’t require explicitly declared variable types like C and Fortran.
In [6]: a = 5 # a is an integer 5
In [7]: b = 'five' # b is a string of the word 'five'
In [8]: c = 5.0 # c is a floating point 5
3.1.1.2.2. Use¶
Use type to determine the type of variable:
In [9]: type(a)
Out[9]: int
In [10]: type(b)