Python Sleep – time.sleep() in Python
source link: https://www.freecodecamp.org/news/python-sleep-time-sleep-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
March 17, 2022 / #Python
Python Sleep – time.sleep() in Python
While running a Python program, there might be times when you'd like to delay the execution of the program for some seconds.
The Python time module has a built-in function called time.sleep()
with which you can delay the execution of a program.
With the sleep() function, you can get more creative in your Python projects because it lets you create delays that might go a long way in helping you bring in certain functionalities.
In this article, you will learn how to use the time.sleep()
method to create delays.
Just note that delays created with time.sleep()
do not stop the execution of the whole program – they only delay the current thread.
Basic Syntax of time.sleep()
To use time.sleep()
in your program, you have to import it from the time module first.
After importing the sleep()
function, specify the number of seconds you want the delay to run inside the parenthesis.
import time
time.sleep(delayInSeconds)
Basic Example of time.sleep()
In the code snippet below, I put a delay of 5 seconds between the 2 print statements, so the second print statement will run 5 seconds after the first print statement runs:
import time
print("Hello world")
time.sleep(5)
print("Hello campers")
You can also specify the delay in floating-point numbers:
import time
print("Hello world")
time.sleep(3.5)
print("Hello campers")
More Examples of time.sleep()
You can get more creative with delays created by time.sleep() by combining it with ctime()
, another built-in function from the time module that stands for “current time”.
import time
print("Execution started at: ", time.ctime())
time.sleep(10)
print("Hello world")
print("Execution ended at at: ", time.ctime())
# Output
# Executiuon started at: Thu Mar 17 10:37:55 2022
# Hello world
# Executiuon ended at at: Thu Mar 17 10:38:05 2022
You can also use time.sleep() to create multiple delays while looping through iterable data such as list or tuple.
The example below shows how I did it with a list:
import time
# Creating the list
legendaryFootballers = ["Okocha", "Pele", "Eusebio", "Martha", "Cruyff", "MAradona"]
for legend in legendaryFootballers:
# Creating the delay
time.sleep(2)
# Individual legends in the list will be printed after 2 seconds
print(legend)
The output:
Conclusion
This article took you through how to use the time.sleep()
function in Python.
time.sleep()
is an exciting built-in function that can be useful for creating delays in your Python projects, whether they're games, web projects, or AI systems.
Keep coding!
Web developer and technical writer focusing on frontend technologies. I also dabble in a lot of other technologies.
If you read this far, thank the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK