Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University

Similar presentations


Presentation on theme: "EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University"— Presentation transcript:

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

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

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

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

5 What is programming? 5 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… 6 syntax? How it looks semantics? What it does intent? 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… 7 syntax? How it looks semantics? What it does intent? 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 8 syntax? How it looks semantics? What it does intent? 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 9 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? 10

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

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

13 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 set equal to divide as usual

14 7 % 3 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 % the “mod” operator

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

16 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 id, del ComputationData Storage name: y type: int LOC: 304 42 memory location 304 Inside the machine…

17 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 42 Computer memory

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

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

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

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

22 Networks Images/Video Sounds/Speech { 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 Ideas? Can all this information be represented using lists ? More complex data

23 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 s1 + s2 2*s1 + s2 + 2*(s1+s2) What are

24 s[ ] indexes into the string, returning a one-character string s = 'northwestern university' s[0] returns 'n' s[12] returns 0123456789 101112131415161718 S[ ] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

25 s[ ] indexes into the string, returning a one-character string s = 'northwestern university' s[0] returns 'n' s[12] returns ' ' 0123456789 101112131415161718 S[ ] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

26 s[ ] indexes into the string, returning a one-character string s = 'northwestern university' s[0] returns 'n' s[12] returns ' ' 0123456789 101112131415161718 S[4] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

27 s[ ] indexes into the string, returning a one-character string s = 'northwestern university' s[0] returns 'n' s[12] returns ' ' 0123456789 101112131415161718 S[4] returns 'h' Which index returns 'e'? python != English s[len(s)] returnsERROR Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

28 s = 'northwestern university' Negative indices… -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 0123456789 10111213141516171819 2021 22 -20 -21 -22 -23 Negative indices count backwards from the end! s[-1] returns 'y' s[-11] returns s[-0] returns

29 s[ : ] slices the string, returning a substring s[5:9] returns 'west' s[0:5] returns 'north' What's going on here? s[17:] returns 'ersity' s[:] returns 'northwestern university' Slicing s = 'northwestern university' 0123456789 10111213141516171819 2021 22

30 s[ : ] slices the string, returning a substring s[5:9] returns 'west' s[0:5] returns 'north' s[17:] returns 'ersity' s[:] returns 'northwestern university' Slicing s = 'northwestern university' 0123456789 10111213141516171819 2021 22 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

31 Skip-slicing s = 'northwestern university' 0123456789 10111213141516171819 2021 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2] returns 'nrhe' What skip-slice returns What does this return? 'ruv' s[1::6]

32 Skip-slicing s = 'northwestern university' 0123456789 10111213141516171819 2021 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2] returns 'nrhe' What skip-slice returns What does this return? 'ruv' s[10:17:3] s[1::6]

33 Skip-slicing s = 'northwestern university' 0123456789 10111213141516171819 2021 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2] returns 'nrhe' What skip-slice returns What does this return? 'ruv' s[10:17:3] s[1::6] 'osus'

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

35 Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L)4 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.

36 Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. L[0]3.14 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. len(L)4

37 Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. L[0]3.14 L[0:1][3.14] 'hi' How could you extract from L Slicing: always returns the same type Indexing: could return a different type Commas separate elements. len(L)4

38 Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. L[0]3.14 L[0:1][3.14] 'hi' L[2][1:3] How could you extract from L Slicing: always returns the same type Indexing: could return a different type Commas separate elements. len(L)4

39 Raising and razing lists What are "Quiz" pi = [3,1,4,1,5,9]Q = [ 'pi', "isn't", [4,2] ] What slice of pi is [3,4,5] What is pi[pi[2]] ? message = 'You need parentheses for chemistry !' What is message[::5] What are pi[0] * (pi[1] + pi[2]) and pi[0] * (pi[1:2] + pi[2:3]) What is message[9:15] What are What slice of pi is [3,1,4] How many nested pi 's before pi[…pi[0]…] produces an error? Name(s): Extra! Mind Muddlers Part 2Part 1 Q[0] Q[0:1] Q[0][1] Q[1][0] len(pi) len(Q) len(Q[1])

40 Raising and razing lists What are "Quiz" pi = [3,1,4,1,5,9]Q = [ 'pi', "isn't", [4,2] ] What slice of pi is [3,4,5] What is pi[pi[2]] ? message = 'You need parentheses for chemistry !' What is message[::5] What are pi[0] * (pi[1] + pi[2]) and pi[0] * (pi[1:2] + pi[2:3]) What is message[9:15] What are What slice of pi is [3,1,4] How many nested pi 's before pi[…pi[0]…] produces an error? Name(s): Extra! Mind Muddlers Part 2Part 1 Q[0] Q[0:1] Q[0][1] Q[1][0] len(pi) len(Q) len(Q[1])

41 If statements (1) 41 name = input('Hi... what is your name? ') if name == ’Ning': # is it Ning? print('x1’) else: # in all other cases... print('x2’) print('x3’)

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

43 If statements (3) 43 name = input('Hi... what is your name? ') if name == ’Ning': print( 'x1' ) elif name == 'Aleksandar': print( 'x2' ) else: print( 'x3’ ) print( 'x4’ )

44 If statements (4) 44 name = input('Hi... what is your name? ') if name == ’Ning': print( 'x1' ) elif name == 'Aleksandar': print( 'x2' ) else: print( 'x3’ ) print( 'x4’ )

45 If statements (5) 45 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’ )

46 If statements (6) 46 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’ )


Download ppt "EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University"

Similar presentations


Ads by Google