赞
踩
目录
(5)给定列表变量ls, ls.pop(i)方法的功能是什么?
(7)给定字典变量dicts, dicts.items()方法的功能是什么?
(1)下面程序的输出结果是“found it! 44",[代码]处应补充的语句是什么?
(3)下面程序的功能是:输入以逗号分隔的一组单词,判断是否有重复的单词。如果存在重复的单词,打印”有重复单词“,退出;如果无重复的单词,打印”没有重复单词“【代码】处应补充的语句是什么?
(2)使用字典描述学生信息,包括no(学号),name(姓名),score(成绩)。使用列表储存信息,并根据给定学生姓名查找学生信息。
(3)使用input函数,输入若干单词,然后按字典的顺序输出单词(即使某个单词出现多次,也只输入一次)
(4)使用元组创建一个储存python关键字的对象,并检查指定的单词是不是python的关键字
1.b dicts.pop()这个应该是写错了,应该是dicts.pop(key),此方法是从字典中移除指定键并返回相应的值。因此选b
2.a
3.d 我选的b,b选项是错的,因为里面既有整数也有字符串,所以为集合
4.c
5.d已知列表[1,2,3,2,3,4,5]中的数字2和数字3都重复了,当我们将列表转换为集合时,重复的数字2和3只会出现一次,因此转换为集合后,内容是{1,2,3,4,5},len()是计算字符串长度,所以结果为5
6.b 7.d 8.c 9.d
10.d 这道题我也做错了,b不是一个变量或字符串,而是作为字典中的键来使用。因为在代码中没有定义变量b的值,所以会导致KeyError(KeyError:'b')并触发异常
11.a get()方法用于获取指定键的值,如果键不存在,则返回默认值。字典d中并没有键'cake',因此会返回默认值
12.b
13.d d选项为啥错了,因为序列类型是一维的有序数据类型,元素之间存在先后关系,通过序号访问
14.b
列表:用 [] 标记,用list()可以将元组转换为列表,也可以直接用 [] 创建。
元组:用 () 标记,用tuple()可以将其他可迭代对象(如列表、字符串等)转换为元组。
字典:用 {} 标记,用dict()创建字典函数。
列表与元组非常相似,元组是固定的,元组中的元素值不能被修改,如果想要修改元素值,可以将元组转换为列表,修改完毕后,将列表转换为元组,列表是可以改变的,可以增加元素,也可以减少元素 总的来说元组不可以修改,列表可以修改
特点:
1.字典中的值没有特殊的顺序
2.键可以是数字、字符串以及元组
3.字典中的元素(键值对)是无序的
字典的操作方法:
dicts.keys()、dicts.values()、dictsitems()、dicts.clear()、dicts.copy()
用for循环遍历列表
#ex0503.py lst=['primary schoo1','secondary schoo1','high school ','college'] for item in lst: print (item, end=",")
用while循环也可以遍历列表但需要先获取列表的长度,将获取的长度作为循环条件,这个书上有了我就不写了
lst=list(range(2,21,3)) i=0 result=[] while i<len(lst): result.append(lst[i]*lst[i]) i+=1 print (result)
用for循环遍历元组
my_tuple = (1, 2, 'apple', True) for item in my_tuple: print(item)
使用索引和len()函数:
my_tuple = (1, 2, 'apple', True) for i in range(len(my_tuple)): print(my_tuple[i])
使用enumerate()函数:用 enumerate()函数获取元组中的元素和对应的索引值.
my_tuple = (1, 2, 'apple', True) for index, value in enumerate(my_tuple): print(f"Index {index}: {value}")
ls.pop(i)
返回列表第i行元素并删除该元素
将列表转换为元组:tuple()
将元组转换为列表:list()
返回所有的键值对
ls=[12,33,44,55,66] for i in ls: if i==44: print("found it! i=",i) [代码] else: continue
break
**程序就输出了一个44,这说明程序循环到44的时候,打印出44后就退出来了,所以缺的是break.
x=[90,80,70] y=("Rose","Mike","John") z={} for i in range(len(x)): z[x[i]]=y[i] print(z)
{'90':"Rose",'80':"Mike",'70':"John"}
从代码中我们可以看出x是一个列表[],y是一个元组(),z是一个字典{};此程序用了一个for循环将y中的每个值赋值给z,90所对应的是Rose,80所对应的是Mike,70所对应的是 John,而字典的一般格式是{'id':"100",'name':"3",'flag':"red"}(例子),所以答案是{'90':"Rose",'80':"Mike",'70':"John"}
txt=input("请输入一组单词,以逗号分隔:") ls=txt.split(',') #用,分隔开 words=[] #words是一个空列表,为了检查单词是否重复,我们要给空列表里面填写东西来检查是否有单词重复 for word in ls: if word in words: print("有重复单词") break else: [代码] else: print("没有重复单词")
words.append(word)
import random import string def generate_verification_code(length=4): characters=string.ascii_letters+string.digits code=''.join(random.choices(characters,k=length)) return code verification_code=generate_verification_code(4) print(f"verification_code:",verification_code) #random模块是包含了一系列用与生成随机数的函数包括: random.randint(a,b)和random.choice(list) #random.randint(a,b)用于生成一个介于a到b的随机整数(包括a和b) #random.choice(list)从列表随机选择一个元素 #import string:这是导入python的标准库string模块,这个模块包含一些与字符串相关的常量,其中最重要的就是: string.ascii_letters和string.digits #string.ascii_letters 包含所有的ASCII字母(大写和小写) #string.digits 包含所有的数字 #''.join()用于将列表中的字符连接成一个字符串,''作为分隔符,所以字符之间不存在任何额外的字符或空格
student1={'no':"001",'name':'Bob','score':94} student2={'no':"002",'name':'Alice','score':86} student3={'no':"003",'name':'Free','score':97} student_list=[student1,student2,student3] def Search_student(name,student_list): for student in student_list: if student["name"]==name: return student return None search_student="Alice" found_student=Search_student(search_student,student_list) if found_student: print(f"学号:{found_student['no']}") print(f"名字:{found_student['name']}") print(f"分数:{found_student['score']}") else: print("没有查到有关信息") #print(f"学号:{found_student['no']}")这个里面就不能用双引号,不然编译器不知道在哪里结束,所以用双引号定义字符串后里面不能再用双引号,可以用单引号来进行替换。
def sort_word(): words=input("请输入单词以空格为分隔:").split() unique_words=list(set(words)) #将words转换为集合,集合没有重复的元素,相同的只会出现一次,然后用list将集合转换为列表 sorted_unique_words=sorted(unique_words) #sort函数排序 print("按字典的顺序排序后的单词:") for word in sorted_unique_words: print(word) sort_word() #调用sort函数
def examine(): python=("False","None","True","and","as","assert","break","class","continue","def","del","elif","else","except","finally","for","from","global","if","import","in","is","lambda","nonlocal","not","or","pass","raise","return","try","while","with","yield") words=list(python) word=input("please enter word:") if word in words: print("此单词是python的关键字") else: print("此单词不是python的关键字") examine()
txt=input("请输入一组单词,以逗号分隔:") ls=txt.split(',') #用,分隔开 words=[] for word in ls: if word in words: pass else: words.append(word) print(words)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。