Measure the execution time of small bits of Python code with the “timeit” module

# The “timeit” module lets you measure the execution # time of small bits of Python code >>> import timeit >>> timeit.timeit(‘”-“.join(str(n) for n in range(100))’, number=10000) 0.3412662749997253 >>> timeit.timeit(‘”-“.join([str(n) for n in range(100)])’, number=10000) 0.2996307989997149 >>> timeit.timeit(‘”-“.join(map(str, range(100)))’, number=10000) 0.24581470699922647

Swap two variables using Python easily

#Why Python Is Great: # In-place value swapping # Let’s say we want to swap # the values of a and b… a = 23 b = 42 # The “classic” way to do it # with a temporary variable: tmp = a a = b b = tmp # Python also lets us #…