
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)
More Posts related to Python,
- Comments in Python Programming
- tkinter - Hello World! Program
- How to install Python 3.11 on Mac
- Python matplotlib segmentation fault: 11 macOS Big Sur
- Change label (text) color in tkinter
- Python Hello World! Program with code example (snippet)
- Calculate discount amount python code
- Take input argument from command line in Python Programming
- How to pip install Python Modules in VSCode
- Tkinter - add x and y padding to label text
- Python raise error with message example
- Check if String Contains a Substring - Python
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- What does b prefix before a String mean in Python?
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to List all Packages installed using pip [Python]
- Convert Float to String in Python
- Change the background of Tkinter label or text
- How to Install Python Modules in VS Code
- Indent Python code in Notepad++
- Validate email address in Python using regular expression (regex)
- Python: Fix command not found pip or pip3 on zsh shell
- TypeError: must be str, not int [Fix Python]
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python - Convert float to String
More Posts:
- How to Undo-Revert Sent Email in Google Gmail - Google
- Managed Metadata error - The data returned from the tagging UI was not formatted correctly - SharePoint
- Java location in Mac OS X - Mac-OS-X
- [Solution] Java JDBC SQLException: No value specified for parameter 1 - Java
- Path of homebrew (brew) installation in macOS Big Sur - MacOS
- How to declare and initialize Array in Java Programming - Java
- The service instance - SharePoint
- Save a Notepad file as Microsoft Excel file - Microsoft