Python








Introduction

One alternative to MATLAB, Mathematica and Maple is to use SageMath. SageMath is build on open source programming language Python. There is a cloud based system cloud.sagemath.com [[https://cocalc.com ]]. In this course, we describe the use of 3 lightweight Python libraries:

  1. [Sympy]- Symbolic mathematics with Sympy.ipynb) for carrying out symbolic mathematical calculations;
  2. [Numpy]- Linear algebra with Numpy.ipynb) for dealing with large numeric arrays: perfect for linear algebra;
  3. [Pandas]- Data analysis with Pandas.ipynb) a brilliant tool for data analysis.
Python and the above libraries are all free to use (and potentially change if you needed to!).


Variable

  
# Anything after a `#` will be ignored. This is what we call a 'comment'
a = 20  # Assigning a value to a
b = 21  # Assigning a value to b
a * b  # Calculating the product of a and b
a / (a + b)  # Calculating a / (a + b)
a ** b  # Calculating a raised to the power of b



Function

  
def proportion(a, b):
    """
    We can use triple " to describe what our function does.
    
    Here for example: we're creating a function to calculate 
    the proportion of a of a + b
    """
    return a / (a + b)
proportion?
proportion(3, 1)
 



Function

Repeating things with for loops
We can use a for loop to repeat bits of code with Python.
For example the following will calculate:
\[ \displaystyle \sum_{i=1}^9 i \]
  
total = 0
for i in range(10):
    total = total + i
total
 

No comments:

Post a Comment