Download presentation
Presentation is loading. Please wait.
Published byJodie Sims Modified over 9 years ago
1
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!
2
“ Kinds ” of Data What examples of “data” can you think of?
3
Python Data Types bool int long float 3.14 10**100 42 True False Numeric NameExampleWhat is it? values with a fractional part integers > 2147483647 integers <= 2147483647 the results from a comparison: ==, !=,, = "Boolean value"
4
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?
5
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
6
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
7
Naming Data Choosing the right name is more important than I thought. >> x = 41 >> y = x + 1
8
Inside the machine… x = 41 y = x + 1 name: x type: int LOC: 300 41 What is happening behind the scenes: What's happening in python: "variables as containers" memory location 300 ComputationData Storage name: y type: int LOC: 304 42 memory location 304
9
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: x type: int LOC: 300 4 bytes for an int on or off is it really a coincidence that this looks like the string theory picture?? 42
10
Naming Data Choosing the right name is more important than I thought. >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? >> y ??
11
Are numbers enough? No! You need lists of numbers, as well! and strings are helpful, too. list str
12
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 ?
13
str ing functions str len + * 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
14
String Surgery s[ ] indexes into the string, returning a one-character string s = ’Washington State' s[0] returns ’W' s[10] returns 0123456789 101112131415 s[ ] returns ’i' Which index returns 'e'? python != English s[len(s)] returns Reads as "s-of-zero" or "s-zero" index ?
15
Negative indices… s = ’Washington State’ 0123456789 101112131415 -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…
16
s[ : ] slices the string, returning a substring s[11:15] returns 'stat' s[0:4] returns 'Wash' s = ’Washington state' 0123456789 101112131415 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'
17
s[ : ] slices the string, returning a substring s = ’Washington state' 0123456789 101112131415 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'
18
s[ : ] slices the string, returning a substring s[15:-1] s[:] s = ’Washington state' 0123456789 101112131415 Slicing what to do if you want a bigger piece of the pie! What are these slices? How would you get 'ton' 'e'
19
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.
20
>>> 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…
21
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 dir These are the most important: You call that a language?!
22
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
23
# my own function! def dbl( x ): """ returns double its input, x """ return 2*x Functioning in Python
24
# 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 function return stops it immediately and sends back the return value Some of Python's baggage…
25
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.