Intro to Python types, lists, strings
Download
Report
Transcript Intro to Python types, lists, strings
This stuff
hurts my
brane.
String theory
Now
To Do: Lists
when you learn string theory from google images…
You should now be reading Chapters 2 & 3 in the
text!
Goal: Thinking like a machine
“Kinds” of Data
What examples of “data” can you think of?
Python Data Types
Numeric
Name
Example
What is it?
float
3.14
values with a
fractional part
long
10**100
integers > 2147483647
int
42
integers <= 2147483647
bool
True
False
the results from a comparison:
"Boolean value"
==, !=, <, >, <=, >=
Datatypes as genes…
Dominant
What will these results be?
float
1.0 / 5
long
10**100 - 10**100
int
1 // 5
bool
41 + True
Recessive
Precedence
( )
Caution Level
Highest
**
-
*
/
+
%
Python
Operators
assign
=
divide
/
remainder
%
power
**
is equal to
==
*
-
+
>
<
==
>
as usual
=
Lowest
It's not worth remembering all these %+/* things!
I’d go with parentheses over precedence
<
( )
% the "mod" operator
x%y returns the remainder when x is divided by y
7 % 3
8 % 3
9 % 3
16 % 7
x%2 == 0
For what values of x
are these True?
x%2 == 1
x%4 == 0
What happens on these years?
Naming Data
>> x = 41
>> y = x + 1
Choosing the right
name is more important
than I thought.
Inside the machine…
What's happening in python:
x = 41
y = x + 1
What is happening behind the scenes:
"variables as containers"
41
42
name: x
type: int
LOC: 300
name: y
type: int
LOC: 304
memory location 300
Computation
memory location 304
Data Storage
Computer Memory
Random Access Memory (RAM)
is a long list of memory locations
on or off
bit = 1 "bucket" of charge
byte = 8 bits
word = 4 bytes = 32 bits
42
name: x
type: int
LOC: 300
4 bytes for an int
is it really a coincidence that this
looks like the string theory picture??
Naming Data
>>
>>
>>
41
>>
42
>>
>>
??
>>
??
x = 41
y = x + 1
x
y
x = x + y
x
y
Choosing the right
name is more important
than I thought.
Are numbers enough?
No!
You need lists of numbers, as well!
list
and strings are helpful, too.
str
More Complex Data
{ 2,
3, 5, 7, 11
‘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.’
}
Sets
Can all this information be
represented using lists ?
Text
Sounds/Speech
Networks
Images/Video
Ideas?
string functions
str
len
+
*
str(42) returns '42'
converts input to a string
len('42') returns 2
returns the string’s length
'XL' + 'II' returns 'XLII'
concatenates strings
'VI'*7 returns 'VIVIVIVIVIVIVI'
Given these strings
What are
repeats strings
s1 = "ha"
s2 = "t"
s1 + s2
2*s1 + s2 + 2*(s1+s2)
What did you say!?!
String Surgery
s = ’Washington State’
0 1 2 3 4 5 6 7 8 9
s[ ]
10
11
12
13
14
15
indexes into the string, returning a one-character string
index
s[0] returns ‘W’
Reads as "s-of-zero" or "s-zero"
s[10] returns
s[ ] returns ‘i’
s[len(s)] returns
Which index returns 'e'?
?
python != English
Negative indices…
0 1 2 3 4 5 6 7 8 9
10
11
12
13
14
15
s = ’Washington State’
-16
-14
-15
-12
-13
-10
-11
-8
-9
-6
-7
-4
-5
-2
-3
-1
Negative indices count backwards from the end!
s[-1]
returns 'e'
s[-11] returns
s[-0]
returns
Python can suit
any mood…
what to do if you want a
bigger piece of the pie!
Slicing
s = ’Washington state'
0 1 2 3 4 5 6 7 8 9
s[ : ]
10
11
12
13
14
15
slices the string, returning a substring
What's going on here?
s[0:4] returns 'Wash'
s[11:15] returns 'stat'
s[14:] returns 'te'
s[:] returns 'Washington state'
Slicing
what to do if you want a
bigger piece of the pie!
s = ’Washington state'
0 1 2 3 4 5 6 7 8 9
s[ : ]
10
11
12
13
14
15
slices the string, returning a substring
the first index is the first
character of the slice
the second index is ONE
AFTER the last character
s[0:4] returns 'Wash'
a missing index means
the end of the string
s[11:15] returns 'stat'
s[14:] returns 'te'
s[:] returns 'Washington state'
Slicing
what to do if you want a
bigger piece of the pie!
s = ’Washington state'
0 1 2 3 4 5 6 7 8 9
s[ : ]
What are
these
slices?
How would
you get
11
12
13
14
slices the string, returning a substring
s[15:-1]
s[:]
'ton'
'e'
10
15
Lists ~ Strings of anything
Commas
separate
elements.
L = [ 3.14, [2,40], 'third', 42 ]
Square brackets tell python you want a list.
len(L)
L[0]
Indexing: could return a different type
Slicing: always returns the same type
L[0:1]
How could you
extract from L
'hi'
The in thing
>>> 3*'i' in 'alien'
False
python is badly confused here…
>>> 'i' in 'team'
False
but otherwise it seems
pretty perceptive!
>>> 'cs' in 'physics'
True
>>> ‘sleep' not in ‘CS 121'
True
>>> 42 in [41,42,43]
True
>>> 42 in [ [42], '42' ]
False
a little bit different for lists…
Functioning in Python
Some basic, built-in functions:
abs
bool
absolute value
float
max
min
of lists
sum
these change data from
one type to another
int
long
range
creates lists
list
round
only as accurately as it can!
str
These are the most important:
help
dir
You call
that a
language?!
Functioning in Python
Far more are available in separate files, or modules:
import math
accesses math.py's functions
math.sqrt( 1764 )
dir(math)
lists all of math.py's functions
from math import *
same, but without typing math.
all of the time…
pi
sin( pi/2 )
help()
help modules
Functioning in Python
# 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
keywords
Some of Python's baggage…
def starts the function
return stops it immediately
and sends back the return value
Comments
They begin with
#
Docstrings
They become part of python's built-in help system!
With each function be sure to include one that
(1)
describes overall what the function does, and
(2) explains what the inputs mean/are
Functioning in Python
def undo(s):
""" this "undoes" its string input, s """
return 'de' + s
>>> undo('caf')
>>> undo(undo('caf'))
strings, lists, numbers … all data are fair game