430 likes | 519 Views
EECS 110: Lec 3: Data. Aleksandar Kuzmanovic Northwestern University. http://networks.cs.northwestern.edu/EECS110-s14/. What is programming?. Programming as learning a foreign language. 1) Expect it to be different!. 2) Don’t feel you need to memorize it. 3) Immersion == Experimentation.
E N D
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University http://networks.cs.northwestern.edu/EECS110-s14/
What is programming? Programming as learning a foreign language 1) Expect it to be different! 2) Don’t feel you need to memorize it 3) Immersion == Experimentation
The foreign language of Python… syntax? semantics? intent? How it looks What it does What it should do name = input('Hi... what is your name?') print# prints a blank line if name == ’Ning': # is it Ning? print( name, '??’) print(‘You must be a TA!') elif name == ‘Aleksandar’: # is it Aleksandar? print( ‘You must be an instructor!') else: # in all other cases... print( 'Welcome to Python,', name, '!')
The foreign language of Python… syntax? semantics? intent? How it looks What it does What it should do name = input('Hi... what is your name?') print# prints a blank line if name == ’Ning': # is it Ning? print( name, '??’) print(‘You must be a TA!') elif name == ‘Aleksandar’: # is it Aleksandar? print( ‘You must be an instructor!') else: # in all other cases... print( 'Welcome to Python,', name, '!')
The foreign language of Python syntax? semantics? intent? How it looks What it does What it should do • how punctuation is used • the language keywords that are used • use of whitespace • peculiarities of formatting • how behavior is affected …
Today • Data! • Labs at Wilkinson Lab tomorrow: • Half of Homework 1 • Bring your laptop if you’d like Goal: Thinking like a machine
“Kinds” of data • What examples of data can you think of?
“Kinds” of data • What examples of data can you think of? • Video • Statistics • Binary (I/0, True/False) • Matrices • Qualitative/quantitative
Python (numeric) data types What will these results be? Dominant 1 / 5 float long 10**100 - 10**100 1 // 5 int bool 41 + True Recessive
* / % + - > < == Precedence Caution Level = ( ) set equal to Highest / divide ** Python Operators % remainder ** power == is equal to * = Lowest + > as usual < - 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
Inside the machine… What's happening in python: x = 41 y = x + 1 What is happening behind the scenes: "variables as containers" 42 41 name: y type:int LOC:304 name: x type:int LOC:300 memory location 300 memory location 304 Computation Data Storage id, del
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
Naming data >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? >> y ??
Naming data >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? 83 >> y ??
Naming data >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? 83 >> y ?? 42
Are numbers enough? No! You need lists of numbers, as well! list and strings are helpful, too. str
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.’ { 2, 3, 5, 7, 11 } Text Sets Can all this information be represented using lists ? Sounds/Speech Networks Images/Video Ideas?
string functions str(42)returns'42' converts input to a string str len + * len('42')returns2 returns the string’s length 'XL' + 'II'returns 'XLII' concatenates strings 'VI'*7returns 'VIVIVIVIVIVIVI' repeats strings s1 = 'ha' Given these strings s2 = 't' What are s1 + s2 2*s1 + s2 + 2*(s1+s2)
String surgery s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns S[ ]returns'h' Which index returns 'e'? s[len(s)]returns python!=English
String surgery s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns ' ' S[ ]returns'h' Which index returns 'e'? s[len(s)]returns python!=English
String surgery s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns ' ' S[4]returns'h' Which index returns 'e'? s[len(s)]returns python!=English
String surgery s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns ' ' S[4]returns'h' Which index returns 'e'? s[len(s)]returns ERROR python!=English
Negative indices… 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s = 'northwestern university' -23 -21 -19 -17 -15 -13 -11 -9 -7 -5 -3 -1 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 Negative indices count backwards from the end! s[-1]returns 'y' s[-11]returns s[-0]returns
Slicing s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : ] slices the string, returning a substring What's going on here? s[0:5]returns'north' s[5:9]returns'west' s[17:]returns'ersity' s[:]returns'northwestern university'
Slicing s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : ] 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 a missing index means the end of the string s[0:5]returns'north' s[5:9]returns'west' s[17:]returns'ersity' s[:]returns'northwestern university'
Skip-slicing s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2]returns'nrhe' 'ruv' What skip-slice returns s[1::6] What does this return?
Skip-slicing s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2]returns'nrhe' 'ruv' s[10:17:3] What skip-slice returns s[1::6] What does this return?
Skip-slicing s = 'northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2]returns'nrhe' 'ruv' s[10:17:3] What skip-slice returns s[1::6] 'osus' What does this return?
Commas separate elements. Lists ~ Strings of anything 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'
Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] Indexing: could return a different type Slicing: always returns the same type L[0:1] How could you extract from L 'hi'
Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] 3.14 Indexing: could return a different type Slicing: always returns the same type L[0:1] How could you extract from L 'hi'
Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] 3.14 Indexing: could return a different type Slicing: always returns the same type L[0:1] [3.14] How could you extract from L 'hi'
Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] 3.14 Indexing: could return a different type Slicing: always returns the same type L[0:1] [3.14] How could you extract from L 'hi' L[2][1:3]
"Quiz" Raising and razing lists Name(s): pi = [3,1,4,1,5,9] Q = [ 'pi', "isn't", [4,2] ] message = 'You need parentheses for chemistry !' Part 1 Part 2 len(pi) Q[0] len(Q) What are Q[0:1] len(Q[1]) What are Q[0][1] Q[1][0] What slice of pi is [3,1,4] What slice of pi is [3,4,5] What is message[9:15] What are pi[0] * (pi[1] + pi[2]) and pi[0] * (pi[1:2] + pi[2:3]) What is message[::5] Extra! Mind Muddlers What is pi[pi[2]]? How many nested pi's before pi[…pi[0]…] produces an error?
"Quiz" Raising and razing lists Name(s): pi = [3,1,4,1,5,9] Q = [ 'pi', "isn't", [4,2] ] message = 'You need parentheses for chemistry !' Part 1 Part 2 len(pi) Q[0] len(Q) What are Q[0:1] len(Q[1]) What are Q[0][1] Q[1][0] What slice of pi is [3,1,4] What slice of pi is [3,4,5] What is message[9:15] What are pi[0] * (pi[1] + pi[2]) and pi[0] * (pi[1:2] + pi[2:3]) What is message[::5] Extra! Mind Muddlers What is pi[pi[2]]? How many nested pi's before pi[…pi[0]…] produces an error?
If statements (1) name = input('Hi... what is your name?') if name == ’Ning': # is it Ning? print('x1’) else: # in all other cases... print('x2’) print('x3’)
If statements (2) name = input('Hi... what is your name?') if name == ’Ning‘: print('x1’) else: print('x2’) print('x3’)
If statements (3) name = input('Hi... what is your name?') if name == ’Ning': print('x1' ) elif name == 'Aleksandar': print('x2' ) else: print('x3’ ) print('x4’ )
If statements (4) name = input('Hi... what is your name?') if name == ’Ning': print('x1' ) elif name == 'Aleksandar': print('x2' ) else: print('x3’ ) print('x4’ )
If statements (5) name = input('Hi... what is your name?') if name == ’Ning': # is it Ning? print('x1') elif name == 'Aleksandar': print('x2') elif name == 'Lisa': print('x3') else: # in all other cases... print('x4’ ) print('x5’ )
If statements (6) name = input('Hi... what is your name?') if name == ’Ning‘: print('x1’ ) elif name == 'Aleksandar’: print('x2') elif name == 'Lisa': print('x3') else: print('x4’ ) print('x5’ )