python的练习题(基础知识篇)
基础知识练习: #① 学生成绩去年 72分,今年 85分,相比去年,成绩提高了多少 >>> s1 = 72 >>> s2 = 85 >>> r = (s2-s1)/s1*100 >>> print('%2.1f %%' % r) #中间不需要逗号 print('%2.1f %%', % r) 18.10% #② 循环的使用 #一种是for...in循环,依次把list或tuple中的每个元素迭代出来 #另一种是while循环,条件不满足时退出循环 >>> L = ['Bart', 'Lisa', 'Adam'] >>> temp='' >>> while temp in L : #此处 temp in L,被视为条件判断为FALSE了,所以不执行循环。 ... print(temp) #③ 请定义一个函数quadratic(a, b, c),接收3个参数,求一元二次方程:ax2 + bx + c = 0的两个解。 #提示:计算平方根可以调用math.sqrt()函数: import math def quadratic(a, b, c): if a==0: print('无效输入,a不能=0') return None delt=b*b-4*a*c if delt<0: print('无实数解') return None temp=math.sqrt(delt) x1=(-b+temp)/(2*a) #必须要加括号,2a需要先计算出来,再参与除法 x2=(-b-temp)/(2*a) return x1,x2 print('quadratic(2, 3, 1) =', quadratic(2, 3, 1)) #④ 定义可变参数: #方法1:用列表或元组作为参数 def calc(numbers): sum = 0 for n in numbers: sum = sum + n * n return sum #但是调用的时候,需要先组装出一个list或tuple: >>> calc([1, 2, 3]) 14 >>> calc((1, 3, 5, 7)) 84 #方法2:在参数前面加了一个*号,传入的参数,在函数内部,视为一个tuple def calc(*numbers): sum = 0 for n in numbers: sum = sum + n * n return sum #调用函数的方式可以简化成这样 >>> calc(1, 2, 3) 14 >>> calc(1, 3, 5, 7) 84 #------------------------------- #练习以下函数允许计算两个数的乘积,请稍加改造,变成可接收一个或多个数并计算乘积: def product(*num): tsum=1 for temp in num: tsum=tsum*temp return tsum #⑤利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法 def trim(gold): x=-1 for n in gold: x=x+1 if n!=' ': #找到字符串首部的非空格下标 break temp=len(gold) #下面是获取字符串尾部的非空格下标 j=0 while j<temp: m=j+1 if gold[-m]!=' ': break j=j+1 y=temp-j #j=0时,y=字符串长度 return gold[x:y] #切片中间的字符串,最末的元素下标是[len-1] #----------------------------------- def trim(s): if s == '': #很好,判断了为空的情况 return '' while s[0] == ' ': #这里首部为空,则去掉首部第1个元素,s在变化,循环再看s首部是否为空 s = s[1:] if s == '': return s while s[-1] == ' ': #这里尾部为空,则去掉最后1个元素,s在变化,循环再看s尾部是否为空 s = s[:-1] return s # -------------测试:--------------- if trim('hello ') != 'hello': print('1测试失败!') elif trim(' hello') != 'hello': print('2测试失败!') elif trim(' hello ') != 'hello': print('3测试失败!') elif trim(' hello world ') != 'hello world': print('4测试失败!') elif trim('') != '': print('5测试失败!') elif trim(' ') != '': print('6测试失败!') else: print('测试成功!') #-------------------------------------------
#-*- coding:utf-8 -*- ''' 请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2+bx+c=0的两个解。 提示 一元二次方程的求根公式为: 计算平方根可以调用math.sqrt()函数 ''' import math def quadratic(a, b, c): if not isinstance(a,(int,float)): raise TypeError('bad operation!') if not isinstance(b,(int,float)): raise TypeError('bad operation!') if not isinstance(c,(int,float)): raise TypeError('bad operation!') temp=b*b-4*a*c if temp>=0: x1=(-b+math.sqrt(temp))/(2*a) x2=(-b-math.sqrt(temp))/(2*a) return x1,x2 else: print('无解') # 测试: print('quadratic(2, 3, 1) =', quadratic(2, 3, 1)) print('quadratic(1, 3, -4) =', quadratic(1, 3, -4)) print('quadratic(1, 3, -4) =', quadratic(1, 1, 1)) print(math.sqrt(2)) if quadratic(2, 3, 1) != (-0.5, -1.0): print('测试失败') elif quadratic(1, 3, -4) != (1.0, -4.0): print('测试失败') else: print('测试成功')
-----------------------------------------Have a good day!---------------------------------------------------------------------------------------------------

浙公网安备 33010602011771号