25
Now String theory To Do: Lists This stuff hurts my brane. when you learn string theory from google images… Goal: Thinking like a machine You should now be reading Chapters 2 & 3 in the text!

Now String theory To Do: Lists This stuff hurts my brane. when you learn string theory from google images… Goal: Thinking like a machine You should now

Embed Size (px)

Citation preview

NowString theory

To Do: Lists

This stuff hurts my brane.

when you learn string theory from google images…

Goal: Thinking like a machine

You should now be reading Chapters 2 & 3 in the text!

“Kinds” of Data

What examples of “data” can you think of?

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"

Datatypes as genes…

bool

Dominant

int

long

float

Recessive41 + True

10**100 - 10**100

1.0 / 5

1 / 5

What will these results be?

Python Operators

I’d go with parentheses over precedence

Precedence

*

%

**

/

>

<

==

+

-

Caution Level

=Highest

Lowest

**

* %/

> < ==

+ -

=

-

( )

( )

It's not worth remembering all these %+/* things!

remainder

power

is equal to

assign

divide

as usual

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

Naming Data

Choosing the right name is more importantthan I thought.

>> x = 41

>> y = x + 1

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

Computation Data Storage

name: ytype: intLOC: 304

42

memory location 304

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

Naming Data

Choosing the right name is more importantthan I thought.

>> x = 41

>> y = x + 1

>> x

41

>> y

42

>> x = x + y

>> x

??

>> y

??

Are numbers enough?

No!

You need lists of numbers, as well!

and strings are helpful, too.

list

str

Networks

Images/Video

Sounds/Speech

{ 2, 3, 5, 7, 11 }

More Complex Data

‘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?

Can all this information be represented using lists ?

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

What did you say!?!

s1 + s2

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

What are

String Surgery

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

s = ’Washington State'

s[0] returns ’W'

s[10] returns

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

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

python != English

s[len(s)] returns

Reads as "s-of-zero" or "s-zero"

index

?

Negative indices…

s = ’Washington State’ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

-1

-2

-3

-4

-5

-6

-7

-8

-9

-10

-11

-12

-13

-14

-15

-16

Negative indices count backwards from the end!

s[-1] returns 'e'

s[-11] returns

s[-0] returns Python can suit

any mood…

s[ : ] slices the string, returning a substring

s[11:15] returns 'stat'

s[0:4] returns 'Wash'

s = ’Washington state'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Slicing what to do if you want a bigger piece of the

pie!

What's going on here?

s[14:] returns 'te'

s[:] returns 'Washington state'

s[ : ] slices the string, returning a substring

s = ’Washington state'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Slicing what to do if you want a bigger piece of the

pie!

the first index is the first character of the slice

the second index is ONE AFTER the last character

a missing index means the end of the string

s[11:15] returns 'stat'

s[0:4] returns 'Wash'

s[14:] returns 'te'

s[:] returns 'Washington state'

s[ : ] slices the string, returning a substring

s[15:-1]

s[:]

s = ’Washington state'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Slicing what to do if you want a bigger piece of the

pie!

What are these

slices?

How would you get

'ton'

'e'

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.

>>> 3*'i' in 'alien'

False

The in thing

>>> 'i' in 'team'

False

>>> 'cs' in 'physics'

True

>>> ‘sleep' not in ‘CS 121'

True

>>> 42 in [41,42,43]

True

>>> 42 in [ [42], '42' ]

False

python is badly confused here…

but otherwise it seems pretty perceptive!

a little bit different for lists…

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:

You call that a

language?!

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

# my own function!

def dbl( x ):

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

return 2*x

Functioning in Python

# 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…

Functioning in Python

>>> undo('caf')

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

def undo(s):

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

return 'de' + s

strings, lists, numbers … all data are fair game