List
li1 = []
li = ["ABC", 1, [li1]]
# li1[0] = 2
li.append("2")
print(li)
li.insert(0, 3)
print(li)
li.pop()
print(li)
li.pop(1)
print(li)
del li[-1]
print(li)
li = ["a", "b", "c", "d", "c"]
li.remove("c")
print(li)
li = ["a", "b", "c", "d", "c"]
li.sort()
print(li)
li = ["a", "b", "c", "d", "c"]
li2 = sorted(li)
print(li2)
print(li)
li.reverse()
print(li)
print(len(li))
ls = li[2:9]
print(ls)
ls = li[-3:]
print(ls)
ls = li[:]
print(id(li))
print(id(ls))
ls = ["a", "b", "c"]
print(" ".join(ls))
Tuple
t = ("3", "2")
# del t1[0]
t1 = ("aa", li)
print(t1)
li.append("e")
print(t1)
print(id(li))
li2 = li
print(id(li2))
print(sorted(t))
# t.sort()
t = ("bb")
t = ("cc")
print(t)
l1 = [n for n in range(0, 10)]
print(l1)
l2 = [chr(n) for n in range(ord("a"), ord("z")+1)]
print(l2)
l3 = [chr(n) for n in range(ord("A"), ord("Z")+1)]
print(l3)
lr = l1 + l2 + l3
print(lr)
print(random.randint(10, 100))
print(random.randrange(10, 100, 10))
print(random.sample(lr, 5))
Basic Python Learning--List&tuple
最新推荐文章于 2024-11-04 22:26:34 发布
这篇博客探讨了Python中的列表和元组操作。包括列表的创建、元素的添加与删除、排序以及切片等基本操作。还介绍了元组的不可变特性及其在编程中的应用。此外,涉及到了随机数生成、字符编码与合并的相关函数。通过实例展示了如何在实际编程中灵活运用这些数据结构和函数。
摘要由CSDN通过智能技术生成