Friday, June 27, 2025
  • About
  • Advertise
  • Careers
  • Contact
Connect 4 Programming
  • Home
  • Python
  • Java
  • SQL
  • JavaScript
  • HTML
  • Data Structure
  • GIT
  • OOP
  • Interview Questions
  • Login
No Result
View All Result
Connect 4 Prog
Home Python

Python time sleep() Method

Python time sleep() Method

The Python time sleep() method suspends execution for a certain time limit. However, this method only halts the execution of a specific thread; and not the entire program.

The argument accepted by this method may be a floating point number to indicate a more precise sleep time.

Related posts

Tkinter GUI Projects With Python

Tkinter GUI Projects with Python PDF Guide

March 28, 2025
Python String split()

Python String split()

October 30, 2024

The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine.

Syntax

Following is the syntax for sleep() method −

time.sleep(t)

Parameters

t âˆ’ This is the number of seconds execution to be suspended.

Return Value

This method does not return any value. It just halts the program execution for the given time.

Example

The following example shows the usage of the Python time sleep() method.

import time

print("Start:", time.ctime())
time.sleep(5)
print("End:", time.ctime())

When we run above program, it produces following result −

Start: Tue Jan 10 09:48:49 2023
End: Tue Jan 10 09:48:54 2023

Example

Now let us see how this method works in a for loop.

In the example below, we are printing a range of numbers using the for loop and halting the execution for each iteration using the sleep() method.

import time

print("Start program")
for x in range (0, 5):
print(x)
time.sleep(2)
print("Program halted for 2 seconds")

print("End Program")

Once compiled and run, the program prints the numbers after sleeping for 2 seconds each −

Start program
0
Program halted for 2 seconds
1
Program halted for 2 seconds
2
Program halted for 2 seconds
3
Program halted for 2 seconds
4
Program halted for 2 seconds
End Program

Example

This method can also take fractional values as an argument. The program demonstrating it is given below.

import time

print("Start:", time.ctime())
time.sleep(0.5)
print("End:", time.ctime())

If we compile and run the given program, the output is displayed as follows where each print statement is executed with a 0.5 seconds gap −

Start: Wed Jan 11 14:20:27 2023
End: Wed Jan 11 14:20:27 2023

Example

Let us also try to pass a fractional valued argument to the method with the loop statements as shown in the example below −

import time

print("Start program")
for x in range (0, 5):
print(x)
time.sleep(2)
print("Program halted for 2 seconds")

print("5")
time.sleep(1.5)
print("Program halted for 1.5 seconds")
print("End Program")

On executing the program above, the output is displayed as follows −

Start program
0
Program halted for 2 seconds
1
Program halted for 2 seconds
2
Program halted for 2 seconds
3
Program halted for 2 seconds
4
Program halted for 2 seconds
5
Program halted for 1.5 seconds
End Program
Print PDF

Related Posts

71 Python Projects with References and Source Code
Python

71 Python Projects with References and Source Code

March 28, 2025
OOPS in Python Handwritten Notes
Python

OOPS in Python Handwritten Notes

March 28, 2025
Python Programming and SQL PDF
Python

Python Programming and SQL PDF

March 28, 2025
Basic Python Programs
Python

Basic Python Programs Handwritten PDF

March 28, 2025
Python Notes Handwritten
Python

Python Notes Handwritten PDF

March 28, 2025
Python Programming for Beginners PDF: Your Step-by-Step Guide to Learning Python
Python

Python Programming for Beginners PDF: Your Step-by-Step Guide to Learning Python

March 28, 2025

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

POPULAR NEWS

  • 71 Python Projects with References and Source Code

    71 Python Projects with References and Source Code

    0 shares
    Share 0 Tweet 0
  • OOPS in Python Handwritten Notes

    4 shares
    Share 0 Tweet 0
  • Most Asked JavaScript Interview (100 Q&A) PDF

    0 shares
    Share 0 Tweet 0
  • Most Asked Java Interview (100 Q&A) PDF

    0 shares
    Share 0 Tweet 0
  • Top 50 Java Interview Questions and Answers PDF

    0 shares
    Share 0 Tweet 0
Connect 4 Programming

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc.

Follow us on social media:

Recent News

  • Tkinter GUI Projects with Python PDF Guide
  • Python String split()
  • Desktop Notifier in Python

Category

  • Data Structure
  • GIT
  • HTML
  • Interview Questions
  • Java
  • JavaScript
  • OOP
  • Programming
  • Python
  • SQL

Recent News

Tkinter GUI Projects With Python

Tkinter GUI Projects with Python PDF Guide

March 28, 2025
Python String split()

Python String split()

October 30, 2024
  • About
  • Advertise
  • Careers
  • Contact

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Python
  • Java
  • SQL
  • JavaScript
  • HTML
  • Data Structure
  • GIT
  • OOP
  • Interview Questions