



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to programming with python, including an overview of idle, the python shell, and vpython, a graphical extension to python. It includes lab exercises and instructions for writing and running python programs, as well as creating and manipulating graphical objects with vpython.
Typology: Lab Reports
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Thursday 30 August, 2007 - Due: Thursday 6 September
The aim of this lab is to get you started with programming in Python. While programming has a strong abstract and theoretical component, it is also some- thing which most people learn with practice and experimentation. Here, you will learn about IDLE, the environment in which you develop Python programs, standalone programs and the interactive shell.
Programming takes patience and practice. Writing good programs requires planning and a logical approach. For someone who has not programmed before there is usually a period of frustration. Don’t worry - this is normal and will decrease with practice. Be careful to type things exactly as they are written in the lab – computers are very literal beasts and require all i’s to be dotted and t’s to be crossed before they will do your bidding. If in doubt refer to your Python book and the online documentation http://www.vpython.org/webdoc for Visual Python.
For the purposes of this class, a program in Python is simply one or more text files. These files are collections of lines of text that give a recipe telling the computer what to do. A program can be more than one file: one of the files will be the main program and the other files will be modules that give the main program additional capabilities. These files are read and interpreted by Python as commands. These commands have meanings such as ”print this” or ”draw that”, multiply these two numbers or do the following commands 1000 times. The Python interpeter, while reading the instructions, manages the data you create and follows your instructions to give more detailed instructions to the computer on what to draw or multiply or print. Python is called a high level language which means that you can tell the computer to do quite complicated things with relatively few, transparent instructions. The interpreter is then able to understand these instructions and translate them ultimately into binary instructions (strings of 0 and 1’s) which can be fed to the CPU of the computer to execute.
IDLE stands for Integrated DeveLopment Environment. It is the interface that you will use for writing and executing Python programs. It contains (amoung other things):
VPython is a special graphical extension to Python (technically a module) which gives a very nice simple environment to produce animations (simulations) of physical systems and record quantitative output data in the form of, for example, graphs. Typically a separate window is fired up to contain the graphical output.
You should record all your work and observations for each lab into a separate file (use Word, wordpad or your favorite text/document editor). You can subsequently print this out on the cluster printer to hand in.
2 Starting IDLE and the shell
Record in your report what the type of your first Python window.
3 Writing and running program files
Interactive mode as exemplified by the Python shell is neat for experimenting with simple commands but becomes tedious for creating real programs. To do the latter we build se- quences of commands and store them in a file. This allows you to use the program many times with ease and carry out small changes without retyping everything.
a= b= c=range(1,11) print a, "+", b,"is",a+b print "A list of the numbers between 1 and 11 is",c d=range(a,b) print "A list of numbers between",a,"and",b,"is",d
4 VPython
Now we will experiment with the graphical capabilities of Python.