MATH 356-01
Information on Python
**Last updated 10/5/18 (Subfolders may have been updated more recently.)
Anaconda installation files here. This link takes you to the Windows files; there are tabs at the top for other operating systems.
Anaconda is a platform for launching several different things; when you run Anaconda, you will see several buttons. The one we want is Jupyter Notebook -- that will start a Python session for you in a web browser.
Python is a programming language. Fortunately for us, we don't have to begin the programming from scratch; there are a number of useful commands built in, and there are also a lot of command libraries that others have written (and we can use!).
Two major packages of commands are numpy and sympy. These can be loaded via "from numpy import *" and "from sympy import *" respectively (without the quotes). Most of the commands we will need are in these two packages, although the "math" package will also be useful. There are other ways to go about this, as well, and there are some subtleties that may crop up from time to time, but this is a good start.
Rather than attempt a Python tutorial here, I will offer some of the basic things that we will find useful so that you can search for how to implement them.
- To execute a command, use SHIFT+ENTER. ENTER alone will give you another line in the same command window (also useful).
- Loops look like "for i in range(100):" (for example). Note that in Python, range(100) refers to the integers 0, 1, 2, ..., 99 (not 1 to 100).
- Python requires that you declare variables. Use "symbols" for this; e.g., x,y=symbols("x,y")
- The percent sign % is used to reduce a number modulo some modulus: 25%3 will return 1, or 25 mod 3.
- You can use "factor" to factor a polynomial if you have the sympy package loaded.
- Complex numbers are indicated as a+bj -- no * to indicate multiplication by j (which Python uses for i), just the coefficient next to a j, like 4+5j.
- Exponentiation is performed with ** instead of ^ -- watch out for that!
- You can solve equations with the solve command: solve((x+y-2,x-y-4),(x,y)) (for example). Python's solve command assumes that the equation is of the form your_expression=0 -- note that neither "equation" in the command has an = in it. Also, be sure to declare your variables first.
- You can also simplify expressions with the simplify command: simplify(expression) (replacing "expression" with the thing you want to simplify). This requires sympy (and a declaration of variables).
- The math package includes a gcd command. "import math" will load the package, and then you can access the gcd command via "math.gcd(a,b)" (where a and b are, of course, the number you're interested in). Note the . notation -- we will see more of that. It may take some getting used to...
- Sometimes Python just does what you want but doesn't report out. If you want to be sure to see the output, you have to print it: print(whatever).
That's it for now. I will try to add to this as we encounter more things we need.
Classroom Demos
Python Worksheets