12. matplotlib-basics
·
Python
We want matplotlib to work inline in the notebook. In [1]: %matplotlib inline In [ ]: import matplotlib.pyplot as plt import numpy as np In [10]: import matplotlib.pyplot as plt import numpy as np from scipy import linalg def markov_chain(n,N): # I use np.array instead of List to utilize the numpy # Construct a random n-vector with non-negative entries and scale its entries so that the sum is 1...
11. sympy-examples
·
Python
Symbolic Computation with SymPy¶ This Notebook closely follows the SymPy tutorial. In [2]: import sympy as sym import math Basic Symbolic Manipulation¶ In [3]: print(math.sqrt(2)**2) 2.0000000000000004 In [4]: print(sym.sqrt(2)**2) 2 In [5]: print(sym.sqrt(8)**2) 8 We can do symbolic math not just on numbers, but we can tell SymPy what to treat as a variable. In [6]: x, y, z = sym.symbols("x y z..
10. NumPy tutorial
·
Python
This notebook is based on the SciPy NumPy tutorial. Array Creation and Properties¶ Here we create an array using arange and then change its shape to be 3 rows and 5 columns. In [10]: a = np.arange(15, dtype=np.float32).reshape(3, 5) print(a.dtype) a float32 Out[10]: array([[ 0., 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [10., 11., 12., 13., 14.]], dtype=float32) In [7]: np.arange? Docstring: arang..
9. Regular Expression
·
Python
Regular Expression¶ This notebook closely follows the Python Course on Google for Education. In Python a regular expression search is typically written as: match = re.search(pat, str) The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise. Therefore, the..
8. Text File I/O
·
Python
Text File I/O¶ Print Method¶ The print() method can be used to print objects. It adds a space between its arguments and a newline at the end. In [1]: x = 1 y = 0.0000354 z = 3.0 s = "my string\n" print(x, y) print(z, s) 1 3.54e-05 3.0 my string To print internal representation of objects, use the repr() method. In [2]: print(repr(x), repr(y)) print(repr(z), repr(s)) 1 3.54e-05 3.0 'my string\n' ..
7. Lambda Functions
·
Python
Lambda Functions¶ Lambda functions are "disposable" nameless functions. They are often used as arguments in other functions. For example: In [1]: pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] pairs.sort(key = lambda p : p[1]) print(pairs) [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')] See its visualization here. T..
6. Recursion vs. Iteration
·
Python
Recursion vs. Iteration¶ A recursive function is concptually clean. In [1]: def factorial(n): if n < 2: return 1 return n * factorial(n - 1) print(factorial(5)) 120 However, it is relatively expensive because of the creation and manipulation of stack frames. You can visualize its execution here. For better efficiency, we can convert the function to use iteration instead Approach 1: Brute Force U..
5. Function
·
Python
Functions¶ In [12]: def my_fun1(i): print("in the function, here&#39;s i:" , i) return my_fun1(10) my_fun1(5) in the function, here&#39;s i: 10 in the function, here&#39;s i: 5 Arguments¶ Note: Python passes mutable objects by reference, similar to assignment. In [4]: def my_fun2(i, A, B): i = 5 A[0] = 5 B = {4, 5, 6} i = 1 A = [1, 2, 3] B = {1, 2, 3} my_fun2(i, A, B) print(&#39;i={}; A={}; B={}..
4. Conditionals and Loops
·
Python
Control Flow¶ Like any other programming languanges, Python support conditionals and loops. Conditionals¶ If Statement¶ Python uses if-elif-else. Conditions are boolean expressions. Notice about ':' and indentation. See visualization of this example. In [1]: x = 10 if x range object | | Return an object that produces a sequence of integers from start (inclusive) | to stop (exclusive) by step. ra..
3. Exception Handling
·
Python
Exception Handling¶ Exceptions are mechanism for handling runtime errors. A simple example: In [2]: dict = {"a":1, "b":2, "c":3} print(dict["d"]) --------------------------------------------------------------------------- KeyError Traceback (most recent call last) in 1 dict = {"a":1, "b":2, "c":3} ----> 2 print(dict["d"]) KeyError: &#39;d&#39; KeyError is the exception that was raised. We can ch..