Summary of Some Python Technical Operations 1. Converting a number to a string: str(12) gives the string "12". "abc"+12 is invalid, but "abc"+str(12) gives "abc12" 1b. int("35") is an expression that returns the NUMERICAL value 35. That is, it converts the STRING "35" to the NUMBER 35. It's the complement to the str operation above. The int function can also return an integer given a floating point number: int(3.9) return 3. Note that it doesn't round off. To round off, use the expression int(x+0.5) (think through why). To convert an integer to a floating point number, you can use the expression float(3), which returns 3.0 (you can also use expressions like x*1.0). 1c. This is also important: x = input("enter your name: ") will require you to type in the quotes for the string. However, x = raw_input("enter your name: ") will read whatevery you type in as a string (you don't have to type the quotes). However, if you enter 34, x will be assigned to the STRING "34", not the NUMBER "34" 2. Each character has a cooresponding ASCII code. ord("A"), for example, returns 65, which is the ASCII code for the letter "A". chr(65) will give "A". 3. The expression len(s) will return the length (number of characters) in the string s. s[0] is the first character. s[len(s)-1] is the last character. 4. Arrays: A = [2,3,5,7,11] # assigns A to an array A.append(13) # adds element 13 to the end of the array. Note that this # line is a STATEMENT: it CHANGES the value of A. A+[3,4,5] # adds elements 3,4,5 to end of the array, HOWEVER, unlike # append this is an EXPRESSION, not a statement. The value # A is not changed. A = A+[x] would be the same as # A.append(x) len(A) # expression that returns the length of A (in this case 6) A[0] # expression to return the first element (in this case 2) A[len(A)-1] # expression to return the last element (in this case 13) A[3] = A[3] + 1 # assignment statement (changes the 7 to 8) 4 in A # boolean expression (in this case False since 4's not in A) 5. While loops. This subject is more than just a technical command or expression. But here's an example to print every element of an array A backwards: i = len(A)-1 # start with last index of A while i>=0: print A[i], # the comma at the end allows for printing on the same line i = i-1 # decrement counter to print next char from end # end while # At this point, the value of the variable i is -1, and it's important # that you see why. The while loop stops when i>=0 becomes false.