


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Thursday 18 January - Due: Thursday 25 January
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 and run Python programs. We will also experiment with solving some simple mechanics problems using Python/VPython.
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
if(ball.pos.y<0): run=
Also change the condition while(1): to while(run): at top of loop. You will need to set run=1 somewhere after you create the ball object also. This if statement is a standard Python construct and allows us here to break out of the infinite update loop if a suitable logical condition is satisfied. In this case the variable run is a logical (sometimes called Boolean) variable which can take the values either 1 (true) or false (0). Notice also that the attribute pos of the ball object is accessed with the code ball.pos and that the y-component is further accessed with another dot operator i.e ball.pos.y. Finally replace the statement from force import * by the new one from force_projectile import *