Sleep with time.sleep()
time.sleep(seconds) pauses execution for the given number of seconds. Useful for rate-limiting, animation delays, or waiting between retries.
import time
print("Starting...")
time.sleep(2)
print("Two seconds later!")
Try it Yourself →
Timing Code with perf_counter()
time.perf_counter() returns a high-resolution timestamp. Call it before and after a block of code to measure elapsed time precisely.
import time
start = time.perf_counter()
total = sum(range(1_000_000))
end = time.perf_counter()
print(f"Sum: {total}")
print(f"Time: {end - start:.4f} seconds")
Try it Yourself →