############### Notes on Tuples, Strings and Arrays ################ # Python has a type of expression called "tuples", which are just # sequences of expressions grouped into a single expression. m = (3,5,7,9) # m is a tuple of integers print( len(m) ) # prints 4, which is the length of the tuple. # We can also access the individual members of the tuple m by specifying an # *index*. The first element of the tuple is the zeroth element, and the # last element has index len(m)-1: print(m[0]) # prints 3, the first element print(m[2]) # prints 7, the third element. print( m[len(m)-1] ) # will always print the last element (in this case 9) ### Arrays (Lists) # Arrays are similar to tuples with one big exception. Arrays are MUTABLE. # This means that the contents of an array can CHANGE. Syntactically, # arrays are indicated by square brackets instead of parentheses. # Arrays are also called lists in Python. Sometimes they're also called # "vectors". n = [2,3,5,7,11] p = ["abc",23] # arrays (and tuples) can contain elements of different types. # typical loop that visits each element of the array: i = 0 # starting index while i<=len(n)-1: # same as i=0: print(s[i], end="") # end="" does not add newline char at end i -= 1 # same as i = i-1 # while loop will print s backwards ## But strings are not arrays because you can't change a character in a ## string: s[0] = 'z' will give error. Strings are not mutable. ####### ASCII CODE # The individual characters that make up a string each has a numerical # code (so it can be represented in binary) called its ASCII value. print(ord('A')) # prints 65, ascii value for upper case letter A print(chr(66)) # prints B, the character associated with ascii code 66. # The following loop prints the ASCII character set: i = 32 # This is the code for the space character while i<128: print(i, chr(i)) i += 1 # while loop to print all ascii chars from 32 to 127 # Not all characters are printable. Control characters are not included. # Other characters not included above are ord('\n')==10, the newline # character, and ord('\t')==9, the tab character. # The largest ascii value is 127. # If you run the above code, you will see that the numerical characters # '0' to '9' also have ascii values. chr(0)==48, chr(1)==49, etc. # Another point to note is that the ascii value of a lower case character # is always 32 + the value of the corresponding upper case character. # Don't confuse the ord function with the int function: int("2") will # give you the number 2, but ord("2") returns 50, the ascii value of 2. ### ASCII stands for "American Standard Code for Information Interchange" # and is very old. It's now been extended to "Unicode", which includes # a much larger set of characters (64K), including characters in many # languages. But the ASCII part of this code still applies and is usually # what we're going to use unless we write programs for some language other # than English. You're welcome to modify the loop above to try to print # all characters up to chr(65535), but your system may not be able to display # all these characters (you must also have the right fonts installed). ## Building Strings. # Two strings can be concatenated together with +: s = "ab" + "cd" # s is now "abcd" # Strings are sometimes built char by char starting with the empty string "": alph = "" # initializes s with the empty string i = ord('a') # ascii value of 'a' while i <= ord('z'): alph = alph + chr(i) # adds next alphabetical char to end of alph i += 1 # i=i+1 # while print(alph) # prints alphabet # EXPERIMENT: what would happen if line above was changed to # alph = chr(i) + alph ??? #### Let's write some code that combine what we learned about arrays, # strings, and ascii values: #### The following code will convert a string into an array of ascii values: s = input("Enter any string: ") SA = [] # array of ascii values i = 0 # indexes s while i=ord('a') and val<=ord('z'): val = val-32 # ascii char of uppercase SA[i] = val i += 1 #while print("uppercase ascii values:",SA) #### now we convert the array of upper-case ascii values back to a string: sup ="" # start with empty array i = 0 while i