1 / 43

EECS 110: Lec 3: Data

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.

basil
Download Presentation

EECS 110: Lec 3: Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University http://networks.cs.northwestern.edu/EECS110-s14/

  2. 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

  3. 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, '!')

  4. 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, '!')

  5. 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 …

  6. Today • Data! • Labs at Wilkinson Lab tomorrow: • Half of Homework 1 • Bring your laptop if you’d like Goal: Thinking like a machine

  7. “Kinds” of data • What examples of data can you think of?

  8. “Kinds” of data • What examples of data can you think of? • Video • Statistics • Binary (I/0, True/False) • Matrices • Qualitative/quantitative

  9. 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

  10. * / % + - > < == 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 ( )

  11. % 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?

  12. Naming data >> x = 41 >> y = x + 1

  13. 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

  14. 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

  15. Naming data >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? >> y ??

  16. Naming data >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? 83 >> y ??

  17. Naming data >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? 83 >> y ?? 42

  18. Are numbers enough? No! You need lists of numbers, as well! list and strings are helpful, too. str

  19. 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?

  20. 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)

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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'

  27. 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'

  28. 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?

  29. 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?

  30. 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?

  31. 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'

  32. 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'

  33. 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'

  34. 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'

  35. 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]

  36. "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?

  37. "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?

  38. 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’)

  39. If statements (2) name = input('Hi... what is your name?') if name == ’Ning‘: print('x1’) else: print('x2’) print('x3’)

  40. 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’ )

  41. 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’ )

  42. 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’ )

  43. 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’ )

More Related