def turnRight(): turnLeft() turnLeft() turnLeft() def turnAround(): turnLeft() turnLeft()
def leftIsClear(): turnLeft() ret = frontIsClear() turnRight() return ret
frontIsClear(). Introduce the notion of a variable. ret is a variable in the example above.
x = 7This associates the object
7 into the variable x, i.e., x -> 7.
x in the code will evaluate to the associated objectx becomes a shorthand for 7= in this way is called "variable assignment"del x. Deleting does not necessarily remove the object.None represents a null value. None is not the same as 0, False, or an empty string. None is a data type of its own (NoneType) and only None can be None.
>>> name = "ABCDEFGH"
>>> print("happy birthday to " + name)
happy birthday to ABCDEFGH
>>> print("happy birthday to " + name)
happy birthday to ABCDEFGH
>>> print("happy birthday to " + x)
NameError: name 'x' is not defined
>>> print("happy birthday to " + x)
NameError: name 'x' is not defined
>>> y = None
>>> print("happy birthday to " + y)
TypeError: can only concatenate str (not "NoneType") to str
True and False.if-then-else. Implement noBeeperPresent() using beeperPresent(). Construct and show its example usage to implement the case where you fill the pothole only if a beeper is not already present.if-then-else a bit more using arbitrary statements as follows:
S1
S2
else:
S3
S4
S5
S6
If cond is True, then S1;S2;S5;S6; get executed.
If cond is False, then S3;S4;S5;S6; get executed.
if can appear without else:
if (cond): S1 S2 S5 S6What happens if
cond is True? What happens if cond is False?
for loop.count" number of times, i.e., count iterations:
S0 count = 1000 for i in range(count): #0..999 S1 S2 S3The
range function returns a sequence of numbers.
numPotholes = 5 for i in range(numPotholes): fillNextPothole()
S0 count = 1000 for i in range(count): #0..999 S1 S2 S3
S1 is count = 0? Answer: The loop still executes 1000 times; the range construct has already constructed a range.S1 is i = i + 3? Answer: The loop still executes 1000 times; i gets re-assigned each time control transfers to the for loop. At the end of the program (after S3), the value of i will be the 999+3=1002.
range construct to start from a particular index (instead of the default index of 0) using range(begin, end). This leads to begin, begin+1, ..., end-1.range construct to skip by a particular step (instead of the default step of 1) using range(begin, end, step). This leads to begin, begin+step, ..., endi (where endi is the largest number of the form begin+step*n that is smaller than end.while loop:
def moveTillClear():
while (frontIsClear()):
move()