×

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user.

Time sleep () syntax:

Sleep(seconds)

Limitations:

  • The number of seconds to be suspends the code as per its requirements.

Returns:

Void

The execution of the program must frequently be stopped to allow for a number of more executions or just for the sake of utility. In such a case, the useful function sleep() can be used to accurately and flexibly suspend the execution of code for any length of time. This function talks about its insight.

Time is a built-in function of the Python time module. Sleep() allows you to postpone a program's execution.

You can be more inventive with the sleep() function in your Python projects because it enables you to build delays that could be quite helpful in enabling particular functionality.

You will discover how to implement delays using the time.sleep() method in this post.

Just keep in mind that delays develop over time.

Sleep() just delays the current thread; it does not halt the entire program's execution.

Example 1:

Generating a time delay in seconds

With a 5 second delay, the start time and end time will be printed.

// python program
importtime
 
//beginning and printing the start time
Print("begin the execution of time code: ", time.ctime())
 
// using sleep() to the code execution
time.sleep(5)
 
// printing the end time
Print("end the execution of time code: ", time.ctime())

Output:

begin the execution of time code: tues mar 9 01:01:01 2020
end the execution of time code:tues mar 9 01:01:01 2020

Example 2:

Generating a time delay in minutes

We are delaying the time for 5 minutes

// python program 


importtime
 
//generating a list
Branches =[‘CSE’, ‘EEE’, ‘ECE’, ‘CIVIL’, ‘MECH’];
 
//generating a time delay of 5 minutes
time.sleep(5*60)
 
print(Languages)

Output:

After the delay of 5 minutes, it shown as:
[‘CSE’, ‘EEE’, ‘ECE’, ‘CIVIL’, ‘MECH’]

Applications:

Sleep() is used in a wide variety of contexts. Sleep can be used to implement the background thread execution that occurs repeatedly at pre - determined intervals (). For a great user experience, a common application prints the words letter by letter using the sleep() function.

Example 1:

Generating time delay in the program loop.

// python program


importtime
 
//declaring string
strn ="JavaTpoint"
 
//printing javatpoint after each character has been delayed
fori inrange(0, len(strn)):
    print(strn[i], end="")
    time.sleep(2)

Output:

JavaTpoint

Example 2:

Generating time delay in python list

// python program


importtime
 
//declaring a time delay of 10 seconds
time.sleep(10)
 // creating a list
myList =[‘happy’, ‘new’, ‘year’, 2023]
 
// the list will be displayed after the delay of 10 seconds
print(myList)

Output:

[‘happy’, ‘new’, ‘year’, 2023]

Example 3:

Generating time delay in python tuple

// python program 
importtime
 
// generating a time delay of 6 seconds
time.sleep(6)
 
// creating a tuple
mytuple =(‘ramesh’, ‘suresh’, ‘naresh’, ‘jhon’, ‘siri’, ’alexa’)
 
// the tuple will be showed
print(mytuple)

Output:

[‘ramesh’, ‘suresh’, ‘naresh’, ‘jhon’, ‘siri’, ‘alexa’]

Example 4: list comprehension

// python program
importtime
 
// generating a time delay of 6 seconds
time.sleep(6)
 
// creating a tuple
mytuple =(‘ramesh’, ‘suresh’, ‘naresh’, ‘jhon’, ‘siri’, ’alexa’)
 
// after every 6 seconds item of list gets showed
mytuple =[(time.sleep(6), print(my)) formy inmytuple]

Output:

Ramesh
Suresh
Naresh
John
Siri
Alexa

Example 5: Generating multiple time delays

// python program
importtime
 
// creating a list
Lang =['Java', 'C++', 'Python', 'Javascript']
 
// generating a time delay of 7 seconds
time.sleep(7)
 
// the list will be showed
print(Lang)
 
forlan inLanguages:  
    // generating a time delay of 10 seconds
    time.sleep(10)  
    //every 10 seconds an element is showed
    print(lang)

Output:

After the delay of 7 seconds, the list will be showed
['Java', 'C++', 'Python', 'Javascript']
Then after every 10 seconds, the element on the list will be showed
Java
C++
Python
Javascript

Python thread sleep

The sleep() method in Python is a crucial multithreading technique. The straightforward example that follows demonstrates how the Python time sleep function simply halts the execution of the currently running thread in multithreaded programming.

Example:

// python program
import time
from threading import Thread
class W(Thread):
def run(self):
for i in range(0, 11):
print(i)
time.sleep(1)




class Wa(Thread):
def run(self):
for j in range(100, 103):
print(j)
time.sleep(5)
print("w started")
W().start()
print("wa started")
Wa().start()
print("finished")

Output:

w started
0
Wa started
100
Finished
1
2
3
4
101
5
6
7
8
9
102
10

Related Topics

How to Download Python

How to Download Python Python is the most renowned programming language and developers across the globe are working on projects that are made using Python and its libraries. Here we will...

4 minutes read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

Loan calculator using Tkinter in Python

Tkinter: Tkinter, part of all common Python distributions, is the de facto method for creating Graphical User Interfaces (GUIs) in Python. The only framework included in the Python standard library is...

4 minutes read.

Latest Project Ideas using Python 2024

Python is one of the well-known languages for programming in the contemporary domain of computer science. The demand to adopt Python for IT purposes is steadily increasing because of its...

7 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python Algorithms

A quote goes, "A goal without a plan is just a wish." To achieve anything in life, we can't simply go for it without making a plan and sticking to...

4 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Assignment Operators in Python

The prime usage of Assignment Operators is to assign values to variables. These are taken into account to do operations on values and variables. There are some special symbols in python...

4 minutes read.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

Gethostbyname() function in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

Working with JSON in Python

Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. In this article, we are going...

4 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.