Summary of Python Technical Operations that You Need to Know 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" NOTE: while you need to know these function calls to write some programs, you will not be tested on IO operations on quizzes and exams. The only IO you need to know will be print. 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. Strings are tuples, not arrays. This means that you cannot change a character inside a string. 4. Arrays: A = [2,3,5,7,11] # assigns A to an array A = 100 * [0] # intializes a array of 100 zeros. 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, which # is to say when i<0. 6. Functions # defining a function: def f(n): # function to compute n! ax = 1 # accumuator while n>1: ax = ax * n # same as ax *= n n = n-1 # same as n -= 1 # while return ax # f Things to understand about functions: the variable n is called a parameter or "argument". multiple arguments are separated by , n and ax are both "local variables" Their meaning stays local to the function. If you use the variable n or ax outside the function, they will be kept separate from the varibles here. # calling the function: x = f(5) Note the function call is an expression. The value of the expression is the value returned by the function. Quiz: What will be printed by the following: def f(x): x = x+1 return x #f x = 4 y = f(x) print x, y # answer: prints 4,5 # when we call the function we communicate with it at two points: when we # "pass" it the required arguments at the beginning, and when it returns a # value back at the end. There are actually two variables called x in the above code: the one inside the function f and the one outside. It's only coincidence that they are represented by the same symbol. When we call f(x), the local variable x is assigned the value of the external variable x, but they remain separate variables (referring to different locations in memory). That's why the internal x=x+1 does NOT change the value of the external varible.