标签:style blog color 使用 sp for strong on 数据
Python程序员通常把内置函数称为BIF,print() BIF的作用是吧消息显示到标准输出(通常是屏幕)
1 print("You can experiment with code within IDEL‘s shell Coool, eh?") 2 if 43>42 3 print("Don‘t panic!")
要遵循的四个步骤:
Python的变量标识符没有类型
movies =["The Holy Grail","The Life of Brian","The meaning of Life" ]
print(movies[1]) #堆栈中的第一个槽编号为0
可使用print() BIF再屏幕上显示一个列表,使用len()BIF得出列表中有多少个数据项
>>> cast =["Cleese",‘Palin‘,‘Jones‘,"Idle"] >>> print(cast) [‘Cleese‘, ‘Palin‘, ‘Jones‘, ‘Idle‘] >>> print(len(cast)) 4 >>> print(cast[1]) Palin
再列表末尾增加一个数据项使用方法append(),从列表末尾删除数据使用方法(pop()),在列表末尾增加一个数据项集合extend();
1 >>> cast.append("Gilliam") 2 >>> print(cast) 3 [‘Cleese‘, ‘Palin‘, ‘Jones‘, ‘Idle‘, ‘Gilliam‘] 4 >>> cast.pop() 5 ‘Gilliam‘ 6 >>> print(cast) 7 [‘Cleese‘, ‘Palin‘, ‘Jones‘, ‘Idle‘] 8 >>> cast.extend(["Gilliam","Chapman"]) 9 >>> print(cast) 10 [‘Cleese‘, ‘Palin‘, ‘Jones‘, ‘Idle‘, ‘Gilliam‘, ‘Chapman‘]
删除特定的数据项,在某些特定的位置插入一个元素
>>> cast.remove("Chapman") >>> print(cast) [‘Cleese‘, ‘Palin‘, ‘Jones‘, ‘Idle‘, ‘Gilliam‘] >>> cast.insert(0,"Chapman") >>> print(cast) [‘Chapman‘, ‘Cleese‘, ‘Palin‘, ‘Jones‘, ‘Idle‘, ‘Gilliam‘]
在之前的movies每一项前增加时间
代码一:
>>> movies.insert(1,1975)#用合适的列表方法增加需要的额外数据 >>> movies.insert(3,1979) >>> movies.append(1983) >>> print(movies) [‘The Holy Grail‘, 1975, ‘The Life of Brian‘, 1979, ‘The meaning of Life‘, 1983]
方法二:
>>> movies=[‘The Holy Grail‘, 1975, ‘The Life of Brian‘, 1979, ‘The meaning of Life‘, 1983]#从头开始重新创建列表 >>> print(movies) [‘The Holy Grail‘, 1975, ‘The Life of Brian‘, 1979, ‘The meaning of Life‘, 1983]
for迭代处理数据(for循环是可伸缩的,适用于任意大小的列表)
>>> for each_flick in fav_movies:#冒号放在列表名后边,指示列表处理代码的开始 print(each_flick) """注意前面有TAB"""
while和for的对比,完成相同的工作
while:
>>> count=0 >>> while count<len(movies): print(movies[count]) count=count+1
for:
>>> for each_item in movies: print(each_item)
列表可以存放混合类型的数据,也包含任何列表
movies=[‘The Holy Grail‘, 1975, "Terry Jones & Terry Gilliam",91,["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle","Terry Jones"]]] print(movies[4][1][3])
用for循环来处理这个列表
>>> movies=[‘The Holy Grail‘, 1975, "Terry Jones & Terry Gilliam",91,["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle","Terry Jones"]]] >>> print(movies[4][1][3]) Eric Idle >>> print(movies) [‘The Holy Grail‘, 1975, ‘Terry Jones & Terry Gilliam‘, 91, [‘Graham Chapman‘, [‘Michael Palin‘, ‘John Cleese‘, ‘Terry Gilliam‘, ‘Eric Idle‘, ‘Terry Jones‘]]] >>> for each_item in movies: print(each_item) The Holy Grail 1975 Terry Jones & Terry Gilliam 91 [‘Graham Chapman‘, [‘Michael Palin‘, ‘John Cleese‘, ‘Terry Gilliam‘, ‘Eric Idle‘, ‘Terry Jones‘]]
if...else...模式:
if 某个条件满足 :
"true"组
esle:
"false"组
isinstance() 用来检查某个特定标识符是否包含某个特定类型的数据
for each_item in movies: if isinstance(each_item,list): for nested_item in each_item: if isinstance(nested_item,list): for deeper_item in nested_item: print(deeper_item) else: print(nested_item) else: print(each_item) The Holy Grail 1975 Terry Jones & Terry Gilliam 91 Graham Chapman Michael Palin John Cleese Terry Gilliam Eric Idle Terry Jones
在Python中创建一个函数
定义函数的标准形式:
def 函数名(参数):
函数代码组
>>> def print_lol(the_list): for each_item in the_list:#递归处理任意深度的嵌套列表 if isinstance(each_item,list): print_lol(each_item) else: print(each_item) >>> print_lol(movies)
Python3默认递归深度不能超过100
标签:style blog color 使用 sp for strong on 数据
原文地址:http://www.cnblogs.com/cfcf0517/p/4157855.html