當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python time.sleep()用法及代碼示例


sleep()是在python 3的time()模塊中定義的。有時,需要破壞程序的流程,以便可以進行其他幾次執行,或者僅僅是由於所需的實用程序。在這種情況下,sleep()可以派上用場,它提供了一種準確而靈活的方式來在任何時間段內停止代碼流。此函數討論此函數的見解。

語法:sleep(sec)參數:sec:要求停止代碼的秒數。返回:VOID。


代碼1:展示sleep()

Python3

# Python code to demonstrate
# working of sleep()
 
import time
 
# printing the start time 
print("The time of code execution begin is:", end ="")
print(time.ctime())
 
# using sleep() to hault the code execution
time.sleep(6)
 
 
# printing the end time 
print("The time of code execution end is:", end ="")
print(time.ctime())


輸出:

The time of code execution begin is:Mon Apr  9 20:57:10 2018
The time of code execution end is:Mon Apr  9 20:57:16 2018


應用:
sleep()用於許多應用程序。如果以固定間隔重複執行後台線程,則可以借助sleep()來實現。另一個流行的應用程序是使用sleep()逐字母打印單詞,以獲得良好的用戶接口。後者由下麵的代碼表示。

代碼2:演示sleep()的應用

Python3

# Python code to demonstrate
# application of sleep()
 
import time
 
# initializing string 
strn = "GeeksforGeeks"
 
# printing geeksforgeeks after delay
# of each character
for i in range(0, len(strn)):
    print(strn[i], end ="")
    time.sleep(2)

輸出:

GeeksForGeeks
Note: Visible effect of sleep() can be seen in the local editor.

代碼3:在列表中創建時間延遲

Python3

# importing time package
import time
 
# creating a time delay of 5 seconds 
time.sleep(5)
 
# creating and Initilizing a list
myList = ['Jai', 'Shree', 'RAM', 5, 'August', 2020]
 
# the list will be displayed after the delay of 5 seconds
print(myList)

輸出:

延遲5秒後,我們將獲得以下輸出:

['Jai', 'Shree', 'RAM', 5, 'August', 2020]

代碼4:在元組中創建時間延遲



Python3

# importing time package
import time
 
# creating a time delay of 4 seconds 
time.sleep(4)
 
# creating and Initilizing a tuple
mytuple = ('Anil Kumbl', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid', 'Mahendra Singh Dhoni', 
          'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne')
 
# the tuple will be displayed after the delay of 4 seconds
print(mytuple)

輸出:

延遲4秒後,我們將獲得以下輸出:

('Anil Kumbl', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid',
'Mahendra Singh Dhoni', 'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne')

代碼5:創建多個時間延遲

Python3

# importing time package
import time
 
# creating and Initilizing a list
Languages = ['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
 
# creating a time delay of 5 seconds
time.sleep(5)
 
# the list will be displayed after the delay of 5 seconds
print(Languages)
 
for lan in Languages:
     
    # creating a time delay of 13 seconds 
    time.sleep(13)
     
    # After every 13 seconds an item of list will be displayed
    print(lan)

輸出:

延遲5秒後,列表將顯示為:

['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']

然後,每隔13秒,列表項將顯示為:

Java
C++
Python
Javascript
C#
C
Kotlin

代碼6:列表理解中的時間延遲

Python3

# importing time package
import time
 
# creating and Initilizing a list
cricketers = ['Anil Kumble', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid', 'Mahendra Singh Dhoni', 
          'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne']
 
# time delay of 7 seconds is created 
# after every 7 seconds item of list gets displayed
cricketers = [(time.sleep(7), print(cric)) for cric in cricketers]

輸出:

每隔7秒,列表中的項目將顯示為:

Anil Kumble
Sachin Tendulkar
Sunil Gavaskar
Rahul Dravid
Mahendra Singh Dhoni
Dennis Lillee
Muttiah Muralitharan
Shane Warne

代碼7:在列表中創建3分鍾的延遲

Python3

# importing time package
import time
 
# creating and Initilizing a list
Languages = ['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
 
# creating a time delay of 3 minutes
time.sleep(3 * 60)
 
# the list will be displayed after the delay of 3 minutes
print(Languages)

輸出:

延遲3分鍾後,列表將顯示為:

['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']


注:本文由純淨天空篩選整理自manjeet_04大神的英文原創作品 time.sleep() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。