Aleksandar Kuzmanovic Northwestern University EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University http://networks.cs.northwestern.edu/EECS110-s17/
What is computer science (CS)? CS != programming What is computer science (CS)? Take EECS 101
What is computer science (CS)? CS != programming What is computer science (CS)? Take EECS 101 "not equal to"
a vehicle, not a destination CS != programming programming : CS :: machining : engineering grammar : literature Programming equations : mathematics CS a vehicle, not a destination
Programming as learning a foreign language 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 …
Goal: Thinking like a machine 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
What will these results be? Python (numeric) data types What will these results be? Dominant float 1 / 5 long 10**100 - 10**100 int 1 // 5 0.2 42 bool 41 + True Recessive
Python Operators 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 7 % 3 8 % 3 9 % 3 16 % 7 x%2 == 0 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… x = 41 y = x + 1 41 42 Computation Data Storage 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 memory location 304 Computation Data Storage id, del
is a long list of memory locations 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 42 >> x = x + y ??
Naming data >> x = 41 >> y = x + 1 >> x 41 42 >> x = x + y ?? 83 ??
Naming data >> x = 41 >> y = x + 1 >> x 41 42 >> x = x + y ?? 83 ?? 42
You need lists of numbers, as well! Are numbers enough? No! You need lists of numbers, as well! list and strings are helpful, too. str
Can all this information be represented using lists ? 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.’ Text Sets Can all this information be represented using lists ? Sounds/Speech Networks Images/Video Ideas?
string functions str len + * s1 = 'ha' Given these strings s2 = 't' 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' repeats strings s1 = 'ha' Given these strings s2 = 't' ‘hat’ ‘hahathathat’ What are s1 + s2 2*s1 + s2 + 2*(s1+s2)
s = 'northwestern university' String surgery s = 'northwestern university' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexes into 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
s = 'northwestern university' String surgery s = 'northwestern university' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexes into 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
s = 'northwestern university' String surgery s = 'northwestern university' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexes into 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
s = 'northwestern university' String surgery s = 'northwestern university' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexes into 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
s = 'northwestern university' Negative indices… 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] = ‘ ‘ S[-0] = n s[-11] returns s[-0] returns
s = 'northwestern university' Slicing s = 'northwestern university' 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'
s = 'northwestern university' Slicing s = 'northwestern university' 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'
s = 'northwestern university' Skip-slicing s = 'northwestern university' 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 What does this return? s[1::6]
s = 'northwestern university' Skip-slicing s = 'northwestern university' 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 What does this return? s[1::6]
s = 'northwestern university' Skip-slicing s = 'northwestern university' 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 What does this return? s[1::6] 'osus'
Lists ~ Strings of anything 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'
Lists ~ Strings of anything 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'
Lists ~ Strings of anything 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'
Lists ~ Strings of anything 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'
Lists ~ Strings of anything 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]
message = 'You need parentheses for chemistry !' "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] What are len(Q) 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?
message = 'You need parentheses for chemistry !' "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] What are len(Q) 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?