python3学习案例

2020-12-13 02:57

阅读:383

标签:sum   转换   字符串类型   create   分析   out   tor   inpu   print   

""" 请打印出 1024 * 768 = *** """ shu = 1024 * 768 print("1024 * 768 = %d" %shu) """ 请打印出以下变量的值: # -*- coding: utf-8 -*- n = 123 f = 456.789 s1 = ‘Hello, world‘ s2 = ‘Hello, \‘Adam\‘‘ s3 = r‘Hello, "Bart"‘ s4 = r‘‘‘Hello, Lisa!‘‘‘ """ n = 123 f = 456789 / 1000 s1 = "‘Hello, World‘" s2 = "‘Hello, \\‘Adam\\‘‘" s3 = "r‘Hello, \"Bart\"‘" s4 = ‘r‘‘\‘\‘\‘Hello,\nLisa!\‘\‘\‘‘ print(‘n=‘, n, ‘\nf=‘, f, ‘\ns1=‘, s1, ‘\ns2=‘, s2, ‘\ns3=‘, s3, ‘\ns4=‘, s4) """ ×××的成绩从去年的72分提升到了今年的85分,请计算×××成绩提升的百分点,并用字符串格式化显示出‘xx.x%‘,只保留小数点后1位: """ zuo_nain = 72 / 100 jin_nain = 85 / 100 r = (jin_nain - zuo_nain) * 100 print("提升%.1f%%" %r ) """ 请用索引取出下面list的指定元素: # -*- coding: utf-8 -*- L = [ [‘Apple‘, ‘Google‘, ‘Microsoft‘], [‘Java‘, ‘Python‘, ‘Ruby‘, ‘PHP‘], [‘Adam‘, ‘Bart‘, ‘Lisa‘] ] # 打印Apple: print(?) # 打印Python: print(?) # 打印Lisa: print(?) """ L = [ [‘Apple‘, ‘Google‘, ‘Microsoft‘], [‘Java‘, ‘Python‘, ‘Ruby‘, ‘PHP‘], [‘Adam‘, ‘Bart‘, ‘Lisa‘]] print(L[0][0]) print(L[1][1]) print(L[2][2]) """ ×××身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮×××计算他的BMI指数,并根据BMI指数: 低于18.5:过轻 18.5-25:正常 25-28:过重 28-32:肥胖 高于32:严重肥胖 用if-elif判断并打印结果: """ s = input("高》:") height = float(s) a = input("重》:") weight = float(a) BMI = weight / height ** 2 if BMI 32: print("严重肥胖") """ 请利用循环依次对list中的每个名字打印出Hello, xxx!: L = [‘Bart‘, ‘Lisa‘, ‘Adam‘] """ L = [‘Bart‘, ‘Lisa‘, ‘Adam‘] for i in L: print("Hello, %s" % i) """ 实现将列表:[‘a‘,‘a‘,‘b‘,‘a‘,‘b‘,‘c‘]输出为字典:{‘a‘:3,‘b‘:2,‘c‘:1} """ str_list = [‘a‘, ‘a‘, ‘b‘, ‘a‘, ‘b‘, ‘c‘] st_set = set([‘a‘, ‘a‘, ‘b‘, ‘a‘, ‘b‘, ‘c‘]) dic = {} for i in st_set: cont = str_list.count(i) dic[i] = cont print(dic) """ 请利用Python内置的hex()函数把一个整数转换成十六进制表示的字符串: """ n1 = 233 n2 = 1000 print(hex(n1), hex(n2)) """ 请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2+bx+c=0 的两个解。 计算平方根可以调用math.sqrt()函数 """ import math def quadratic(a, b, c): b2 = b ** 2 - 4*a*c if not isinstance(a + b + c, (int, float)): raise TypeError(‘error type‘) if b2 >= 0: ma_x1 = math.sqrt(b ** 2 - 4*a*c) x1 = (-b - ma_x1) / (2 * a) x2 = (-b + ma_x1) / (2 * a) print("x1=%.2f ,x2=%.2f" % (x1, x2)) else: print("无解") quadratic(1, 5, 5) """ 以下函数允许计算两个数的乘积,请稍加改造,变成可接收一个或多个数并计算乘积: def product(x, y): return x * y """ def product(*arges): if len(arges) == 0: raise TypeError(‘参数不能为空, 否则没有意义!‘) s = 1 for i in arges: if not isinstance(i, (int, float)): raise TypeError(‘error type‘) s = i * s #return s print(s) product(10, 25) """ 请编写move(n, a, b, c)函数,它接收参数n,表示3个柱子A、B、C中第1个柱子A的盘子数量,然后打印出把所有盘子从A借助B移动到C的方法,例如: """ def move(n, a, b, c): if n ‘, c) else: move(n - 1, a, c, b) # 1, A, B, C move(1, a, b, c) #1, A, B, C move(n - 1, b, a, c) #1, A, B, C move(2, ‘A‘, ‘B‘, ‘C‘) """ 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法: """ def trim(s): if s[:1] == ‘ ‘: s = s[1:] print(s) elif s[-1:] == ‘ ‘: s = s[:-1] print(s) elif s[:1] == ‘ ‘ and s[-1:] == ‘ ‘: s = s[1:-1] print(s) trim(" abc2" ) """ 请使用迭代查找一个list中最小和最大值,并返回一个tuple: """ def suh(L): if not isinstance(L, (list, tuple)): raise TypeError(‘param must be a list‘) max_L = min_L = L[0] for i in L: if max_L i: min_L = i print(min_L) print(max_L) suh([8, 2, 4, 5]) """ 如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,所以列表生成式会报错: >>> L = [‘Hello‘, ‘World‘, 18, ‘Apple‘, None] >>> [s.lower() for s in L] Traceback (most recent call last): File "", line 1, in File "", line 1, in AttributeError: ‘int‘ object has no attribute ‘lower‘ 使用内建的isinstance函数可以判断一个变量是不是字符串: >>> x = ‘abc‘ >>> y = 123 >>> isinstance(x, str) True >>> isinstance(y, str) False 请修改列表生成式,通过添加if语句保证列表生成式能正确地执行: # -*- coding: utf-8 -*- L1 = [‘Hello‘, ‘World‘, 18, ‘Apple‘, None] L2 = ?? """ L1 = [‘Hello‘, ‘World‘, 18, ‘Apple‘, None] L2 = [s.lower() for s in L1 if isinstance(s, str)] print(L2) """ 杨辉三角定义如下: 1 / 1 1 / \ / 1 2 1 / \ / \ / 1 3 3 1 / \ / \ / \ / 1 4 6 4 1 / \ / \ / \ / \ / 1 5 10 10 5 1 把每一行看做一个list,试写一个generator,不断输出下一行的list: # 期待输出: # [1] # [1, 1] # [1, 2, 1] # [1, 3, 3, 1] # [1, 4, 6, 4, 1] # [1, 5, 10, 10, 5, 1] # [1, 6, 15, 20, 15, 6, 1] # [1, 7, 21, 35, 35, 21, 7, 1] # [1, 8, 28, 56, 70, 56, 28, 8, 1] # [1, 9, 36, 84, 126, 126, 84, 36, 9, 1] """ def triangles(n): L = [1] m = 0 while n > m: yield L p = [L[a] + L[a + 1] for a in range(len(L) - 1)] #print(p) L = [1] + p + [1] m +=1 for i in triangles(11): print(i) """ 利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:[‘adam‘, ‘LISA‘, ‘barT‘],输出:[‘Adam‘, ‘Lisa‘, ‘Bart‘]: """ def normalize(name): name = name.lower() name = name.title() return name L1 = [‘adam‘, ‘LISA‘, ‘barT‘] L2 = list(map(normalize, L1)) print(L2) """ Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积: """ from functools import reduce def prod(G): def fn(x, y): return x * y return reduce(fn, G) # def f(): retuen x*y reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4) print("3 * 5 * 7 * 9 =", prod([3, 5, 7, 9])) """ 利用map和reduce编写一个str2float函数,把字符串‘123.456‘转换成浮点数123.456: """ def str2float(s): for i in range(len(s)): if s[i] == ‘.‘: n = i def num1(x, y = 0): return x * 10 + y Digits = {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9} def digit(x): return Digits[x] return reduce(num1, map(digit, s[ : n])) + reduce(num1, map(digit, s[n + 1 : ])) / pow(10, (len(s) - n - 1)) print("str2float(‘123.456‘) =", str2float(‘123.456‘)) if abs(str2float(‘123.456‘) - 123.456)

python3学习案例

标签:sum   转换   字符串类型   create   分析   out   tor   inpu   print   

原文地址:https://blog.51cto.com/13399294/2411674


评论


亲,登录后才可以留言!