Monday, July 21, 2014

My Mathematical Notes for Python (so far)

Official website:

Latest version: 3.4 (at the time of this blog entry)

My mathematical notes on Python so far:

This is from the 3.4 instructions but should hold in the 2.7 version as well. I am trying to find a good iOS app to do this.

* A good suggestion is to watch a few tutorial YouTube videos on Python. Onestopprogramming has a good set of tutorials. Python is capable of doing many things, including working with computer files, my focus will be on some of the mathematical capabilities of Python.

* The uses of the equals symbol ( = ) in Python:

(1) A single equal sign means assignment.
x = 4 stores "4" in the variable x.
str = "Eddie" or str = 'Eddie' stores "Eddie" in the variable str.

(2) Two equals signs mean comparison, does x equal y? ( x = y ?)
12==12 returns True

(3) An exclamation mark in front of an equals sign means the comparison, does x not equal y? ( x ≠ y ?)
12!=12 returns False

* Variables that are assigned are defaulted to strings. In order to use variables to represent numbers, we must first declare them as such. The declarations are:

int: integer
long: long integer
float: floating numbers
complex: complex numbers

Example: float(x) declares x as a floating variable.

* Working with floating numbers can bring "weird results" due to Python internally representing every number as a binary representation (0s and 1s). For example,

2.2 * 2 returns 4.40000000000000004

Although the PC version returns 4.4. So this may just be he iOS or a not so good app I was using a the time (Python 3.3 iOS app).

In order to get this answer in more acceptable form. This requires the format declaration. It's syntax is generally this:

"{:.xf}".format(answer, expression, variable, etc..)

Where x is the number of decimal places desired.

Back to our example, let's express 2.2 * 2 using four decimal places:

"{:.4f}".format(2.2*2) returns '4.4000'

* To access mathematical functions beyond arithmetic, we must first import the math library. This is done usually at the beginning of each script (program).

Syntax: import math

Common math functions are what you would expect:

math.ceil(x): ceiling
math.fabs(x): absolute value
math.factorial(x): factorial of x, integers only
math.gamma(x): the gamma function, Γ(x)
math.erf(x): error function
math.fsum(list): sum of a lists elements (in single square brackets)
math.exp(x): e^x
math.log(x, base): logarithm. Leave base out for natural logarithms ( ln x ).
math.pow(x, y): x^y. More expansive than using the double asterisk. ( x ** y )
math.sqrt(x): √x

math.sin(x), math.cos(x), math,tan(x): sine, cosine, and tangent, respectively. The angle is always in radians.
math.asin(x), math.acos(x), math.atan(x): arcsine, arccosine, and arctangent, respectively. The angle returned is in radians.
There are hyperbolic versions of these functions.

math.degrees(x): convert from radians to degrees
math.radians(x): convert from degrees to radians
math.pi: π
math.e: e

A complete list can be found in the python documentary.

* Quick math symbols that don't need the math library to be imported:

Arithmetic functions: +, -, *, and /. (For those new to programming, * represents × and / represents ÷)

Power: ** (two asterisks)

Modoluo: %

Representing exponential powers of 10: e+N or e-N

That is my quick notes for now. I want to post some programs using the Python language in the upcoming weeks.


No comments:

Post a Comment

HP 15C: Pythagorean Triples

HP 15C:   Pythagorean Triples This program calculates the Pythagorean triple (A, B, C) such that A^2 + B^2 = C^2 by the formulas: ...