import time
import datetime
import threading
import subprocess
import time
time.time()
1511599245.4871345
s1 = time.time()
result = 1
for i in range(1, 10):
result = result * i
print(i, result)
s2 = time.time()
duration = s2 - s1
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.0009999275207519531
import time
time.sleep(5)
print('Wakeup Now!')
Wakeup Now!
import time
now = time.time()
round(now)
1511604084
round(now, 1)
1511604053.5
round(now, 3)
1511604084.082
import datetime
datetime.datetime.now()
datetime.datetime(2017, 11, 25, 18, 29, 32, 717089)
now = datetime.datetime.now()
now.year, now.month, now.day, now.hour, now.minute, now.second
(2017, 11, 25, 18, 34, 38)
datetime.datetime.fromtimestamp(1000000)
datetime.datetime(1970, 1, 12, 21, 46, 40)
datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2017, 11, 25, 18, 38, 59, 270808)
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
import datetime
dt = datetime.datetime.now()
dt
datetime.datetime(2017, 11, 25, 19, 19, 17, 739012)
thousandDays = datetime.timedelta(days=1000)
dt + thousandDays
datetime.datetime(2020, 8, 21, 19, 19, 17, 739012)
dt + thousandDays*10
datetime.datetime(2045, 4, 12, 19, 19, 17, 739012)
| Strftime Directive | Meaning |
|---|---|
| %Y | Year with century, as in '2014' |
| %y | Year without century, '00' to '99' (1970 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')
print('Cats', 'Dogs', 'Frogs', sep=' & ')
Cats & Dogs & Frogs
import threading
threadObj = threading.Thread(target=print('Cats', 'Dogs', 'Frogs', sep=' & '))
threadObj.start()
Cats & Dogs & Frogs
import threading
threadObj = threading.Thread(target=print, args=['Cats', 'Dogs', 'Frogs'], kwargs={'sep': ' & '})
threadObj.start()
Cats & Dogs & Frogs
Right-click the application’s Start menu item and select Properties to view the application’s filename.
CTRL-click the application and select Show Package Contents to find the path to the executable file.
fileObj = open('Docs\D3_01.txt', 'w')
fileObj.write('Hello world!')
fileObj.close()
import subprocess
subprocess.Popen(['start', 'Docs\D3_01.txt'], shell=True)
<subprocess.Popen at 0x4b0ac50>
fileObj = open('Docs\D3_02.py', 'w')
fileObj.write('print("Hello Olly!")')
fileObj.close()
subprocess.Popen([r'C:\Users\Olly\AppData\Local\Programs\Python\Python35\python.exe', 'Docs\D3_02.py'])
<subprocess.Popen at 0x4b0a668>
import time, subprocess
timeLeft = 5
while timeLeft > 0:
print(timeLeft, end='>')
time.sleep(1)
timeLeft = timeLeft - 1
subprocess.Popen(['start', 'Docs/Sleep_Away.mp3'], shell=True)
5>4>3>2>1>
<subprocess.Popen at 0x4b0a7b8>
import time, datetime, subprocess
now = datetime.datetime.now()
print(now)
while now.minute!=7:
time.sleep(1)
now = datetime.datetime.now()
print(str(now.minute)+':'+str(now.second), end='>')
subprocess.Popen(['start', 'Imgs/D3_08.GIF'], shell=True)
2017-11-30 00:06:52.985218 6:53>6:54>6:55>6:56>6:57>6:58>6:59>7:0>
<subprocess.Popen at 0x51bf9e8>