Today’s topics

20
CompSci 001 4.1 Today’s topics Notes from Zachary Dodds’ CS 5 course at Harvey Mudd Upcoming More Python Reading How to Think, 7.1-7.7 & 9.1-9.12

description

Today’s topics. Notes from Zachary Dodds’ CS 5 course at Harvey Mudd Upcoming More Python Reading How to Think, 7.1-7.7 & 9.1-9.12. Data and lists?. - PowerPoint PPT Presentation

Transcript of Today’s topics

CompSci 001 4.1

Today’s topics

Notes from Zachary Dodds’ CS 5 course at Harvey Mudd

Upcoming More Python

ReadingHow to Think, 7.1-7.7 & 9.1-9.12

CompSci 001 4.2

Can all information be represented using lists ?

Networks

Images/Video

Sounds/Speech

{ 2, 3, 5, 7, 11 }

Data and lists?

‘Many years later, as he faced the firing squad, Colonel Aureliano Buendia was to

remember that distant afternoon when his father took him to discover ice.’

Text

Sets

Ideas?

CompSci 001 4.3

Inside the machine…

x = 41

y = x + 1

name: xtype: intLOC: 300

41

What is happening behind the scenes:

What's happening in python:

"variables as containers"

memory location 300

id, delComputation Data Storage

name: ytype: intLOC: 304

42

memory location 304

CompSci 001 4.4

Python Data Types

bool

int

long

float 3.14

10**100

42

TrueFalse

Numeric

Name Example What is it?

values with a fractional part

integers > 2147483647

integers <= 2147483647

the results from a comparison:

==, !=, <, >, <=, >= "Boolean value"

CompSci 001 4.5

Datatypes as genes…

bool

Dominant

int

long

float

Recessive

41 + True

10**100 - 10**100

1.0 / 5

1 / 5

What will these results be?

CompSci 001 4.6

Python Operators

Precedence

*

%

**

/

>

<

==

+

-

Caution Level

=Highest

Lowest

**

* %/

> < ==

+ -

=

-

( )

( )

remainder

power

is equal to

set equal to

divide

as usual

CompSci 001 4.7

7 % 3

% the "mod" operator

8 % 3

9 % 3

16 % 7

x%4 == 0

x%2 == 0

For what values of x are these True?

What happens on these years?

x%y returns the remainder when x is divided by y

x%2 == 1

CompSci 001 4.8

Computer Memory

Random Access Memory (RAM)

byte = 8 bits

word = 4 bytes = 32 bits

is a long list of memory locations

bit = 1 "bucket" of charge

name: xtype: intLOC: 300

4 bytes for an int

on or off

is it really a coincidence that this looks like the string theory picture??42

CompSci 001 4.9

string functions

strlen+*

converts input to a string

returns the string’s length

str(42) returns '42'

len('42') returns 2

'XL' + 'II' returns 'XLII'

'VI'*7 returns 'VIVIVIVIVIVIVI'

concatenates strings

repeats strings

s1 = "ha"

s2 = "t"Given these strings

s1 + s2

2*s1 + s2 + 2*(s1+s2)

What are

CompSci 001 4.10

String surgery

s[ ] indexes into the string, returning a one-character string

s = ’duke university baby'

s[0] returns ’d'

s[6] returns

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

s[ ] returns 'e' Which index returns 'e'?

python != English

s[len(s)] returns

Read "s-of-zero" or "s-zero"

index

19

CompSci 001 4.11

More on strings

Negative indices count from the back

s[-1] returns ‘y'

s[-11] returns

s[-0] returns s[ : ] slices the string, returning a substring

s[ : : ] skip-slices, returning a subsequence

the third index is the "stride" length

it defaults to 1

CompSci 001 4.12

Lists ~ Strings of anything

L = [ 3.14, [2,40], 'third', 42 ]

Square brackets tell python you want a list.

len(L)

L[0]

L[0:1]

'hi'How could you extract from L

Slicing: always returns the same type

Indexing: could return a different type

Commas separate elements.

CompSci 001 4.13

Raising and razing lists

What are

"Quiz"pi = [3,1,4,1,5,9] Q = [ 'pi', "isn't", [4,2] ]

What slice of pi is [3,4,5]

What is pi[pi[2]]?

message = 'You need parentheses for chemistry !'

What is message[::5]

What are

pi[0] * (pi[1] + pi[2])

and

pi[0] * (pi[1:2] + pi[2:3])

What is message[9:15]

What are

What slice of pi is [3,1,4]

How many nested pi's before pi[…pi[0]…] produces an error?

Name(s):

Extra! Mind Muddlers

Part 2Part 1Q[0]

Q[0:1]

Q[0][1]

Q[1][0]

len(pi)

len(Q)

len(Q[1])

CompSci 001 4.14

Functioning in Python

Some basic, built-in functions:

abs

max

min

sum

range

round

bool

float

int

long

list

str

these change data from one type to another

absolute value

of lists

creates lists

only as accurately as it can!

help dirThese are the most important:

CompSci 001 4.15

Functioning in Python

Far more are available in separate files, or modules:

import math

math.sqrt( 1764 )

dir(math)

from math import *

pi

sin( pi/2 )

accesses math.py's functions

lists all of math.py's functions

same, but without typing math. all of the time…

help()help modules

CompSci 001 4.16

# my own function!

def dbl( x ):

""" returns double its input, x """

return 2*x

Functioning in Python

CompSci 001 4.17

# my own function!

def dbl( x ):

""" returns double its input, x """

return 2*x

Functioning in Python

Comments

Docstrings

(1) describes overall what the function does, and

(2) explains what the inputs mean/are

They become part of python's built-in help system! With each function be sure to include

one that

They begin with #

keywords

def starts the functionreturn stops it immediately

and sends back the return value

Some of Python's baggage…

CompSci 001 4.18

Functioning in Python

>>> undo('caf')

>>> undo(undo('caf'))

# is it dis-0 or dis-O, anyway?

def undo(s):

""" this "undoes" its string input, s """

return 'de' + s

strings, lists, numbers … all data are fair game

CompSci 001 4.19

random thoughts

import random

random.choice( range(0,100) )

help dirThese are the most important:

CompSci 001 4.20

Our DNA's nucleotides:

DNA

http://genome.ucsc.edu/cgi-bin/hgGateway

AGCT