程序媛计划——python初级class5~13
列表和元組都是可迭代對(duì)象(可以用于for in)
列表 []
#添加列表元素:
list.append(argu)
#修改列表:
list[2] = 2017
?
#刪除列表元素
Del list[2]
?
#獲取列表長(zhǎng)度
len(list)
#組合列表
>>>[1,2,3]+[4,6]
[1,2,3,4,6]
#定義有周期性的列表
[100,]*4==[100,100,100,100]
?
元組()
特點(diǎn):元組中的元素不能修改
#創(chuàng)建元組
Tup1 = (‘a(chǎn)pple’,’ip’,2016,2017)
tup2=‘x’,’y’,’z’
#創(chuàng)建空元組
Tup = ()
#創(chuàng)建只含有一個(gè)元素的元祖
Tup =(1,)
?
#對(duì)元組進(jìn)行連接組合
tup3 = tup1+tup2
#刪除整個(gè)元組(不能刪除元組中的元素)
Del tup3
?
#獲取元組長(zhǎng)度
len(tuple)
?
#獲取有周期性的元組
(100,)*4==(100,100,100,100)
?
?
字典{}
特點(diǎn):key必須是不變對(duì)象,如str,num或者tuple;同一個(gè)key不能出現(xiàn)兩次,如果定義時(shí)有兩個(gè)相同的key,相應(yīng)的后一個(gè)value會(huì)覆蓋前一個(gè)
#define
D = dict(name =‘bob’,age =20)
D ={‘a(chǎn)pple’:’蘋果’,‘banana‘:’葡萄‘}#key是不可變對(duì)象字符串,要帶引號(hào)
?
#通過key獲取value
d= {‘name’:’Jane’, ‘sex’:’female’}
print d[‘sex’]
?
#修改value
d[‘name’] = ‘bob’
?
#添加新key-value
d[‘height’] = 163
?
#del key-value
d[‘sex’] = ‘female’
?
#del all dict
d.clear() #刪除后d變成空字典
del d #字典被徹底刪除,再使用時(shí)會(huì)報(bào)錯(cuò)
?
#返回字典中的所有鍵key/value
dict.keys()
dict.values()
?
Class10~13
時(shí)間和日期
#返回1970年1.1午夜到現(xiàn)在的時(shí)間秒數(shù)
Import time
Print time.time()
#返回現(xiàn)在從年到秒的時(shí)間
Print time.localtime(time.time())
#返回有一定格式的當(dāng)前從年開始到秒到時(shí)間
time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())#前面的格式化字符串取的是time.localtime()函數(shù)的中時(shí)間日期格式化符號(hào)對(duì)應(yīng)的數(shù)據(jù);前面這些Ymd……是py中時(shí)間日期格式化符號(hào)
?
#返回月歷
import calendar
print calendar.month(2017,9)
Console:
?? September 2017
Mo Tu We Th Fr Sa Su
?? ? ? ? ? ? 1? 2? 3
?4? 5? 6? 7? 8? 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
?
#返回當(dāng)天所屬星期
import time
print time.strftime('%a',time.localtime())
?
?
?
函數(shù)()
函數(shù)中return是函數(shù)結(jié)束的標(biāo)志,所以執(zhí)行了一個(gè)return 就直接結(jié)束不會(huì)再執(zhí)行下面代碼的return了。
函數(shù)不一定要有return語句
?
轉(zhuǎn)載于:https://www.cnblogs.com/IcarusYu/p/7482896.html
總結(jié)
以上是生活随笔為你收集整理的程序媛计划——python初级class5~13的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 修改hosts 流畅使用coursera
- 下一篇: 复变函数在计算机科学的应用,[2018年