time.sleep() is a simple yet powerful function in Python used to pause code execution for a specified duration in seconds.
The basic syntax is:time.sleep(seconds),You specify the number of seconds as an argument, and Python pauses the code for that time.
import time time.sleep(2) print("Waited for 2 seconds") This code pauses for 2 seconds before printing.
Ideal for: – Simulating delays – Controlling execution speed – Adding pauses between actions
Use time.sleep() wisely. Overusing it can lead to performance bottlenecks and inefficiencies in your code.
You can pass fractions of seconds to time.sleep(), e.g., time.sleep(0.5) pauses for half a second.