Python编程从入门到实践笔记——操作列表
#coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician in magicians : print(magician) print("hello everyone!") #for循环(冒号和缩进) for number in range(1,6): print(number) #用range()创建数字列表 #list()、range()前闭后开 numbers = list(range(1,6)) print(numbers) #range()设置步长 even_numbers = list(range(2,21,2)) print(even_numbers) #**表示乘方 squares=[] for value in range(1,51): squares.append(value**2) print(squares) #数字列表统计 digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] print("min="+str(min(digits))) print("max="+str(max(digits))) print("sum="+str(sum(digits))) #列表解析 squares_1 = [value**2 for value in range(1,10)] print(squares_1) #切片 和range()范围一样,前闭后开 #不指定起始索引,从列表开头开始 #不指定终止索引,终止于列表末尾 players=['jordan','james','magic','kobe','yao','bill'] print(players[0:3]) print(players[:1]) print(players[3:]) #打印最后三名运动员 print(players[-3:]) #复制列表:省略起始索引和终止索引 players_1=players[:] players.append('wade') players_1.append('paul') print(players) print(players_1) #players_1=players,players赋值给players_1,和Java中数组赋值一样,指向的是同一个对象,操作的是同一个对象 #元组()不可以更改其中元素的值,可以改变元组变量的值 dimensions=(100,80) print(dimensions[0]) print(dimensions[1]) dimensions=(80,80) print(dimensions[0]) print(dimensions[1])