Python数据结构练习
1. 给定列表L,如[2,5,3,8,10,1],对其进行升序排序并输出。
代码: list = [2,5,8,10,1]
print(list)
list.sort()
print(list)
2.给定字符串s,如‘123456’,将其逆序输出,使用切片
代码: str = '123456'
print(str[::-1])
3.给定字典dict,如{‘a’:1,'b':'2','c':'3'},分别输出它的key与value。向其中插入字典{'d':4},并输出新的zid
代码:
dict = {"a":"1","b":"2","c":"3"}
print(dict.keys())
print(dict.values())
dict.setdefault("d")
dict["d"] = "4"
print(dict)
4.求出100以内的所有素数
for i in range(2,101):
fg = 0
for t in range(2,(i-1)):
if(i % t == 0):
fg = 1
if(fg == 0):
print(i)