The good news is that we're not going to do much with that math, and past this we'll mostly be working with other concepts. So if things like sin() and exp() make no sense, just ignore them.
All information in a computer is stored as a long series of 1's and 0's. We call those "bits".
Different sorts of information (text, sounds, images, integers, decimal numbers, ...) are have different internal representations.
In python this is called the "type" of the data, and there's a function that you can use to find out about this type.
For example :
>>> a = 3 >>> type(a) <class int> >>> b = 3.0 >>> type(b) <class float>
Integers are the counting numbers -2, -1, 0, 1, 2, 3, ...
Floats are numbers with a decimal point, which are stored as a fractional part and an exponent, as is done with scientific notation.
However, these internal representations are often rounded-off approximations to the value that you had in mind.
>>> 10/3 3.3333333333333335 >>> 0.1 + 0.2 0.30000000000000004
The ...333335 and ...00004 are due to the approximate representation in the computer of the rational number 10/3 , which as a decimal would need an infinite number of digits to store exactly. Python only has a finite number of digits for its 'floating' type.
(Actually, a float is stored as two numbers, in scientific notation. If xxxxx and yyyyy are integers, then the floating point number is 0.xxxxx * 10**yyyyy . And both of these are stored in binary - base 2.)
Integers in python, on the other hand, can be as big as the computer memory can handle in python. And that's pretty big.
>>> 2**10, 2**20, 2**30, 2**40, 2**300 (1024, 1048576, 1073741824, 1099511627776, 2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397376)
Most other languages have integers which can only get so big before running out of space. For example, in C an unsigned 16 bit integer can be at most 2**16 - 1 = 65535.
(This is 'culture' ... computer background that isn't in the text.)
Here are a few places to read about the 1's and 0's" :
Computer humor :
# In python (and many programming languages) you can type binary and hex numbers with a 0b or 0x prefix. >>> 0b10 2 >>> 0xDEAD 57005
These hexadecimal numbers show up in a lot computer nomenclature, for example
And coming in a few weeks when we discuss strings : ASCII and UTF8.
First let's talk a bit about names, namespaces, and dir()
>>> dir() # what names are defined ? ['__builtins__', '__doc__', '__name__'] >>> dir(__builtins__) # ... long list of built-in things ... >>> from math import * >>> dir() # ... another long list of math-y things.
The math functions like sin(), cos(), sqrt(), aren't available in python by default. Instead, you must "import" them from a "module" called "math".
The book does this instead
>>> import math >>> math.sqrt(3.0)
which leaves things from the math module with "math." before their names. If you do things that way, sin(pi/2) is math.sin(math.pi/2).
You can think of math.sqrt as a "sqrt" name "inside" the "math" thing.
>>> import math >>> dir(math) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
This is actually the beginnings of a topic known as "object oriented programming", which we will dive into soon. Here "math" is the object, and "math.sqrt" is a function within that object.
Be clear about the difference between that and this.
>>> from math import * >>> dir() ['__annotations__', '__builtins__', '__cached__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'os', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'sys', 'tan', 'tanh', 'tau', 'trunc']
Coding languages also have their own conventions as to how to type various math operations. Here are a few of Python's.
2 + 3 # This one is obvious. 2 * 3 # multiplication 2 ** 3 # "raise to the power of" 2 / 3 # "divide" ... and get a float 2 // 3 # divide ... and get the integer part
Here are the types of python values we've seen so far (today and earlier).
integer 1278 float 3.23 string "This is a string" list [3, 5, 17, "red", 0.01] module math (i.e. "import math") built-in function math.sin, math.exp (after "import math")
We'll learn more about these and see a few more as we go along.
total = 0 numbers = [1, 10, 20, 18, 17, 34, 22] for number in numbers: total = total + number print("The sum of ", numbers, " is ", total)
Look at this carefully to understand what's going on.
The text doesn't go into the details of lists until much later - mainly because they have a lot of bells and whistles - but for now let me just say that python lets you collect up things into a sequence, and that these loops happen over such a sequence.
>>> list(range(3)) [0, 1, 2] >>> type([0, 1, 2]) <type 'list'> >>> for i in [5, 17, 4]: ... print(i) 5 17 4
This "accumulator pattern" shows up a lot: we start with a variable which is "empty", and then in a loop "fill it up" with something, accumulating the result.
I will sometimes add related topics at the end of my notes ... more culture. ;)