''' name = '张三' age = '18' edu = '清华大学' x1 = f'年龄为{age}的{name}考上了{edu}' print(x1) x = int(input('请输入一个整数:')) if x % 2 == 0: if x % 10 == 0: print(f'{x}既能被2整除又能被10整除') else: print(f'{x}既能被2整除但是不能被10整除') else: print(f'{x}既不能被2整除又不能被10整除') year = int(input('请输入一个整数年份:')) result = f'{year}是闰年'if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else f'{year}是平年' print(result) if year % 400 == 0: print(f'{year}是闰年') elif year % 4 == 0 and year % 100 != 0: print(f'{year}是闰年') else: print(f'{year}不是闰年') if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0): print(f'{year}是闰年') else: print(f'{year}是平年') ''' ''' #index的用法 list1 = [1,2,6,3,7,9,4,5,0] print(list1.index(4)) print(len(list1)) #冒泡排序 list2 = [1, 200, 39, 77, 99, 33, 78, 28, 7, 556] for i in range(0, len(list2)): for j in range(0, len(list2) - 1): if list2[j] > list2[j + 1]: list2[j], list2[j+1] = list2[j+1], list2[j] print(f'冒泡排序的最终结果是:{list2}') ''' ''' list3 = [] list3.append('小明:18 ') print(list3) str1 = '123\nabc' print(str1) str2 = '123\tabc' print(str2) str3 = '123\'abc' print(str3) str4 = r'123\nabc' print(str4) ''' ''' str4 = '我是凯,我最喜欢打小鲁班,哈哈哈,来受死吧,小鲁班。' print(str4.rindex('我')) print(str4.find('我')) str1 = '11233' print(str1.isdigit()) str2 = 'acfsf...' print(str2.isalpha()) print(str2.isalnum()) str3 = 'HHGGYUHUH' str4 = 'hhhorogj' print(str3.lower()) print(str4.upper()) str5 = 'hello, world' print(str5.title()) print(str5.capitalize()) ''' ''' #正确的空集合创建方法 set1 = set() print(type(set1), set1) #集合的构造器语法 str1 = 'hello,world' set2 = set(str1) print(set2) str3 = [1,2,3,4,5,6] set3 = set(str3) print(set3) #集合的遍历 for i in set3: print(i) #集合的增 str4 = '12134, java' set4 = set(str4) set4.add('python') print(set4) set4.update('c++','java') print(set4) set4.pop() print(set4) set4.clear() print(set4) ''' ''' list1 = ['张三','张三','李四','王五','王五','李四'] result = [] for i in list1: if i not in result: result.append(i) print(result, sep='') print('*' * 90) list1 = ['张三','张三','李四','王五','王五','李四'] #列表转集合去掉重复元素 set2 = set(list1) print(set2) #再将集合转列表 list2 = list(set2) print(list2) ''' ''' for i in range(2, 101): for j in range(2, i): if i % j == 0: break else: print(f'{i}是素数') for n in range(100, 1000): i = n // 100 j = n % 100 // 10 k = n % 10 if n == i ** 3 + j ** 3 + k ** 3: print(f'{n}是水仙花数') ''' ''' for i in range(1,10): for j in range(1,10): for k in range(1, 10): if (i != j) and (i != k) and (j != k): print(i, j, k) ''' ''' t1 = (12, 'abc', 'abbu') print(type(t1)) print(len(t1)) t2 = (123,) print(t1 + t2) print(t1 * 4) #元组的直接遍历 for i in t1: print(i) #元组的间接遍历 for i in range(len(t1)): print(t1[i]) #元组的下标 print(t1[-1]) #编写一个程序,输入一串字符串,输出分别有多少个字母和数字 str1 = input('请输入一串字符串:') nums = 0 letters = 0 for i in str1: if 'a' <= i <= 'z' or 'A' <= i <= 'Z': letters += 1 elif '1' <= i <= '9': nums += 1 print(f'该字符串有{letters}个字母,有{nums}个数字') #当列表有重是可以用集合去重 s1 = [1,2,3,44,5,4,2,1,3,5] set1 = set(s1) list1 = list(set1) print(list1) ''' ''' #用max来判断出现最多的次数,用content变量来保存该元素 t1 = [1, 2, 3, 4, 5, 3, 6, 7, 9, 4, 7] #set1 = set(t1) #print(set1) max = 0 content = [] for i in t1: if max < t1.count(i): max = t1.count(i) content.clear() content.append(i) elif max == t1.count(i): content.append(i) print(f'出现次数最多的数是{content}') ''' ''' #空字典的建立 dict1 = {} print(dict1, type(dict1)) num1 = float(input('请输入第一个数:')) num2 = float(input('请输入第二个数:')) sum = num1 + num2 print(f'{num1}和{num2}的和数{sum}') num_sqrt = num1 ** 0.5 print(f'{num1}的平方根我是{num_sqrt}') a = float(input('请输入第一个数a:')) b = float(input('请输入第二个数b:')) c = float(input('请输入第三个数c:')) d = b ** 2 - 4 * a * c C = (a + b + c) / 2 S = (C*(C-a) * (C-b) * (C - c)) print(C, S) ''' ''' X = float(input('请输入摄氏度:')) Y = float(input('请输入华氏度:')) Y = (X * 1.8) + 32 X = (Y - 32) / 1.8 print(f'摄氏度转华氏度是{Y}, 华氏度转摄氏度是{X}') ''' ''' #列表推导式 list1 = [i for i in range(101) if i % 3 == 0] print(list1) #字典推导式 dict_A = {'a': 101, 'b': 99, 'c': 20.1, 'd': 160} dict_B = {i: dict_A[i] for i in dict_A if dict_A[i] > 100} print(dict_B) dict_C = {key : value for key, value in dict_A.items() if value > 100} print(dict_C) stu = [ {'name': '小明', 'age': 18, 'score': 80, 'tel': '1234', 'sex': '男'}, {'name': '小刚', 'age': 17, 'score': 70, 'tel': '12399', 'sex': '女'}, {'name': '小红', 'age': 19, 'score': 90, 'tel': '1237', 'sex': '性别不明'}, {'name': '小强', 'age': 20, 'score': 55, 'tel': '1236', 'sex': '男'}, {'name': '小李', 'age': 15, 'score': 40, 'tel': '1235', 'sex': '女'}, {'name': '小王', 'age': 21, 'score': 95, 'tel': '1238', 'sex': '性别不明'} ] #统计不及格学生的个数 count = 0 for i in stu: #if i.get('score') < 60: #count += 1 count += 1 if i.get('score') < 60 else 0 print('不及格学生个数是:', count) #打印不及格学生的名字和成绩 for i in stu: if i.get('score') < 60: print(i['name'], i['score']) #统计未成年学生的个数 count = 0 for i in stu: if i.get('age') < 18: count += 1 print('未成年学生的个数:', count) #打印手机尾号是8的学生的名字 for i in stu: if i['tel'][-1] == '8': print('手机尾号是8的学生名字是:', i['name']) #打印成绩最大的学生名字和成绩 max = 0 for i in stu: if max < i['score']: max = i['score'] #name = i['name'] print('成绩最大学生的名字和成绩分别是:', i['name'], max) #删除性别不明的学生 for i in range(len(stu) -1, -1, -1): if stu[i]['sex'] == '性别不明': del stu[i] print(stu) ''' list1 = [1, 2, 3, 3, 4, 3, 2, 1] list2 = list1.count(3) print(list2) list1.sort(reverse = True) print(list1) def func1(x, y, z): print(x + y + z) func1(20, 20, 30)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)