Python(4)操作列表

云计算 waitig 633℃ 百度已收录 0评论

4.1遍历整个列表

使用for循环

magicians=['a','b','c']
for magician in magicians:
    print(magician.title()+",that was a great trick!")
    print("I can't wait to see you,"+magician.title()+".\n")
print("Thank you,everyone!\n")

4.3创建数值列表

range函数

for value in range(1,6):
    print(value)
numbers=list(range(1,6))        #转为列表
print (numbers)
even_numbers=list(range(1,10,2))#打印奇数
print (even_numbers)
print "\n"

squares=[]
for Value in range(1,11):
    square=Value**2             #计算平方
    squares.append(square)      #附加到列表squares末尾
print(squares)
print "\n"

对列表进行简单的统计计算

digits=range(1,20,3)
print digits
print min(digits)
print max(digits)
print sum(digits)
print "\n"

列表解析

squares=[Value**2 for Value in range(1,11)]
print(squares)
print "\n"

练习

#
for Int in range(1,20):
    print Int
print "\n"
'''
#
for Int in range(1,1000000):
    print Int
'''
#
count=list(range(1,1000000))
print min(count)
print max(count)
print sum(count)
print "\n"

#
INT=list(range(1,20,2))
print INT
for Int in range(1,20,2):
    print Int
print "\n"

#
count=[value**3 for value in range (1,10)]
print count

#
counts=[]
for value in range(1,10):
    count=value**3
    counts.append(count)
print counts

本文由【waitig】发表在等英博客
本文固定链接:Python(4)操作列表
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)