Jed Rembold
September 5, 2025
Karel starts in the empty world shown at one of the marked positions and runs the below code. One of the starting positions will result in an error before the code finishes. Which one?
for i in range(2):
move()
turn_left()
for i in range(2):
if front_is_blocked():
turn_around()
move()
turn_right()
int for integers and whole
numbers
1, 2, 3, 10, 1001010101, -231float for numbers containing a
decimal point
1.23, 3.14, 10.001, 0.0, -8.23complex for numbers with an imaginary
component (which we won’t deal with)
i + j the sum of
i and ji - j the difference between
i and ji * j the product of
i and ji // j the floor division of
i by ji / j the division of
i by j†i % j the remainder when
i is divided by
ji ** j i
to the power of j‡-j the negation of
j – Returns
int if both i
and j are integers,
float otherwise
† – Returns
float always
‡ – Returns
float always if
j is negative
** (exponents, executed right to
left)-n (negative numbers)*, /,
//, %, executed
from left to right+ and -,
executed from left to rightWhat is the value of the below expression?
1 * 2 * 3 + (4 + 5) % 6 + (7 * 8) // 9
You create a variable by assigning it a value with Python’s
assignment statement, a single equals sign
(=):
|||variable name||| = |||expression|||The variable name must appear on the left of the
= in Python!
Python first computes the value of the right-hand side of the equals and then assigns to the name on the left
The same variable name can seem to appear on both sides of the equals!
total = total + value
total on the right represents some
existing valuetotal on the left is the new label
of whatever the right expression evaluates toWhen you assign a new value to a variable, the old value is lost
>>> A = 10
>>> A
10
>>> B = A + 5
>>> B
15
>>> A = B
>>> A
15Variables defined in terms of others do not get automatically updated!
Python evaluates expressions from the top down
>>> A = 10
>>> B = A + 2
>>> A = 8
>>> B
12this_is_amazingthisIsAmazingMAX_WIDTHradius and
Radius are different variable names!It is very common to want to adjust an existing variable value
balance = balance + depositPython gives you a shorter expression to describe this relationship:
balance += depositYou can do this with any operation
(|||op|||) following the general form:
|||variable||| |||op|||= |||expression|||You can name multiple variables at once by separating with commas
A, B, C = 1, 2, 3
What is the output value of A in the code
below?
>>> A = 10
>>> B = 4
>>> C = A * B
>>> A -= B
>>> A, B, C = C, A, B
>>> A
??