數(shù)據(jù)類型
整數(shù)
浮點(diǎn)數(shù)
字符串
布爾值
空值
有序列表(list(可改變),tuple(不可改變))
字符串編碼
中文編碼,使用encode('utf-8')
英文編碼,使用encode('ascii')
格式化
print('我叫%s,今年%d歲。' % ('小明',20))
print('今年比去年上升了%.1f%%' % 1.5555)
有序列表(類似數(shù)組)
list = []
tuple = ()
條件判斷
age = 20
if age < 18:
print('')
elif age < 10:
print('')
else:
print('')
循環(huán)
for in
while
字典dictionary
dict = {'summer':18,'php':3}
dict['python'] = 100
'test' in dict
False
要保證hash的正確性,作為key的對(duì)象就不能變。在Python中,字符串、整數(shù)等都是不可變的,因此,可以放心地作為key。而list是可變的,就不能作為key;
SET
要?jiǎng)?chuàng)建一個(gè)set,需要提供一個(gè)list作為輸入集合:
s = set([1,2,3])
無(wú)序和無(wú)重復(fù)元素的集合,因此,兩個(gè)set可以做數(shù)學(xué)意義上的交集、并集等操作:
s1 = set([1,2,3])
s2 = set([2,3,4])
// 交集
s1 & s2
{2,3}
// 并集
s1 | s2
{1,2,3,4}
可變參數(shù)
參數(shù)前加*號(hào)
def sum(*number):
result = 0
for num in number:
result = result + num * num
return result
print(sum(*[1,2,3]))
關(guān)鍵字參數(shù)
參數(shù)前加**號(hào)
def test(name,age,**list):
return name,age,list
city = {'key':'wuhan','key2':'shiyan'}
print(test(1,2,**city))
命名關(guān)鍵字參數(shù)
定義命令關(guān)鍵字參數(shù)前必須要有*號(hào)
def param(name,age,*,city='shiyan',num):
return name,age,city,num
print(param('gsy',18,num=2))
def ok(name,age,*key,city,num):
return name,age,key,city,num
print(ok('gsy',20,city='wuhan',num=2))