# 1.集合 *** 作: set_data1 = {1, 2, 3, 4} set_data2 = {2, 3, 5} # 求两个集合的交集 set_data = set_data1.intersection(set_data2) print(set_data) # 求两个集合的并集 set_data = set_data1.union(set_data2) print(set_data) # 求两个集合的差集 set_data = set_data1.difference(set_data2) print(set_data) # 2.运算符: # a.逻辑运算符的使用: # 给出两个条件: 分别使用 and 和 or来连接,输出结果 condition1 = 1 < 10 condition2 = 1 > 10 print(condition1 and condition2) print(condition1 or condition2) # b.成员运算符的使用: # 定义一个变量:如果变量为空,打印此变量为None,不为空打印此变量不为空 data = "" if data is None: print("None") else: print("此变量不为空") # c.身份运算符的使用: # 定义一个列表:然后给定一个值,如果这个值在列表中,打印列表包含这个值, # 如果不在列表中,打印列表不包含这个值 list_data = [1, 2, 3] if 4 in list_data: print("列表包含这个值") else: print("列表不包含这个值") # 3.条件控制语句: # 给定学生的信息:dict_data = {"张三": 21, "李四": 20, "王五": 19, "赵六":18, "刘七": 19, "张麻子": 20, "耿耿": 20, "余淮": 19} # 21级需要学习C语言 # 20级需要学习Python # 19级需要学习Java # 18级需要学习Go # 请统计出学习C语言,学习Python,学习Java,学习Go的人数 dict_data = {"张三": 21, "李四": 20, "王五": 19, "赵六": 18, "刘七": 19, "张麻子": 20, "耿耿": 20, "余淮": 19} c_people = 0 p_people = 0 j_people = 0 g_people = 0 for grade in dict_data.values(): if grade == 21: c_people += 1 elif grade == 20: p_people += 1 elif grade == 19: j_people += 1 elif grade == 18: g_people += 1 print("c语言:", c_people, ";python:", p_people, ";java:", j_people, ";go:", g_people) # 4.for循环: # a.定义一个字典: dict_data = {"张三": 21, "李四": 20, "王五": 19, "赵六": 18, "刘七": 19, "张麻子": 20, "耿耿": 20, "余淮": 19} # 打印出字典里面的每一个key for name in dict_data: print(name, end="t") # 打印出字典里面的每一个value print() for grade in dict_data.values(): print(grade, end="t") print() # 打印出字典里的每一对key和value for people in dict_data.items(): print(people, end="t") print() # b.定义一个元组,请打印出元组的下标和下标对应的元素的值 tuple_data = (1, 2, 3, 4, 5, 6) for index, num in enumerate(tuple_data): print(index, num)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)