1欲侮,數(shù)據(jù)類型
有整數(shù),浮點(diǎn)數(shù)肋联,字符串威蕉,布爾值,空值橄仍,變量韧涨,常量
2,格式化
采用的格式化方式和C語(yǔ)言是一致的侮繁,用%實(shí)現(xiàn)虑粥,舉例如下:
輸入:'Hello, %s' % 'world'
輸出:'Hello, world'
輸入:'Hi, %s, you have $%d.' % ('Michael', 1000000)
輸出:'Hi, Michael, you have $1000000.'
格式化整數(shù)和浮點(diǎn)數(shù)還可以制定是否補(bǔ)0和整數(shù)與小數(shù)的位數(shù):
輸入:'%2d-%003d' % (3,1)
輸出:‘ 3-001’
輸入:'%.2f'% 3.1415926
輸出:'3.14'
如果不確定應(yīng)該用什么,%s永遠(yuǎn)起作用宪哩,會(huì)把任何數(shù)據(jù)類型轉(zhuǎn)換為字符串:
輸入:'name:%s娩贷,age:%s' % (Alex,1)
輸出:‘ name:Alex,age:1’
3锁孟,列表數(shù)組:list
例如:
輸入:classmates=['Alex0','Alex1','Alex2']
輸入:classmates
輸出:['Alex0','Alex1','Alex2']
獲取長(zhǎng)度:
輸入:len(classmates)
輸出:3
獲取數(shù)組元素
輸入:classmates[0]
輸出:'Alex0'
倒數(shù)著獲取數(shù)組元素
輸入:classmates[-1]
輸出:倒數(shù)第一個(gè)元素 'Alex2'
往數(shù)組末尾添加元素
輸入:classmates.append['Alex3']
輸入:classmates
輸出:['Alex0','Alex1','Alex2','Alex3']
往數(shù)組指定位置插入元素
輸入:classmates.insert[0彬祖,'Alex']
輸入:classmates
輸出:['Alex','Alex0','Alex1','Alex2','Alex3']
刪除末尾的元素
輸入:classmates.pop()
輸入:classmates
輸出:['Alex','Alex0','Alex1','Alex2']
刪除指定位置的元素
輸入:classmates.pop(0)
輸入:classmates
輸出:['Alex0','Alex1','Alex2']
在數(shù)組元素中可以是數(shù)組
輸入:s = ['python', 'java', ['asp', 'php'], 'scheme']
輸入:s[2]
輸出:['asp','php']
4茁瘦,元組
一旦初始化就不能修改
classmates=('Alex0','Alex1','Alex2')
當(dāng)元組只有一個(gè)元素時(shí),要添加逗號(hào)
輸入:t=(1,)
輸入:t
輸出:(1,)