# Python Resources

## General

This website has a great set of cheat sheets for general Python knowledge.

Numpy also has a great Numpy for Matlab users section which compares common MATLAB commands to their equivalent expressions in Numpy.

Matplotlib has a tutorial on their `pyplot`

plotting system here. Their syntax is generally the same as MATLAB's plotting syntax. but you prepend `plt.`

to most commands. For example, a basic matplotlib plot looks like:

import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4]) plt.ylabel('some numbers') plt.show()

Last but not least, the official Python docs should be your go-to for Python questions if the cheatsheets above aren't sufficient.

## DSP

Scipy's signal processing library documentation can be found here.

## A brief cheatsheet

### Conditionals

a = 5 b = 5 c = 8 if a == b or b != c: # ...

### Loops

for i in range(10): # i = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 for i in range(5, 10): # i = 5, 6, 7, 8, 9 j = 10 while j >= 0: # ... j -= 1 while True: # ...

### Python Arrays

a = [1, 2, 3, 4, 5] # a[0] == 1 # a[1] == 2 # ... # a[-1] == a[N - 1] == 5 # a[-2] == a[N - 2] == 4 b = [] b.append(6) b.append(7) b.append(8) # len(b) == 3 # b[0] == 6

### Function definitions

def yourNewFunction(requiredArg1, requiredArg2, optionalArg3=defaultValue): # ... return 0

### Numpy arrays

import numpy as np # Numpy arrays are quicker than python arrays (actually called lists) # because they are preallocated. This means that a.append() does # not work for numpy arrays. a = np.array([1, 2, 3, 4, 5]) # array access is the same # a[0] == 1 # ... b = np.zeros(5) # b.shape == (5, ) # This is a numpy vector of length 5 c = np.ones(5, 2) # c.shape == (5, 2) # This is a numpy array with 5 rows and two columns

### Plotting

import matplotlib.pyplot as plt t = np.array([i for i in range(100)]) x = np.cos(t) plt.figure() plt.plot(t, x) plt.xlabel('index') plt.ylabel('x') plt.title('A random plot') plt.show()

### File reading/writing

import numpy as np # For CSV read specifically filename = 'csv_sample.csv' data = np.genfromtxt(filename, delimiter=',') # For general file read with open(filename, 'r') as f: data = f.readlines() # For general file write with open(filename, 'w') as f: f.write('Hello world!') # Using "with" automatically handles opening/closing file descriptors