-
if?語句【每日一個(gè)知識(shí)點(diǎn)第350期-Python】
if?語句 也許最有名的是?if?語句。例如: >>> x = int(input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print('Negative changed to zero') ... elif x == 0: ... print('Zero') ... elif x == 1: ... print('Sing…
-
字符串日期格式化為秒數(shù),返回浮點(diǎn)類型【每日一個(gè)知識(shí)點(diǎn)第348期-Python】
字符串日期格式化為秒數(shù),返回浮點(diǎn)類型: expire_time = "2013-05-21 09:50:35" d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S") time_sec_float = time.mktime(d.timetuple()) print time_sec_float
-
獲取日期差【每日一個(gè)知識(shí)點(diǎn)第346期-Python】
oneday = datetime.timedelta(days=1) #今天,2014-03-21 today = datetime.date.today() #昨天,2014-03-20 yesterday = datetime.date.today() - oneday #明天,2014-03-22 tomorrow = datetime.date.today() + oneday #獲取今天零點(diǎn)的時(shí)間,2014-03-21 00:00:00 today_zero_time = datet…
-
獲取格式化的時(shí)間【每日一個(gè)知識(shí)點(diǎn)第344期-Python】
可以根據(jù)需求選取各種格式,但是最簡單的獲取可讀的時(shí)間模式的函數(shù)是asctime():1.日期轉(zhuǎn)換為字符串 首選:print time.strftime('%Y-%m-%d %H:%M:%S'); 其次:print datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S') 最后:print str(datetime.datetime.now())[:19] 2.字符串轉(zhuǎn)換為日期 expire_time = "2…
-
獲取當(dāng)前時(shí)間【每日一個(gè)知識(shí)點(diǎn)第342期-Python】
獲取當(dāng)前時(shí)間,例如: import time, datetime; localtime = time.localtime(time.time())#Local current time : time.struct_time(tm_year=2014, tm_mon=3, tm_mday=21, tm_hour=15, tm_min=13, tm_sec=56, tm_wday=4, tm_yday=80, tm_isdst=0)print “Local current time :”, loca…
-
2020年最新Python學(xué)習(xí)路線圖(內(nèi)附免費(fèi)學(xué)習(xí)視頻)
如果你想選擇一種語言來入門編程,那么Python絕對是首選! Python非常接近自然語言,精簡了很多不必要的分號和括號,非常容易閱讀理解。編程簡單直接,更適合初學(xué)編程者,讓其專注于編程邏輯,而不是困惑于晦澀的語法細(xì)節(jié)上,比起JAVA、C#和C/C++這些編程語言相對容易很多。 因此,即使是非計(jì)算機(jī)專業(yè)或者沒有基礎(chǔ)的小白,也能分分鐘入門。那么,Python到底該怎么學(xué)?從哪里入手呢?下面由馬哥教育資深團(tuán)隊(duì)為初級入門的小伙伴提供2019年最新Python學(xué)習(xí)路線圖,還為小伙伴們免費(fèi)提供學(xué)習(xí)視頻。 …
-
字典內(nèi)置函數(shù)&方法【每日一個(gè)知識(shí)點(diǎn)第340期-Python】
字典內(nèi)置函數(shù)&方法 cmp(dict1, dict2) 比較兩個(gè)字典元素。 len(dict) 計(jì)算字典元素個(gè)數(shù),即鍵的總數(shù)。 str(dict) 輸出字典可打印的字符串表示。 type(variable) 返回輸入的變量類型,如果變量是字典就返回字典類型。 radiansdict.clear() 刪除字典內(nèi)所有元素 radiansdict.copy() 返回一個(gè)字典的淺復(fù)制 radiansdict.fromkeys() 創(chuàng)建一個(gè)新字典,以序列seq中元素做字典的鍵,val為字典所有鍵對…
-
刪除字典【每日一個(gè)知識(shí)點(diǎn)第338期-Python】
刪除字典 del dict[‘name’]; # 刪除鍵是’name’的條目dict.clear(); # 清空詞典所有條目del dict ; # 刪除詞典 例如: #!/usr/bin/Python dict = {'name': 'Zara', 'age': 7, 'class': 'First'}; del dict['name']; #dict {'age': 7, 'class': 'First'} print "dict", dict; 注意:字典不存在,del會(huì)引發(fā)一個(gè)異常
-
修改字典【每日一個(gè)知識(shí)點(diǎn)第336期-Python】
向字典添加新內(nèi)容的方法是增加新的鍵/值對,修改或刪除已有鍵/值對如下實(shí)例: #!/usr/bin/Python dict = {'name': 'Zara', 'age': 7, 'class': 'First'}; dict["age"]=27; #修改已有鍵的值 dict["school"]="wutong"; #增加新的鍵/值對 print "dict['age']: ", dict['age']; print "dict['school']: ", dict['school'];
-
訪問字典里的值【每日一個(gè)知識(shí)點(diǎn)第334期-Python】
訪問字典里的值 #!/usr/bin/Python dict = {'name': 'Zara', 'age': 7, 'class': 'First'}; print "dict['name']: ", dict['name']; print "dict['age']: ", dict['age'];