Apr 28, 2026 | 330 words | 3 min read
12.2.2. Task 2#
Learning Objectives#
Determine the output of a program through hand tracking
if-elif-else structures and UDFs. Investigate logical
operators in Python.
Task Instructions#
Open the task template py2_team_2_teamnumber.docx and save it with your
team’s number replacing teamnumber in the file name. For each exercise below,
add your answers to the template. When completed, save the document as a PDF named
py2_team_2_teamnumber.pdf.
Exercise A (if-elif-else)#
The Python code in Listing 12.4 is typed in after defining
x and y. Assume that all other variables are cleared before the
commands are executed.
Note
This exercise should be completed in Python's interactive mode. To start
interactive mode, open a terminal and type python, then press Enter.
You should see a prompt like >>> where you can type Python commands
directly. After each command, press Enter to execute it immediately and see the
result. To exit interactive mode, type exit() and press Enter.
if x <= 50:
z = 4
if y < 30:
z = x*y
elif y >= 100:
z = x+y
elif y >= 60:
if x > 80:
z = x
elif y > 50:
z = y
else:
z = z*2
print('z =', z)
What will appear on the screen when the following Python commands are executed before the code in Listing 12.4 is entered?
>>> x = 40 >>> y = 40
>>> x = 20 >>> y = 20
>>> x = 55 >>> y = 20
Exercise B (functions)#
Use hand tracking to answer the following questions about Python program in Listing 12.5. Check your work by running the code in Python's interactive mode.
What is printed to the console window?
What variables are in the global namespace, and what are their values?
def myFun1(in1, in2):
x = in1
y = in2
out1 = 6
out1, out2 = myFun2(x, y)
print(f'in1={in1:.0f}, in2={in2:.0f}, out1={out1:.0f}')
return out1
# --------------------------------------------------------
def myFun2(in2, in1):
a = in1*3
b = in2*2
out1 = a+b
out2 = a-b
print(f'in1={in1:.0f}, in2={in2:.0f}, out1={out1:.0f}, out2={out2:.0f}')
return out2, out1
# --------------------------------------------------------
def myFun3():
var1 = 1
var2 = 2
output = 3
print(f'var1={var1:.0f}, var2={var2:.0f}, output={output:.0f}')
output = myFun1(var1, var2)
print(f'var1={var1:.0f}, var2={var2:.0f}, output={output:.0f}')
# --------------------------------------------------------
var1 = 4
var2 = 2
myFun3()
print(f'var1={var1:.0f}, var2={var2:.0f}')
Exercise C (logic operators)#
Create a table with four columns labeled: logic operations, hand calculation, Python calculation, and differences.
Consider the logic operations in Listing 12.6 assuming that
a = True and b = False.
Copy the logic operations to the first column of the table.
Without using Python, determine the value of each logic operation by hand and record the results in the second column.
Perform the same logic operations in Python’s interactive mode, and record the results in the third column.
In the fourth column, explain any differences between the hand calculation and the Python calculation.
Note
In many programming languages, including Python, the integers \(1\) and
\(0\) become True and False respectively when converted to
boolean values. In these comparisons you may assume \(1\) to be equivalent to
True, and \(0\) to be equivalent to False.
1a and b
2a or b
3a == b
4(a and b) == (a or b)
5a != b
6a > b
7a >= b
8a = b
Deliverables |
Description |
|---|---|
py2_team_2_teamnumber.pdf |
Your answers for each part of this task. |