Questions about anything so far?
Please do consider the assignments to be ways to engage the material.
For some of the assignments - including next TuesdayNs - I will post my own code and output after the due date that you can compare with your own. (However, this means that late work will be worth less after I've posted answers.)
Coding is like weight lifting: you need to work at it to get better, eh?
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.
data types - discuss
float numbers are approximate - finite number of bits for storage.
>>> 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.)
"It's all just 1's and 0's" : discuss briefly how numbers are stored in computers.
"How many people know binary if only you and 10 people do?"
"How many people know hex if only you and dead people do?"
# In python (and many programming languages) you can type binary and hex numbers with a 0b or 0x prefix.
>>> 0b10
2
>>> 0xDEAD
57005
aside:
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.
sum = 0
numbers = [1, 10, 20, 18, 17, 34, 22]
for number in numbers:
sum = sum + number
print("The sum is ", sum)
Look at this carefully to understand what's going on. In class: run this. Then put in more print statements to see exactly what is going on during the loop. (The loop is the "for" statement and the indented part after it.)
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.
Write a program to calculate the mean of N numbers input by the user. First ask for N, the ask for the numbers one at a time. (Code from 2018 is attached which we can look after after we try ourselves.)
I've uploaded the one that we did in class.
An occasional subtopic in my notes, somewhat related to what we're doing ...
last modified | size | ||
average_2018.py | Tue Dec 03 2024 05:35 pm | 484B | |
average_2019.py | Tue Dec 03 2024 05:35 pm | 357B |