Python列表、元组练习
#通用序列操作:分片、索引、加、乘、成员资格、长度及最值
#成员资格
pwd = 'dxrw'
print('rw' in pwd)
user = ['zhangsan','26','123456']
database = [
['zhangsan','1234'],
['lisi','235'],
['wangwu','4567']
]
Flag = True
while(Flag):
username = input("input your name:")
pwd = input("input your pwd:")
if [username,pwd] in database:
print("logo in")
break
else:
print("failed")
#长度及最值
num = [1,2,3,4,5,6,7,8]
print("len:",len(num))
print("min:",min(num))
print("max:",max(num))
#list函数
print(list("hello world"))
#列表的基本操作:元素赋值、元素删除、分片赋值、列表方法
list1= [1,2,34]
list1[2] = 0
print("赋值:",list1)
del list1[2]
print("删除",list1)
name = list("Hello World")
name[5:]=list("1234")
print("分片修改:",name)
num1 = [1,2,3,4,5,6,7,8,9]
num1[1:1] = [100,200,300]
print("分片插入:",num1)
num2 = [1,2,3,4,5,6,7,8,9]
num2[1:4] = []
print("分片置空:",num2)
#列表方法
lst = [1,2,3,4,4,5,6,7,9,7]
lst_extend= list("hello")
#lst_extend = [2,34,555]
lst.append(5)
lst.append(6)
lst.extend(lst_extend)
print("append:",lst)
print("conut:",lst.count(5))
print("尾部追加:",lst)
print("索引:",lst.index(5))
lst.insert(4,"heheh")
print("插入:",lst)
print("移除last:",lst.pop())
print("移除last:",lst.pop(0))
print("move:",lst)
x = [1,2,3,4,5,6,7,66,6,665,4436]
x.append(x.pop())
print("append入栈pop出栈:",x)
x.remove(2)
print("remove:",x)
x.reverse()
print("reserver:",x)
x.sort()
print("sort:",x)
y=x.sort()
print("x:",x)
print("y:",y)
ab=["hell","ajdlasg","fadfasgasgas","fa"]
ab.sort(key=len)
print("按长度排序:",ab)
ab.sort(reverse=True)
print("反转:",ab)
#元组不可变序列
print(3*(40+2))
print(3*(40+2,))
print("把序列转换为元组",tuple([1,3,4,5,6]))
print(tuple((1,23,44,5,6)))
print(tuple("helloafadfad"))
"""
C:\python3.7\python.exe D:/Python-Test/qiubai/qiubai/Test.py
True
input your name:zhangsan
input your pwd:1234
logo in
len: 8
min: 1
max: 8
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
赋值: [1, 2, 0]
删除 [1, 2]
分片修改: ['H', 'e', 'l', 'l', 'o', '1', '2', '3', '4']
分片插入: [1, 100, 200, 300, 2, 3, 4, 5, 6, 7, 8, 9]
分片置空: [1, 5, 6, 7, 8, 9]
append: [1, 2, 3, 4, 4, 5, 6, 7, 9, 7, 5, 6, 'h', 'e', 'l', 'l', 'o']
conut: 2
尾部追加: [1, 2, 3, 4, 4, 5, 6, 7, 9, 7, 5, 6, 'h', 'e', 'l', 'l', 'o']
索引: 5
插入: [1, 2, 3, 4, 'heheh', 4, 5, 6, 7, 9, 7, 5, 6, 'h', 'e', 'l', 'l', 'o']
移除last: o
移除last: 1
move: [2, 3, 4, 'heheh', 4, 5, 6, 7, 9, 7, 5, 6, 'h', 'e', 'l', 'l']
append入栈pop出栈: [1, 2, 3, 4, 5, 6, 7, 66, 6, 665, 4436]
remove: [1, 3, 4, 5, 6, 7, 66, 6, 665, 4436]
reserver: [4436, 665, 6, 66, 7, 6, 5, 4, 3, 1]
sort: [1, 3, 4, 5, 6, 6, 7, 66, 665, 4436]
x: [1, 3, 4, 5, 6, 6, 7, 66, 665, 4436]
y: None
按长度排序: ['fa', 'hell', 'ajdlasg', 'fadfasgasgas']
反转: ['hell', 'fadfasgasgas', 'fa', 'ajdlasg']
126
(42, 42, 42)
把序列转换为元组 (1, 3, 4, 5, 6)
(1, 23, 44, 5, 6)
('h', 'e', 'l', 'l', 'o', 'a', 'f', 'a', 'd', 'f', 'a', 'd')
"""
生活可以漂泊,可以孤独,但灵魂不能没有归处。

浙公网安备 33010602011771号