0% found this document useful (0 votes)
49 views7 pages

Carleton University Department of Systems and Computer Engineering ECOR 1051 - Fundamentals of Engineering I - Fall 2019 Lab 2 Objectives

Lab 2 for Ecor 1051 at Carleton University

Uploaded by

Eric Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

Carleton University Department of Systems and Computer Engineering ECOR 1051 - Fundamentals of Engineering I - Fall 2019 Lab 2 Objectives

Lab 2 for Ecor 1051 at Carleton University

Uploaded by

Eric Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Carleton University

Department of Systems and Computer Engineering


ECOR 1051 - Fundamentals of Engineering I - Fall 2019

Lab 2 ​- Working with Variables, Visualizing Code Execution

Objectives

● To gain experience translating mathematical formulae into Python statements that use
variables.
● To gain experience using the online Python Tutor to visualize the execution of the code.

Learning outcomes: 1, 3; Graduate attributes: 1.3, 5.3 (see the course outline)

Getting Started

Launch Wing 101. When Python starts, it prints a message in the shell window. The first line
should contain “3.7.4”, which indicates that Wing is running Python version 3.7. If another
version is running, ask a TA for help to reconfigure Wing to run Python 3.7.

In this lab, you'll use Python Tutor to edit and execute your code. ​PyTutor is a great tool for
visualizing the execution of short pieces of code, but it is not a complete program development
environment, and it doesn't provide a way for us to save our code in a file. So, while you're
working on the exercises, you'll use Wing 101 to edit a file that contains a copy of your
solutions.

Download ​lab2.txt​ from cuLearn and open it in Wing 101. (By default, only the names of
Python files are listed in the ​Open File​ dialogue box. You’ll need to change this so that ​lab2.txt
appears​. C​lick on the ​Files of type​ drop-down menu and select ​Plain Text​.)

This file is a template in which you will type your solutions to the exercises. Please, do not
change the layout of this file; that is, delete any lines in the template. Don’t change the
formatting; for example, use boldface or italicized text. You will be submitting your solutions as
plain text.

Remember, you don’t have to finish the lab during the lab period. You have until Sunday, Sept.
15 at 11:59 pm to submit your solutions for grading.

1
Exercise 1:​ To convert a temperature measured on the Celsius scale to the equivalent Fahrenheit
temperature, we multiply the Celsius temperature by 95 , then add 32. For example, 20.0 degrees
Celsius is equivalent to 68.0 degrees Fahrenheit.

Find the link ​Open Python Tutor in a new window​ in the ​Labs​ section of the course cuLearn
page. Launch Python Tutor, and verify that it ​is configured this way: "Write code in Python 3.6",
"hide exited frames [default]", "render all objects on the heap (Python/Java)" and "draw pointers
as arrows [default]". If necessary, select the correct options from the drop-down menus.

In Python Tutor's editor window:

1. Type ​one​ assignment statement that creates a new variable named ​degrees_c​ and binds
it to ​20.0​.

2. Type ​one​ assignment statement that converts the temperature bound to ​degrees_c​ to the
equivalent temperature in degrees Fahrenheit. This temperature should be bound to a new
variable named ​degrees_f​.

Click the ​Visualize Execution​ button. Execute the code, one statement at a time, by clicking
the ​Forward>​ button. Observe the memory diagram as the code is executed, step-by-step. Make
sure you can answer these questions:

● When does variable ​degrees_c​ appear in the diagram?


● What is the name of the frame containing ​degrees_c​?
● What does the arrow that points from ​degrees_c​ to ​20.0​ represent?
● When does variable ​degrees_f​ appear in the diagram?
● The arrow in ​degrees_f​ points to an object. What is the type and value of this object?

Verify that your code converts 20.0 degrees to 68.0 degrees Fahrenheit. When your code is
correct, copy/paste the two assignment statements from PyTutor into ​lab2.txt​, record your
answers to the questions, then save the file.

Exercise 2:​ In some countries, a vehicle's fuel efficiency is measured in miles per gallon. In
other countries, the efficiency is measured in litres per 100 km. For example, 32 miles per
Imperial gallon is equivalent to approximately 8.83 litres per 100 km.

You're going to write Python statements that convert miles per gallon to litres per 100 km. One
Imperial gallon is equal to approximately 4.54609 litres. One mile is equal to approximately
1.60934 km.

Delete the code in PyTutor's editor, then:

1. Type ​one​ assignment statement that creates a new variable named ​mpg​ and binds it to the
value ​32​ (which represents 32 miles per gallon).

2. Type ​two​ assignment statements that create new constants named ​LITRES_PER_GALLON
and ​KMS_PER_MILE​ and binds them to the values ​4.54609​ and ​1.60934​, respectively.
(Note that the names of constant values in Python are, by convention, usually written

2
entirely in uppercase.)

3. Type ​one​ assignment statement that converts the mileage bound to ​mpg​ to the equivalent
fuel consumption, measured in litres per 100 km. This value should be bound to a
variable named ​fuel_consumption​.

Click the ​Visualize Execution​ button. Execute the code, one statement at a time, and observe
the memory diagram as the code is executed, step-by-step. Make sure you can answer these
questions:

● When do the variables and objects appear in the diagram?


● What are the types and values of the objects?

Verify that your code converts 32 mpg into 8.83 litres per 100 km. When your code is correct,
copy/paste the four assignment statements from PyTutor into ​lab2.txt​, record your answers to
the questions, then save the file.

Exercise 3:​ Suppose you have some money (the ​principal​) that is deposited in a bank account
for a number of years and earns a fixed annual ​rate​ of interest. The interest is compounded ​n
times per year.

The formula for determining the amount of money you'll have is:
rate n·time
amount = principal(1 + n )
where:

● amount​ is the amount of money accumulated after ​time​ years, including interest.
● principal​ is the initial amount of money deposited in the account
● rate​ is the annual rate of interest, specified as a decimal; e.g, 5% is specified as 0.05
● n​ is the number of times the interest is compounded per year
● time​ is the number of years for which the principal is deposited.

For example, if $1,500.000 is deposited in an account paying an annual interest rate of 4.3%,
compounded quarterly (4 times a year), the amount in the account after 6 years is:
0.043 4·6
amount = $1, 500(1 + 4 ) ≈ $1938.84

Delete the code in PyTutor's editor, then:

1. Type ​four​ assignment statements that create new variables named ​principal​, ​rate​, ​n
and ​time​, and bind them to the values ​1500​, ​0.043​, ​4​ and ​6​ respectively.

2. Type ​one​ assignment statement that calculates the amount of money in the account after
time​ years. This value should be bound to a new variable named ​amount​.

Click the ​Visualize Execution​ button. Execute the code, one statement at a time, and observe
the memory diagram as the code is executed, step-by-step.

3
Verify that your code calculates that the accumulated amount of money is approximately
1938.84​. When your code is correct, copy/paste the five assignment statements from PyTutor
into ​lab2.txt​, then save the file.

Exercise 4:​ Consider these assignment statements:

a = 9
b = 4
c = a * b
d = b
a = 2
b = 3

Without using the Python shell or PyTutor to assist you​,

● predict the value that will be bound to ​c​ after Python executes ​a = 2​ followed by
b = 3​.

● predict the value that will be bound to ​d​ after Python executes ​b = 3​.

Delete the code in PyTutor's editor, then ​type the six assignment statements.

Click the ​Visualize Execution​ button. Execute the code, one statement at a time, and observe
the memory diagram as the code is executed, step-by-step. Were your predictions correct?

Record your answers to these questions in ​lab2.txt​.

● What value is bound to ​c​ before ​a = 2​ and ​b = 3​ are executed? Does this value
change when the two statements are executed?

● What value is bound to ​d​ before ​b = 3​ is executed? Does this value change when
b = 3​ is executed?

Exercise 5 starts on the next page.

4
Exercise 5:​ This is an edited excerpt from ​Practical Programming, 2nd Edition: An
Introduction to Computer Science Using Python 3​, Paul Gries, Jennifer Campbell, Jason
Montojo, © 2013 The Pragmatic Programmers LLC, Book Version P3.0, January 2016.

In this example, the variable ​score​ appears on both sides of the assignment statement:

>>> score = 50
>>> score
50
>>> score = score + 20
>>> score
70

This is so common that Python provides a shorthand notation for this operation:

>>> score = 50
>>> score
50
>>> score += 20
>>> score
70

An augmented assignment combines an assignment statement with an operator to make the


statement more concise. An augmented assignment statement is executed as follows:

1. Evaluate the expression on the right of the ​=​ sign to produce a value.

2. Apply the operator attached to the ​=​ sign to the variable on the left of the ​=​ and the value
that was produced in step 1. This produces another value. Store the binding to that value
in the variable on the left of the ​=​.

Note that the operator is applied ​after​ the expression on the right is evaluated:

>>> d = 2
>>> d *= 3 + 4
>>> d
14

All of the binary operators, ​+​, ​-​, ​*​, ​/​, ​//​, ​%​ and ​**​, have shorthand versions. For example, we
can square a number by multiplying it by itself:

>>> number = 10
>>> number *= number
>>> number
100

5
This code is equivalent to:

>>> number = 10
>>> number = number * number
>>> number
100

This table contains a summary of the augmented assignment operators:

Symbol Example Result

+= x = 7 x​ is bound to ​9
x += 2

-= x = 7 x​ is bound to ​5
x -= 2

*= x = 7 x​ is bound to ​14
x *= 2

/= x = 7 x​ is bound to ​3.5
x /= 2

//= x = 7 x​ is bound to ​3
x //= 2

%= x = 7 x​ is bound to ​1
x %= 2

**= x = 7 x​ is bound to ​49


x **= 2

Without using the Python shell or PyTutor to assist you​, predict the value that will be bound to ​x
after Python executes these two assignment statements :

x = 4
x *= x - x

Delete the code in PyTutor's editor, then ​type the two statements.

Click the ​Visualize Execution​ button. Execute the code, one statement at a time, and observe
the memory diagram as the code is executed, step-by-step. Were your predictions correct?

In ​lab2.txt​, write a short step-by-step explanation of how Python evaluates the statement
x *= x - x ​when ​x​ is bound to ​4​.

6
Wrap Up

1. After you've typed your solutions in ​lab2.txt​, login to cuLearn.

2. Click the link ​Submit Lab 2 for grading​.

3. Click the ​Add submission​ button.

4. Copy/paste your solutions from ​lab2.txt​ to the ​Online text​ box, then click the ​Save
changes​ button. The status of your submission is now ​Draft (not submitted),​ which
means that you can make changes.

5. Review the online text. If you want to make changes to your submission, click the ​Edit
submission​ button. When you are ready to finish submitting your lab work, click the
Submit assignment​ button.

6. When the ​Confirm submission​ page appears, click the ​Continue​ button to submit your
work. The status of your submission will change to ​Submitted for grading.​ If you've
changed your mind, click the​ Cancel​ button. This will return you to the page where you
can edit your submission.

Last edited: Sept. 5, 2019

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy