Download String are Uninterpreted - Concepts of Computing - Lecture Slides | ECS 010 and more Study notes Computer Science in PDF only on Docsity! 1 1 ECS 10: Basic Concepts of Computing Prof. Michael Gertz
[email protected] April 11, 2008 2 Announcements Assignment 1 is due today at 10pm Assignment 2 will be posted this upcoming Monday/Tuesday Read recommended sections in the textbook indicated on slides and on Web page with lecture notes Try the examples I present in class (“get a feeling for different concepts in Python”) 2 3 In the previous lecture... IDLE Programming versus IDLE Script window Program “resides” in programming window and interaction with program (user input and output occurs in shell window >>>). Integers and floating point data types Type conversions int( ), float( ) round(<float> [,decimal points] ) Strings (yet another data type) are uninterpreted (e.g., 2+5 != “2+5”) 4 Strings are Uninterpreted "blah blah blah" - just a string of characters >>> type("blah blah blah") <type 'str'> 2 + 5 != "2 + 5" != means “does not equal” type(2+5) is 'int' and type(“2+5”) is 'str' >>> my_dog = "Rover" >>> print my_dog Rover >>> print "my_dog" my_dog Section 4.1 5 9 One more note on strings A string is a sequence of characters; such a sequence is indexed by numbers Index starts with 0 and ends with n-1, where n is the length of the string >>> len("COMPUTER SCIENCE") 16 >>> var = "COMPUTER SCIENCE“ >>> print len(var) 16 >>> my_len = len(var) >>> print my_len * 10 160 Section 4.1 len is a built-in function 10 One more note on strings var = “COMPUTER SCIENCE” 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 print var[2]+var[4]+var[9]+var[11]+var[14] MUSIC One can access individual characters and substrings using <string>[start:end] (from start to end, excluding end) var[0:8] COMPUTER (= var[:8]) var[3:8] PUTER var[9:] SCIENCE (= var[9:16]) var[-1] E var[-2] C ..... var[-16] C CI NECSRETU EPMOC Section 4.2 6 11 Summary Differences between Integers and Strings: They actually mean different things Strings are not "interpreted" and also + does different things when it's between two strings and when it's between two integers this is called overloading of symbols similarly: + between ints and between floats is sth different + between lists is again different and another one: X/Y is very different depending on whether X, Y, are ints or floats … 12 Loop Structures So far: Algorithm ~ Recipe ~ Program Linear sequence of instructions (Python statements) typically input, sequence of computations, output In practice, one often needs to repeat the same sequence of instructions several times depending on the user input depending on some computation .... Sum of N numbers, N!, .... Sections 2.6 & 8.1 7 13 Loop Structures There are different types of loops Definite loops (for-loops), typically based on some (computed) number Indefinite loops (while-loops), typically based on some condition that is evaluated to true or false Example (algorithm for computing average): Input the count of the numbers, n Initialize sum to 0 Loop n times Input a number, x Add x to sum Output average as sum/n 14 For-loops Pattern for <variable> in <sequence>: <loop body> # some computations, input etc. A useful built-in function to “compute” a sequence (list) of integers is the range function. >>>range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(1,11,3) [1, 4, 7, 10] Note the indentation!!