import time
import datetime
import threading
import subprocess
import time
time.time()
1529772044.2020128
startTime = time.time()
result = 1
for i in range(1, 10):
result = result * i
print(i, result)
endTime = time.time()
duration = endTime - startTime
print('Cost time : %s' %(duration))
1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880 Cost time : 0.0010063648223876953
import time
time.sleep(5)
print('Wakeup Now!')
Wakeup Now!
import time
for i in range(5):
time.sleep(1)
print('Wakeup Now!')
import time
now = time.time()
print (now)
1529772049.7269764
round(now)
1529772050
round(now, 1)
1529772049.7
round(now, 3)
1529772049.727
import datetime
#Get localtime
datetime.datetime.now()
datetime.datetime(2018, 6, 24, 0, 40, 49, 779979)
#Get UTCtime
datetime.datetime.utcnow()
datetime.datetime(2018, 6, 23, 16, 40, 49, 798978)
now = datetime.datetime.now()
now.year, now.month, now.day, now.hour, now.minute, now.second
(2018, 6, 24, 0, 40, 49)
datetime.datetime.fromtimestamp(1000000)
datetime.datetime(1970, 1, 12, 21, 46, 40)
datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2018, 6, 24, 0, 40, 51, 201974)
NewYear = datetime.datetime(2018, 1, 1)
ChristmasDay = datetime.datetime(2018, 12, 25)
NewYear == ChristmasDay
False
NewYear < ChristmasDay
True
NewYear != ChristmasDay
True
delta = datetime.timedelta(days=10, hours=10, minutes=10, seconds=10)
delta.days, delta.seconds
(10, 36610)
delta.total_seconds()
900610.0
delta = datetime.timedelta(weeks=1, days=10, hours=10, minutes=10, seconds=10)
delta.days, delta.seconds
(17, 36610)
delta_2 = datetime.timedelta(days=10, hours=34, minutes=10, seconds=10)
delta_2.days, delta_2.seconds
(11, 36610)
delta = datetime.timedelta(days=10, hours=10, minutes=10, seconds=10, milliseconds=1000)
delta.days, delta.seconds
(10, 36611)
import datetime
dt = datetime.datetime.now()
dt
datetime.datetime(2018, 6, 24, 0, 40, 54, 191976)
thousandDays = datetime.timedelta(days=1000)
dt + thousandDays
datetime.datetime(2021, 3, 20, 0, 40, 54, 191976)
dt + thousandDays*10
datetime.datetime(2045, 11, 9, 0, 40, 54, 191976)
| Strftime Directive | Meaning |
|---|---|
| %Y | Year with century, as in '2014' |
| %y | Year without century, '70' to '99' (1970 to 1999) & '00' to '69' (2000 to 2069) |
| %m | Month as a decimal number, '01' to '12' |
| %B | Full month name, as in 'November' |
| %b | Abbreviated month name, as in 'Nov' |
| %d | Day of the month, '01' to '31' |
| %j | Day of the year, '001' to '366' |
| %w | Day of the week, '0' (Sunday) to '6' (Saturday) |
| %A | Full weekday name, as in 'Monday' |
| %a | Abbreviated weekday name, as in 'Mon' |
| %H | Hour (24-hour clock), '00' to '23' |
| %I | Hour (12-hour clock), '01' to '12' |
| %M | Minute, '00' to '59' |
| %S | Second, '00' to '59' |
| %p | 'AM' or 'PM' |
import datetime
oct14st = datetime.datetime(2018, 10, 14, 17, 2, 3)
oct14st.strftime('%Y/%m/%d %H:%M:%S')
'2018/10/14 17:02:03'
oct14st.strftime('%I:%M %p')
'05:02 PM'
oct14st.strftime("%B of '%y")
"October of '18"
datetime.datetime.strptime('October 14, 2018', '%B %d, %Y')
datetime.datetime(2018, 10, 14, 0, 0)
datetime.datetime.strptime('2018/10/14 01:02:03', '%Y/%m/%d %H:%M:%S')
datetime.datetime(2018, 10, 14, 1, 2, 3)
datetime.datetime.strptime("October of '18", "%B of '%y")
datetime.datetime(2018, 10, 1, 0, 0)
import time, datetime
startTime = datetime.datetime(2029, 10, 31, 0, 0, 0)
while datetime.datetime.now() < startTime:
time.sleep(1)
print('Program now starting on Halloween 2029')
import threading, time
print('Start of program.')
def takeANap():
time.sleep(5)
print('Wake up!')
takeANap()
print('End of program')
Start of program. Wake up! End of program
import threading, time
print('Start of program.')
def takeANap():
time.sleep(5)
print('Wake up!')
threadObj = threading.Thread(target=takeANap)
threadObj.start()
print('End of program')
Start of program. End of program
print('Cats', 'Dogs', 'Frogs', sep=' & ')
Cats & Dogs & Frogs
import threading
threadObj = threading.Thread(target=print, args=['Cats', 'Dogs', 'Frogs'], kwargs={'sep': ' & '})
threadObj.start()
Cats & Dogs & Frogs
fileObj = open('D3_01.txt', 'w')
fileObj.write('Hello world!')
fileObj.close()
import subprocess
subprocess.Popen(['start', 'D3_01.txt'], shell=True)
<subprocess.Popen at 0x1a331243dd8>
fileObj = open('D3_02.py', 'w')
fileObj.write('print("Hello Jamie!")')
fileObj.close()
subprocess.Popen([r'C:\Program Files\Python36\python3.exe', 'D3_02.py'])
<subprocess.Popen at 0x1a331243908>
import time, datetime, subprocess
import time, datetime, subprocess
now = datetime.datetime.now()
print(now)
while now.minute!=42:
time.sleep(1)
now = datetime.datetime.now()
print(str(now.minute)+':'+str(now.second), end='>')
subprocess.Popen(['start', 'snoopy.JPG'], shell=True)
2018-06-24 00:41:30.914973 41:31>41:32>41:33>41:34>41:35>41:36>41:37>41:38>41:39>41:40>41:41>41:42>41:43>41:44>41:45>41:46>41:47>41:48>41:49>41:50>41:51>41:52>41:53>41:54>41:55>41:56>41:57>41:58>41:59>42:0>
<subprocess.Popen at 0x1a33124a668>
import time, subprocess
timeLeft = 5
while timeLeft > 0:
print(timeLeft, end='>')
time.sleep(1)
timeLeft = timeLeft - 1
subprocess.Popen(['start', 'Sleep_Away.mp3'], shell=True)
5>4>3>2>1>
<subprocess.Popen at 0x1a33124a0f0>