×

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

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.

Standard Scalar in Python

Python is a vast and open-source language that contains many libraries, modules, and functions. These functions in python are reusable codes where we don’t need to type the whole code...

4 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Difference Between Python and Scala

What is Python? Python is a high-level general-purpose interpreted programing language. It is used for multi general-purpose work such as language construct as well as its object-oriented approach aims to help...

4 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python Dictionary popitem() method

Python Dictionary popitem() method The dictionary.popitem() method in Python removes the item that was last inserted into the dictionary. Syntax dictionary.popitem() Parameter NA Return This method returns an arbitrary element (key, value) pair from the given dictionary...

1 minute read.

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

AX Contour 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...

3 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Curdir Python

Python Programming Language Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Colors in Python

Adding colour to your visualisations will help them come to life. Even if you know the colours you want to use, picking good ones and putting them into practise might...

4 minutes read.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.