Official website: www.python.org
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 eN
That is my quick notes for now. I want to post some programs using the Python language in the upcoming weeks.
Eddie
A blog is that is all about mathematics and calculators, two of my passions in life.
Monday, July 21, 2014
My Mathematical Notes for Python (so far)
Subscribe to:
Post Comments (Atom)
HP 42S and TI60: Dimensions of a Race Track
HP 42S and TI60: Dimensions of a Race Track We have a race track that consists of two rectangular tracks connected by a circular ri...

Casio fx991EX Classwiz Review Casio FX991EX The next incarnation of the fx991 line of Casio calculators is the fx991 EX. ...

HP Prime: Basic CAS Commands for Polynomials and Rational Expressions Define the following variables: poly: a polynomial o...

The Odds of Hitting it Big The number of possible combinations is fairly easy to calculate. You multiply the number symbols each slot has ...
No comments:
Post a Comment