Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aleksandar Kuzmanovic Northwestern University

Similar presentations


Presentation on theme: "Aleksandar Kuzmanovic Northwestern University"— Presentation transcript:

1 Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University

2 What is computer science (CS)?
CS != programming What is computer science (CS)? Take EECS 101

3 What is computer science (CS)?
CS != programming What is computer science (CS)? Take EECS 101 "not equal to"

4 a vehicle, not a destination
CS != programming programming : CS :: machining : engineering grammar : literature Programming equations : mathematics CS a vehicle, not a destination

5 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

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

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

8 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 …

9 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

10 “Kinds” of data What examples of data can you think of?

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

12 What will these results be?
Python (numeric) data types What will these results be? Dominant float 1 / 5 long 10** **100 int 1 // 5 0.2 42 bool 41 + True Recessive

13 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 ( )

14 % 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?

15 Naming data >> x = 41 >> y = x + 1

16 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

17 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

18 Naming data >> x = 41 >> y = x + 1 >> x 41
42 >> x = x + y ??

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

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

21 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

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

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

24 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

25 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

26 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

27 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

28 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

29 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'

30 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'

31 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]

32 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]

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

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

35 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'

36 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'

37 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'

38 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]

39 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?

40 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?


Download ppt "Aleksandar Kuzmanovic Northwestern University"

Similar presentations


Ads by Google