Python Sleep Function/Method Code Example


Python Sleep Code Example
Python Sleep Code Example

What is sleep() function/method in Python Programming?

In computer programming sleep is an inactive state for a period of time for execution of a process, task, or thread.

In Python programming sleep() function/method is used to suspend the execution of a program for the provided number of seconds as an input parameter.

Python Sleep Syntax:

time.sleep(time)

Parameter/Argument:  time -> Number of seconds for which the execution is to be suspended.

Return value: sleep function does not return any value back.

Sleep Python Code Examples:

Example 1: sleep() for 10 seconds

# Example 1: Sleep for 10 seconds after saying Hello world!

import time

print("Hello World, We will wait for 10 seconds before saying bye!")
time.sleep(5)
print("Program completed...")

Example 2: sleep() for 10 seconds

# Example 2: One-minute countdown using sleep()

print("60 minute countdown started!")

for x in range(60, 0, -1):
  time.sleep(1)
  print(x)

print("60 minute countdown completed!")

Example 3: Digital Clock using sleep():

import time
import os

while True:
  os.system('cls') # Clear Screen
  print(time.strftime("%I:%M:%S %p", time.localtime()))
  time.sleep(1)
Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency