标签:切片 pytho result min done 遍历 ack list python
fruits = [‘apple‘,‘banana‘,‘pear‘]
for fruit in fruits:
print(fruit) #缩进表示在循环体内
print(‘done!‘) #不缩进是循环体外,避免缩进错误
numbers = list(range(1,6))
print(numbers) #result: [1,2,3,4,5]
even_numbers = list(range(2,11,2)) #左闭,右开,循环加2
print(even_numbers) #result: [2,4,6,8,10]
square = value**2 #同理,立方就是value**3
digits = [1,2,3,4,5,6,7,8,9,0]
min(digits) #0
max(digits) #9
sum(digits) #45
squares = [value**2 for value in range(1,11)] #[1,4,9,16,25,36,49,64,81,100]
list[0:3]
list[:3] #与上行等价,取列表索引0,1,2列表片段
list[3:] #从索引3到列表末尾
list[-3:] #最后三位
list2 = list1[:] #不可以是list2=list1,这样只是将两个列表关联起来,其实只是一个列表
标签:切片 pytho result min done 遍历 ack list python
原文地址:https://www.cnblogs.com/cnyws/p/14889807.html