Comp1012 (2015)
Comp1012 (2015)
COMP
1012
Section
A02
Computer
Programming
for
Scientists
and
Engineers
REVIEW
2015
January
to
April
3 4
Terminology Terminology
n Literal
n Variable
n Key
word
Full
list?
n “hello
world”
n D_
=
3.4
n Import
n 2
n statement
=
“hello
n while,
for
import
keyword
n 5.678
world”
n if,
elif,
else
keyword.kwlist
n 3.141592653
n math.pi
n print
n ‘y’
n myTuple
=
(3.4,2.5,1.2)
n def
You
can’t
use
any
of
these
n True
n mixedTuple
=
n
and,
or,
in,
is,
as
as
variable
or
function
(math.pi,math.e,state names!
n assert
ment)
n try,
except
1
5
6
-‐
In
python
you
don’t
have
to
declare
the
type
it
You
can
also
use
math…
automatically
happens…
but
if
you
WANT
a
specific
type
you
need
to
cast
it
to
that
type.
There
are
built
in
functions
for
this…
We
can
have
more
control
with
the
string
by
formatting
the
number…
7 8
“%r”%x Creates a string containing a representation of the value of x as “%0f”%x Includes a leading 0 before the
it is stored in memory… as close as possible. number
“%10r”%x The same as above, but only what fits into 10 character spaces! “%.9f”%x Shows x as a floating point number
with 9 decimal places
“%s”%x Creates a string containing the value of x. If x is a floating point “%-10f”%x Left justifies the ‘field’ tha the
this may be less precise. numberis placed in.
“%d”%x Creates a string containing an integer representation of the value “%+10f”%x Shows a plus sign for positive
of x. (python site says you can also use ‘i’) numbers a negative sign for negative
numbers within the 10 character
“%f”%x Creates a string containing a floating point representation of the spaces allotted for the field.
value of x. If x is an integer and you do not say how many
decimal spaces to print? Testing says 6! “%10.6”%x Assigns 10 character spaces for the
field and shows 6 decimal spaces for
“%e”%x Scientific notation. Use this when you have super small, or x. Is right justified. If the number is
super large numbers. Floating point exponential format. Here TOO big for 10 space, it will go over.
also you would want to say how many decimal places to show.
2
9
49
n Then
you’ve
got
the
built
in
function
Unary
sign
+
len,
–
2**4
complex…
Division,
Multiplication,
7
/
3,
7
//
3,
7
%
3,
7
*
3
remainder
Addition,
Subtraction
12
+
2,
6
-‐
2.1
this functionality when
Use
you have formulas that Relational
<
<=
>
>=
!=
==,
in,
is
include the imaginary
number the root of -1
Logical/Boolean
not
and
or
11 12
3
13
74
15
16
Infinite Sums
– a particular kind of loop Tuples and Lists
n Sum
n Store
multiple
values
of
any
type
n Term
n Tuples
Are
immutable,
lists
can
change
in
size
and
n Count
(sometimes)
content
n Calculation
inside
the
loop
calculates
a
term
and
adds
n Allow
for
passing
multiple
values
back
from
a
it
to
a
sum
single
function
n Watch
for
some
thing
about
the
term
that
is
fractal
like…
linking
it
to
the
last
term.
How
are
the
terms
n Tuple
…(
)
and
list…
[]
related?
Can
you
calculate
the
new
term
from
the
old
n A
list
and
a
tuple
may
contain
the
same
values
but
term?
they
would
not
be
considered
equal
in
a
boolean
n When
does
the
loop
stop?
The
new
term
==
the
old
expression
term?
The
new
term
is
so
small
?
4
119
121
121 121
Slices Slices
n nums
=
range(10)
n nums
=
[14,
32,
2,
6,
2,
18,
9,
5,
73,
28]
n nums
à
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9]
n nums
à
[14,
32,
2,
6,
2,
18,
9,
5,
73,
28]
n nums[0]
à
0
n nums[0]
à
14
n nums[-‐1]
à
9
#
same
as
length
-‐
1
n nums[-‐1]
à
28
#
same
as
length
-‐
1
n nums[2:5]
à
[2,
3,
4]
n nums[2:5]
à
[2,
6,
2]
n nums[7:-‐1]
à
[7,
8]
n nums[7:-‐1]
à
[5,
73]
n nums[7:
]
à
[7,
8,
9]
n nums[7:
]
à
[5,
73,
28]
n nums[
:
4]
à
[0,
1,
2,
3]
n nums[
:
4]
à
[14,
32,
2,
6]
n nums[
:
]
à
[0,
1,
2,
3,
4,
5,
6,
7,
8,
9]
n nums[
:
]
à
[14,
32,
2,
6,
2,
18,
9,
5,
73,
28]
n nums[2:1]
à
[
]
n nums[2:1]
à
[
]
5
21
22
23 24
6
25
26
2. What
are
the
output
types?
2. Any
constant
values
required?
3. Calculate
the
output
using
formula
or
algorithm
4. Test
the
result
and
fix
errors
n If
you
are
hand
coding,
mentally
run
through
your
code
as
if
you
were
the
computer.
Make
sure
you’ve
identiGied
what
you
are
returning
and
include
n Try
various
input
values.
that
as
soon
as
you
deGine
your
function.
If
the
function
is
5. Make
changes
as
necessary
and
repeat
printing
out
and
not
returning
identify
that
in
comments.
27 28
7
29
30
31 173
8
33
34
35 36
9
37
38
cos( ) = + + ... n Review
the
various
incarnations
of
infinite
sums
! ! ! ! n Understand
how
these
can
be
efficiently
coded
n Consider
how
each
term
builds
of
the
last
term
n the
sum
is
simply
sum
+
term
for
however
many
terms
–
same
for
each
n But
the
term
calculation
is
different
for
each
series…
practice
on
a
few
series…
you
can
even
look
up
some
series
online
to
get
practice
–
compare
your
answer
to
math.cos,
math.sin,
math.arcsin
etc
…
Topics 3 40
Week
Topic
Event
Numpy Arrays
0
Introduction
A1
posted
1
Basics:
Expressions
and
Scripts
Weekly
labs
start
n Arrays
allow
for
vectorization…
doing
operations
2
while
Loops
and
Infinite
Sums
on
large
sets
of
data
all
at
once
(or
at
least
3
Lists
and
Tuples
A2
posted
4
for
Loops
A1
due
seeming
to
us)
5
Functions
and
if
…
else
n What
is
different
when
you
are
calculating
for
a
6
User
interactions
A2
due,
A3
posted
scalar
vs
calculating
for
an
array?
7
Review
8
Mid
Term
&
Start
Arrays
Mid
Term
Wed
Oct
30
n Calculating
a
list
vs
calculating
for
an
array?
9
Arrays
A3
due,
A4
posted
10
Curve
plotting
VW
deadline
n If
you
want
to
do
vector
arithmetic
you
cannot
use
11
Pseudorandom
numbers
math
library
you
must
use
numpy
library
12
String
processing
A4
due
Final Exam
Dec
12
9AM
10
41
42
We can switch between lists and
Lists vs numpy arrays arrays
List Array
Similarities
Uses slice notation [a : b : c] uses slice notation [a : b : c]
Apply functions len, .sort Apply functions len, .sort
Apply operators is, in Apply operators is, in
Mutible, can change contents Mutable, can change contents
a, b, c, d, e = range 5 a, b, c, d, e = np. arange(5)
Differences
Any collection of types All entries must be the same type
+ concatenates; * repeates +, *, / … operate term by term
Functions append, insert, remove Size is fixed (see np, repeat, np.resize())
Generate with range, list comprehension, Generate with zeros, arrange, linspace, + numerical
append, + * operations
y = x[a:b] # a copy of the list data y = x[a:b] is the same array data
Use math library functions Use numpy library functions
43 44
11
45
46
47 48
12
49
50
51 52
plot
to
see
the
probability
of
each.
Thus
we
take
random.seed(12345)
np.random.seed(12345)
advantage
of
computers
to
simulate
stochastic,
#the
code
will
always
produce
the
same
#note
that
you
could
pass
in
something
13
53
54
55 56
n Put
file
to
be
read
in
the
same
folder
as
the
python
n Now
we
just
need
the
file
name
and
we
can
open
script
file
and
read
the
file
quite
simply…
n Import
items
to
help
find
the
file
14
256
58
15
61
62
63 64
16