Unit 0 - Practice Problems

These problems are for practice. They are optional, but you are encouraged to complete them and discuss them with members of the course staff during lab and office hours.

0 - Mathematical Operators - Example

What is printed by the following program?

x = 0
for i in range(4, 6):
    x += i
    x -= i // 2
    x -= i % 2
print(x)
  1. Outside of the loop, x is initialized and assigned value 0.
  2. The loop on Line 2 will iterate variable i over range(4, 6), which corresponds to the collection [4, 5], so i will take on value 4 and then value 5.
  3. The first time the loop runs, i = 4. Lines 3, 4, and 5 are indented, and so they are inside the loop.
    1. Line 3: Remember that x += i is the same as x = x + i, adding i to x and assigning that value to x. Here, i is 4 and x is 0, so x + i is 4, and x becomes 4.
    2. Line 4: Remember that x -= i // 2 is the same as x = x - i // 2. The // operator is integer division, returning the quotient without the remainder. 4 divided by 2 has a quotient of 2 and a remainder of 0, so 4 // 2 is 2. Since x is 4 before this line, x -= i // 2 subtracts 2 from x, and x becomes 2.
    3. Line 5: The % operator is modulus, returning the remainder from integer division without the quotient, so 4 % 2 returns 0. x was 2 before this line, and subtracting 0 from x leaves x as 2.
  4. The second time the loop runs, i = 5. Lines 3, 4, and 5 run again for this value of i.
    1. Line 3: x was 2 before this line ran. 5 is added to x, and x becomes 7.
    2. Line 4: x was 7 before this line. 5 // 2 yields 2. 2 is subtracted from x and x becomes 5.
    3. Line 5: x was 5 before this line. 5 % 2 yields 1. 1 is subtracted from x and x becomes 4.
  5. The loop on Line 2 has iterated through all of its values. The program continues on to Line 6.
  6. x is 4, so 4 is printed to the console by print(x).
  7. The program is complete.

1 - Mathematical Operators

What is printed by the following program?

x = 0
for i in range(8, 11):
    x += i
    x -= i // 3
    x -= i % 3
print(x)

2 - Mathematical Operators

What is printed by the following program?

x = 0
for i in range(2, 5):
    x *= i
    x -= i
print(x)

3 - Mathematical Operators

What is printed by the following program?

x = 0
for i in range(4, 2023):
    x += i
    x -= i % 16
    x = i
print(x)

4 - Mathematical Operators

What is printed by the following program?

x = 1
for i in range(19, 19000):
    x += x % 5
print(x)

5 - Mathematical Operators

What is printed by the following program?

x = 1
y = 3
z = x // y + 2 
print(z)

6 - Mathematical Operators

What is printed by the following program?

x = 4
y = 5
z = 2 * y + x * x - 1 
print(z)

7 - Strings

This program intends to print: The sky in the West was red and black.

s = 'The sky in the West was'
color1 = 'red'
color2 = 'black'

print(s + color1 + 'and' + color2 + '.')

Does it produce this output? If not, why not?

8 - Strings

This program intends to print: 1 3 5 7 9 11

for i in range(1, 12, 2):
    print('i', end="")

Does it produce this output? If not, why not?

9 - Strings

This program intends to print:

2 squared is 4
5 squared is 25
8 squared is 64
for i in range(2, 9, 3):
    print(i + ' squared is ' + i * i)

There is an error on Line 2. Rewrite Line 2 so that the correct output will print.

10 - Variable Types

This program intends to print:

123
x = 4
x = x / 3
x = x * 3

for j in range(1, x):
    print(j, end="")

Does it produce this output? If not, why not?

11 - Variable Types

This program intends to print:

1
22
333
for j in range(3):
    print(j * str(j))

It does not produce this output. Rewrite Line 2 so that this output is produced.

12 - Functions

Which of these programs prints the following output:

A screaming comes across the sky.

A.

def gravity():
    print('A screaming comes across the sky.')

print('gravity')

B.

def gravity():
    print('A screaming comes across the sky.')

gravity()

C.

def gravity():
    print('A screaming comes across the sky.')

gravity

D.

def gravity():
    print('A screaming comes across the sky.')

print(gravity)

13 - Whitespace and Formatting

What is the difference, if any, between these two programs’ output? Both programs can be highlighted and copied for inspection.

First program.

print("It has happened before,", end=" ")
print('but there is nothing to compare it to now.')

Second program.

print("It has happened before, ",  end="")
print( 'but there is nothing to compare it to now.' )

14 - Whitespace and Formatting

Several of these programs produce the same output. Identify them. All programs can be highlighted and copied for inspection.

A.

print("""You will ride life straight to perfect laughter.
It's the only good fight there is.""")

B.

print("You will ride life straight to perfect laughter.\n")
print("It's the only good fight there is.")

C.

print("You will ride life straight to perfect laughter.\n", end = "")
print("It's the only good fight there is.")

D.

print("You will ride life straight to perfect laughter. ", end = "\n")
print("It's the only good fight there is.")

E.

print("You will ride life straight to perfect laughter.")
print("It's the only good fight there is.")

15 - Whitespace and Formatting

What is the difference, if any, between these programs’ output?

for q in range(3):
    print(q, end="")
for q in range(3):
    print(q, end=" ")