ENGG1811 Lab 02: Introduction to Python Programming

Objectives

After completing this lab, students should be able to write and run simple Python programs using assignment, mathematical expression, print() and the math library. You will also become familiar with the Spyder software development environment.

Marking Scheme

Each lab will be marked out of 3 marks: 1 mark from an online multiple choice question and 2 marks from your tutor's assessment of your lab work. Remember you must be ready for some assessment half an hour before the end of the class.

There are three exercises for this week. The online assessment question requires the knowledge and skills that you have acquired after doing the first two exercises. We recommend that you attempt it after finishing these exercises. You can only attempt your online exercise once so make sure you select the correct answer.


Getting started   

We assume that you have already completed Lab01 which means you know how to log onto a lab computer and perform some basic operations.

Before starting your exercises, there are four tasks that we would like you to do:

  1. You will be working with many files for the entire duration of this course. It is a good idea to get them organised. We recommend that you put the files in each lab in a separate folder. Double click on the "Home" icon and create a new folder called "lab02". More detailed instructions are available at the Getting Started section of the course website. 
  2. By clicking on the "ENGG1811" icon, it will start a web browser with the ENGG1811 website as its home page. 
  3. Locate this week's lab (lab02) on the course website. 
  4. You can start Spyder by clicking on the "Spyder3" icon. 

Exercise 1: Debugging a Python program

Download this Python program with the filename lab02_ex1.py (right mouse click to download) and save it in the directory lab02 that you have created earlier. If you do not know how to do this, there are more detailed instructions at the Getting Started section of the course website.

The file contains 2 lines of code. The first line of code (in Line 12) defines a variable. The intention of the second line of code (in Line 15) is to compute the square of the variable defined earlier. If you run the file, it returns an error. Check the console to see what the error is and try to fix the program so that it does what it is intended for.

If you manage to fix the bug, the console will not report any errors. The next step is to check whether the program is calculating the correct value. If you type the name of the variable you want to check into the console, it will print the value of that variable and you can make sure it is correct. Do that.

If you have difficulty in doing this question, you should consult p.27-28 of Week 1's lecture notes.


Exercise 2: Computing the number of revolutions and angular position

In this exercise you will write a Python program using the Spyder editor and execute it. You can consult the program on temperature conversion that we discussed in Week 1's lecture as an example. A complete version of the program under code_final of Week 1's lecture. You should write your program in a separate file.

The picture below depicts a rotating disc and a particular point on the disc has been marked with a red dot.

The disc is rotating at a uniform angular speed of 621 degrees per second. You can assume that the red dot is at an angular position of 0 degrees at time 0. For a given time t, you want to determine the number of complete revolutions that the disc has made and the angular position of the red dot. For example, if t is 6, then the disc will have turned 621 times 6 = 3726 degrees. This corresponds to 10 complete revolutions and the red dot will be at an angular position of 126 degrees. 

You are asked to write a Python program which does the following:

In addition, we impose these requirements:

Question: Did you define any intermediate variables in your program? Why or why not? Discuss this with your lab mates and tutor.

Remark: In some programming languages, you can specify that a variable name is a constant and assign it a value. It is called a constant in these programming languages because its value cannot be changed when the program is executed. Although Python does not perform such checks, it is a good programming practice to define the constants in the beginning of a program because they give meanings to the numbers that you use in a program.

Hint: Think about the example earlier which says that 3726 degrees corresponds to 10 complete revolutions and an angular position of 126 degrees. You need to think about how you can obtain the numbers 10 and 126 from 3726 degrees. You need to do some specific arithmetic calculations to get those numbers. What are they? If you can figure out what calculations are needed, then you can choose the appropriate Python arithmetic operators to do the work for you. A list of Python arithmetic operators can be found on page 14 of Week 1's lecture notes.


Exercise 3: Computing the position of a projectile

In this exercise you will be using the math library to do some calculations. Pages 62-65 of Week 1's lecture are relevant to this exercise. You may also consult the program math_example.py which is filed under the code_prelim section of Week 1's lecture. You should write your program in a separate file.

You have learnt in Physics on how to calculate the position of a projectile subject to constant gravity. Let us define the following mathematical quantities:

Assuming the projectile is initially situated at the coordinates (0,0), the formulae for computing x(t) and y(t) are:


You are asked to write a Python program which does the following:

In addition, we impose the requirements that you should choose informative names for your variables and document your program.

Hint: If you are getting an error when you run your code and you cannot see any issues, take a look at your editor. It will warn you with a yellow warning sign (see the image below) where it thinks you've made some mistakes. You can hover your mouse over the warning symbol to see what the issue might be. If the warning is "undefined name 'pi'", "undefined name 'cos'" or something similar, then you have forgotten to do something. It can be something that you have not done in the beginning of the program. It can also be something that you have forgotten at those lines that editor is warning you. Have a look at Slide 64 in the Week's 1 lecture notes.



At the End of the Lab

You should be able to show your tutor the three exercises. You should be comfortable with using Spyder, and be able to write and run simple Python programs.

Finally, do not forget to complete your on-line multiple choice question if you have not done it yet. 

If you have completed everything, please do not forget to logout. Simply double click on the "Log Out" icon