列表

作者:追风剑情 发布于:2017-12-4 21:11 分类:Python

示例

# -*- coding: cp936 -*-
print '列表'
edward = ['Edward Gumby', 42]
john = ['John Smith', 50]
database = [edward, john]
print database

print '通用序列操作: 索引(indexing)、分片(slicing)、加(adding)、乘(multiplying)'
greeting = 'Hello'
print greeting[0]
#可以用负数作为索引
print greeting[-1]
print greeting[-2]
print '字符串字面值(量)'
print 'Hello'[0]

print "分片"
numbers = [1,2,3,4,5,6,7,8,9,10]
print numbers[3:6]
print numbers[0:2]
#从后往前计数
print numbers[-3:-1]
#输出-3后面的所有元素
print numbers[-3:]
#输出前面三个
print numbers[:3]
#输出全部
print numbers[:]
print '显示指定步长(第3个参数)'
print numbers[0:10:1]
print numbers[0:10:2]
print numbers[::4]
#步长为负时从右往左提取元素
print numbers[::-4]
print numbers[10:0:-4]

print '相同类型序列相加'
print [1,2,3] + [4,5,6]
print 'Hello. ' + 'world!'

print '相乘'
print 'python' * 5
print [42] * 10
#创建空列表
sequence = [None] * 10
print sequence

print '包含检查'
permissions = 'rw'
print 'w' in permissions
print 'x' in permissions
user = ['mlh', 'foo', 'bar']
print raw_input('Enter your name: ') in user
subject = '$$$ Get rich now!!! $$$'
print '$$$' in subject

#检查列表中是否包含列表
database = [
    ['albert', '1234'],
    ['dilert', '4242'],
    ['smith', '7524'],
    ['jones', '9843']
]
username = raw_input('User name: ')
pin = raw_input('PIN code: ')
if [username, pin] in database: print 'Access granted'

print 'len函数、min函数、max函数'
numbers = [100, 34, 678]
print 'len=' + `len(numbers)`
print 'max=' + `max(numbers)`
print 'min=' + `min(numbers)`
print max(2,3)
print min(9,3,2,5)

运行测试

111111.png

标签: Python

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号