python之路 第二篇 數據類型詳解及其方法

字符串app

#做用:描述名字,性別,國籍,地址等信息

#定義:在單引號\雙引號\三引號內,由一串字符組成
name='Matthew'

#優先掌握的操做:
#一、按索引取值(正向取+反向取) :只能取
#二、切片(顧頭不顧尾,步長)
#三、長度len
#四、成員運算in和not in

#五、移除空白strip
#六、切分split
#七、循環
 

 用法實例iphone

#strip
name = '**Matthew*'
print(name.strip(*))
print(name.lstrip(*))
print(name.rstrip(*))

#lower,upper
name = 'Matthew'
print(name.lower())
print(name.upper())

#startwith,endwith
name = 'Matthew'
print(name.endwith('w'))
print(name.startwith('M'))

#format
res = '{},{},{}'.format('Matthew',18,'male')
res = '{0},{1},{0}'.format('Matthew',18,'male')
res = '{name},{age},{sex}'.format(name='Matthew',age=18,sex='male')

#split
name = 'Matthew_male'
name.split('_')

#join
tag = ''
print(tag.join({'Matthew','say','hello','world'})

#replace
msg = 'Alex is a boy'
print(msg.replace('boy','girl'))


 列表spa

#做用:存儲數據,進行數據迭代
#定義:[]內能夠有多個任意類型的值,逗號分隔

name = ['matthew','egon','alex']

#掌握的操做

1.取值
2.切片
3.長度
4.成員運算
5.追加
6.刪除
7.循環

練習code

 
 
list1 = ['matthew','egon','alex']#增
>>>new_list=list1.append('小馬哥')
new_list = ['matthew','egon','alex','小馬哥']#添加到末尾
#刪
>>>new_list = list1.pop() #從末尾刪除1個值並有返回值
‘小馬哥’

#改
>>>new_list[0] = 'matt'           #第一個元素值改成‘matt’
#查:

按索引取值
>>>list1[0]
'matthew'
從頭至尾取值,步長爲2
>>>list1[0::2]
‘matthew’,'alex'

#反轉列表
>>>list1[::-1]
‘alex','egon','matthew'
#求列表長度(通常用於遍歷條件)
>>>len(list1)
3

補充:

 

 元組orm

#做用:不可變的列表,(能夠用做字典的key),主要用來讀
 
 msg=(1,2,3,4)

#須要掌握的操做:
1.按索引取值(顧頭不顧尾)
2.切片
3.成員運算 in和not in
4.循環

練習blog

age = [('matthew',18),('egon','38'),('alex','38')]
#遍歷出每一個人的年齡
for res in age:
    print(res[1])

 

字典索引

#做用:用於存儲數據,讀取速度比列表快,存儲方式爲key-value方式存取

ps:key必須爲不可變類型(字符串,數字,元組)

price = {
'星冰樂':28
'iphone':5000
'蘭博基尼模型':200
}

#須要掌握的操做:
1.按key存取數據
2.字典長度(key的個數)
3.成員運算 in 和not in
4.刪除
5.循環

練習ip

相關文章
相關標籤/搜索