What's the Difference?

The questions below are due on Sunday June 29, 2025; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

1) Part 1

Consider the following code snippets:

# piece A
print(something)
# piece B
print("something")

Briefly describe the differences, if any, between these two pieces of code and how Python evaluates them.

Formatting Help

2) Part 2

Consider the following code snippets (assume num_list has been defined as a list of integers):

# piece A
i = 0
result = []
while i < len(num_list):
    if num_list[i] % 2 == 1:
        result.append(num_list[i] * 2)
        i = i + 1
    else:
        result.append(num_list[i])
        i = i + 1
print(result)
# piece B
for i in range(len(num_list)):
    if num_list[i] % 2 != 0:  # odd number
        num_list[i]  = num_list[i] * 2

print(num_list)

Briefly describe the differences, if any, between these two pieces of code.

Formatting Help

3) Part 3

Consider the following code snippets:

# piece A
a = [1]
b = (a, a)
a = b[0] + [7]
print(a)
print(b)
# piece B
a = [1]
b = (a, a)
b[0].append(7)
print(a)
print(b)

Briefly describe the differences, if any, between these two pieces of code and how Python evaluates them.

Formatting Help

4) Part 4

Consider the following code snippets (assume there is no elif/else after the snippet and that the hidden code is the same):

# Piece A
if condition:
    # hidden code ...
# Piece B
done = False
while not done and condition:
    # hidden code ...
    done = True

Briefly describe the differences, if any, between these two pieces of code and how Python evaluates them.

Formatting Help

Next Exercise: Survey

Back to exercises