====== QUIZ01 ====== NAME: EMAIL: ----------------------- (01) How to run python? ----------------------- Let's start with the example in Chapter 1 in the textbook. >>> def greet(person): ==> Note that the followings are all valid. def greet(person): def greet (person): def greet( person): def greet(person ): def greet(person) : ==> Let's start again. >>> def greet(person): ... print "Hello", person ... print "How are you?" ... ==> Try the followings. >>> greet(John) ==> What do you get? >>> greet("John") >>> greet("Emily") Let's do the following. >>> # convert.py ==> What do you get? >>> # A program to convert Celsius temps to Fahrenheit ==> What do you get? >>> # ==> What do you get? >>> celsius = input("What is the Celsius temperature? ") ==> What do you get? ==> If you get any message or prompt, just type 100 and hit the enter key. >>> fahrenheit = 9.0 / 5.0 * celsius + 32 ==> What do you get? >>> print "The temperature is", fahrenheit, "degree Fahrenheit." ==> What do you get? Let's start with a script file. Assume that the following content is stored in the text file named as 'convert.py' # (1st line) # (2nd line) # convert.py # (3rd line) # A program to convert Celsius temps to Fahrenheit # (4th line) # (5th line) celsius = input("What is the Celsius temperature? ") # (6th line) fahrenheit = 9.0 / 5.0 * celsius + 32 # (7th line) print "The temperature is", fahrenheit, "degree Fahrenheit." # (8th line) (1) > python convert ==> What do you get? (2) > python convert.py ==> What do you get? (3) >>> import convert ==> What do you get? (4) >>> execfile('convert.py') ==> What do you get? (5) > convert.py ==> What do you get? > ./convert.py ==> What do you get? > which python ==> What do you get? ==> It is the path of your python interpreter. Add the path at the 1st line in 'convert.py' after '#!' and change the filename as 'convert1.py' (e.g., #!/lusr/bin/python) Note that "#!...." should be the first line and start from the first column. Please put "#!..." at other positions and check out resulting error messages. In case it is not working properly, use '#!/usr/bin/env python', instead. In fact, '#!/usr/bin/env python' provides better portability, enen though it's a little bit slow. (So, I RECOMMEND you to use '#!/usr/bin/env python' because it uses system's PATH information.) > chmod +x convert1.py ==> What is this? > convert1.py > ./convert1.py > cp convert1.py convert1.test > ls -ltr > ./convert1.test ---------------------------- (02) input() VS. raw_input() ---------------------------- input(): - gets raw input, sends it to eval(), and returns the result - assumes that what you enter is a valid Python expression - It is more or less the inverse of repr. raw_input(): - returns input in its unprocessed ("raw") form ------------------------ (3) Fill out the blanks. ------------------------ =============================================================== Data Type Data Model Mutability Access Type --------------------------------------------------------------- Numeric literal No Direct String literal No Sequential List container Yes Sequential Tuple container No Sequential Dictionary container Yes Mapping =============================================================== (4) List common operations of sequence types ==> IR, ML, CS Indexing e.g., s[i] Repetition e.g., s * 5 Membership test e.g., 'a' in s Length e.g., len(s) Concatenation e.g., s + t Slicing e.g., s[1:5]