September 07, 2007 The followings contain all the contents that we covered in the class. So, please try out all the examples by youself. Note that you need to understand all the examples shown here to do your project. ========================== HOW TO ENTER PYTHON SHELL? ========================== > python ==> What do you get? ==> You'll have the contents similar to the following. Python 2.4.3 (#1, Aug 2 2006, 22:24:22) [GCC 3.3.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> ========================= HOW TO EXIT PYTHON SHELL? ========================= (1) >>> ^D (2) >>> import sys >>> sys.exit() (3) >>> raise SystemExit ================================ HOW TO GET HELP IN PYTHON SHELL? ================================ (1) >>> help ==> What do you get? >>> help() ........... ........... help> # Just hit the enter key or type 'quit' # then you are leaving help and returning to the Python interpreter >>> help() .......... .......... help> keywords ==> What do you get? help> (2) >>> help(keywords) ==> What do you get? >>> help('keywords') ==> What do you get? ======================================= LET'S PLAY WITH MORE EXAMPLES OF IMPORT ======================================= (1) >>> help(calendar) ==> What do you get? >>> import calendar >>> calendar.prcal(2007) ==> What do you get? ==> Can you print out calender for September 2007? ==> You may want to look at what functions are available in calendar module? How? >>> help(calendar) ==> Do you find a function that prints month's calendar? >>> calendar.prmonth(2007, 'September') ==> What do you get? >>> calendar.prmonth(2007, 9) (2) >>> import sys >>> sys.version >>> sys.version_info >>> sys.exit() (3) >>> import time >>> time.asctime() ==================================== HOW TO CHANGE DEFAULT PYTHON PROMPT? ==================================== >>> sys.ps1 ==> What do you get? ==> If you have an error message, explain why? >>> import sys ==> Then, why do you need this? >>> sys.ps1 ==> What do you get? ==> How to change this default python prompt? >>> sys.ps1 = "CS105> " ==> Then, what is it changed by this? CS105> sys.ps1 = 'PYTHON> ' ==> Then, what is it changed by this? PYTHON> sys.ps2 ==> What do you get? ==> What is it related with? PYTHON> def testFunction(): ==> What do you get? ==> Just hit the enter key again ==> What do you get? PYTHON> def testFunction(): ... print 'Not Indented block of testFunction()' ==> What do you get? PYTHON> def testFunction(): ... print 'Indented block of testFunction()' ... PYTHON> sys.ps2 = "BLOCK> " # sys.ps2 = 'BLOCK> ' PYTHON> def testFunction(): ==> What do you get? PYTHON> sys.ps1 = '>>> ' >>> sys.ps2 = '... ' ========================= HOW TO RUN PYTHON SCRIPT? ========================= 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 ======================= HOW TO REIMPORT MODULE? ======================= >>> ^D > python >>> import os ==> Why do we need this? >>> os.system('rm *.pyc') ==> What is it doing? >>> os.system('ls *.py*) ==> What do you get? ==> Hopefully, you see the filename, convert.py. >>> import convert ==> What do you get? >>> os.system('ls *.py*) ==> What do you get? ==> What is new? >>> import convert ==> What do you get? >>> reload(convert) ==> What do you get? ==> Notice the message (i.e., ). >>> os.system('rm convert.pyc') ==> What is it doing? >>> reload(convert) ==> What do you get? ==> Notice the message (i.e., module 'convert' from '??????????'>). >>> ^D > python >>> reload(convert) ==> What do you get? ============================== HOW TO REMOVE IMPORTED MODULE? ============================== We'll discuss this later in the class. ==> Hint: sys.getrefcount() del