This site applies only to CMPT 165 (Distance Ed) in Summer 2016. See the CMPT 165 sections page for other sections.
This lab is intended to get you started with Python.
-
If you haven't already, download and install Python (version 2.7.x, not version 3). (Of course, if you're working in a lab on-campus it's already installed.)
Open up IDLE. (In Windows, select Start → Programs → Python → IDLE (Python GUI).) Create an editor window (where you type your programs) by selecting File → New Window. Also see instructions for using Python.
Type (or copy and paste) this code into the editor window. Save it as
first.py
.print "I'm a Python program."
print "Outta my way!"Press F5 (or select Run → Run Module) to run the program.
-
Modify
first.py
so it prints this instead:I'm a Python program.
This course is "CMPT 165".In order to print the double quotes in the second line, you will have to use single quotes to enclose the string:
'This course is "CMPT 165".'
-
Create a Python program
webtest.py
that prints this (making sure there is a blank line after the first line):Content-type: text/html
<html>
<head>
<title>Web Script</title>
</head>
<body>
<h1>Web Script</h1>
<p>I'm a dynamically generated web page.</p>
</body>
</html>Check your program in IDLE first to make sure it runs and prints text like the above.
Upload this file to your web space on the course web server. Visit the page in your web browser. The URL will be something like this:
http://cmpt165.csil.sfu.ca/~userid/webtest.py
The server is set up so that Python programs (
.py
files) are executed and their output is sent to the browser. (Compare.html
files, where the content of the file is sent as-is.)
For this exercise, you will submit the first.py
file itself and the URL of the webtest.py
page. Submit the file and the URL to CourSys.
[It doesn't make sense to upload the first.py
file: it's not a web script, but the server would treat it like one since it's a Python program.]