python第十七天---時間模組、random模組

來源:互聯網
上載者:User

標籤:close   ace   cti   作業   cal   code   print   opened   變數   

作完一個作業,開始新的學習:

有由今天的時間有限所有學習了以下兩個模組,明天繼續!

 

時間模組、random模組

 

import time

 1 #!usr/bin/env python 2 #-*-coding:utf-8-*- 3 # Author calmyan 4 import time ,datetime 5 print(time.process_time())#測量處理器運算時間,不包括sleep時間 6  7 print(time.altzone)#返回與UTC時間的時間差 以秒計算 8 print(time.asctime())#返回時間格式  "Fri May 19 11:12:23 2017" 9 print(time.time())#時間戳記10 print(time.localtime())#返回本地時間的struct_time 對象格式11 print(time.localtime(time.time()+3600))#返回本地時間的struct_time 對象格式12 13 print(time.gmtime())#返回UTC時間utc時間的struct_time對象格式14 print(time.gmtime(time.time()+3600))#返回UTC時間utc時間的struct_time對象格式15 print(time.time())#目前時間戳16 print(time.asctime(time.localtime()))#返回時間格式 Fri May 19 18:16:42 201717 18 print(time.ctime())#返回時間格式 Fri May 19 18:16:42 201719 20 print(time.strptime(‘2017-05-19 18:16:42‘,‘%Y-%m-%d %H:%M:%S‘ ))#將時間日期文字轉成時間對象21 22 string_2=time.strptime(‘2017-05-19 18:16:42‘,‘%Y-%m-%d %H:%M:%S‘ )#將時間字串轉成時間對象23 stu_2=time.mktime(string_2)#mktime可以將時間對象轉為時間戳記24 print(stu_2)25 26 string_3=time.gmtime(stu_2)#轉成時間對象27 print(string_3)28 string_4=time.strftime(‘%Y-%m-%d %H:%M:%S‘,string_3)#轉成時間字串29 print(string_4)30 31 print(datetime.datetime.now())#目前時間,本地2017-05-19 20:16:52.16596832 print(datetime.date.fromtimestamp(time.time()) )#時間戳記直接轉成日期格式 2017-05-1933 34 print(datetime.datetime.now() + datetime.timedelta(3)) #目前時間+3天35 print(datetime.timedelta(5.3)) #5天時間36 print(datetime.timedelta(-6.4)) #-6天時間37 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #目前時間+3小時38 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #目前時間+30分39 print(datetime.datetime.now() + datetime.timedelta(seconds=40)) #目前時間+30分40 41 a_time  = datetime.datetime.now()42 print(a_time.replace(minute=3,hour=2)) #時間替換43 print(a_time)44 print(a_time.replace(minute=3,hour=2,second=18,day=4)) #時間替換
View Code

import random

 1 #隨機模組 2  3 import random,string#字元模組 4 a=[1,2,3] 5 a.append(random.random())#隨機小數 6 a=random.random()#隨機小數 7 print(random.random()) 8 print(random.randint(1,5))#randint 隨機整數 9 print(random.randint(1,8))10 print(random.randrange(2,3))#隨機整數不包尾數\11 12 print(random.sample(range(100),4))#成在產生器中隨機出數字13 14 str_list=string.digits+string.ascii_uppercase+string.ascii_lowercase#將ascii中的字串賦於變數15 print(‘‘.join(random.sample(str_list,4)))#直接用join拼接16 print(random.sample(str_list,1))17 #str_int=string.a18 # for i in range(0,255):19 #     print(‘編號:%s %s‘%(i,chr(i)))
View Code

 

python第十七天---時間模組、random模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.