您好,欢迎访问三七文档
python-01-输入输出&基本数据类型7-1jmu-python-输入输出-计算字符串中的数(10分)a=[]s=0n=input()a=n.split()#str.split(str=,num=string.count(str)).foriina:s=s+float(i)print(len(a))print({:.3f}.format(s))Python中的spilt方法只能通过指定的某个字符分割字符串,如果需要指定多个字符,需要用到re模块里的split方法。importres=input()x=re.split(r+,s)print(x)7-2jmu-python-输入输出-格式化输出字符串(10分)a=float(input())#3.14159265b=int(input())#10c=input().strip()#abc123print({:.2f}.format(a))print({0:e}{0:E}{0:.2%}.format(a))print({0:b}{0:x}.format(b))print(c.upper())print({:20}.format(c))#format(s,'20')print({:*^20}.format(c))#format(s,'*^20')#s.center(20,*)s.ljust(20,*)print({}+{}={}.format(a,b,a+b))7-3jmu-python-字符串-统计不同字符个数(5分)line=input()c,blank,d,other=0,0,0,0foreinline:ife.isdigit():d+=1elife.isalpha():c+=1elife.isspace():blank+=1else:other+=1print({}{}{}{}.format(c,blank,d,other))#print(中英文字符={}空格={}数字={}其他字符={}.format(c,blank,d,other))#?如果要统计中文个数呢???m=input(请输入一个中文字符\n)foriinm:if(i.encode('unicode-escape').decode()'\\u4E00'andi.encode('unicode-escape').decode()'\\u9FA5'):print(这是中文:,i)else:print(这不是中文:,i)importrea=这是个中文abcb=re.compile([\u4e00-\u9fa5])c=b.findall(a)foriinc:print(i)7-4jmu-python-运算符-找钱(10分)defgiveChange(money):x10=money//10a=money%10x5=a//5c=a%5x1=cprint({}={}*10+{}*5+{}*1.format(money,x10,x5,x1))n=int(input())foriinrange(n):giveChange(int(input()))#格式python-02-选择分支结构7-1jmu-python-汇率兑换(10分)s=input()x=float(s[1:])ifs[0]==R:print(${:.2f}.format(x/6))else:print(R{:.2f}.format(x*6))7-2jmu-python-回文数判断(5位数字)(10分)利用字符串n=input()ifn==n[::-1]:print(yes)else:print(no)利用列表n=list(input())n1=n[:]n.reverse()ifn==n1:print(yes)else:print(no)利用元组n=tuple(input())ifn==n[::-1]:print(yes)else:print(no)defisPalindrome(line):foriinrange(len(line)//2):if(line[i]!=line[len(line)-i-1]):returnFalsei+=1returnTrueline=input()ifisPalindrome(line):print('yes')else:print('no')7-3jmu-python-分段函数(10分)x=float(input())line=f({:.2f})={:.2f}ifx3:print(line.format(x,1.2))elifx==3:print(line.format(x,10))else:print(line.format(x,2*x+1))7-4jmu-python-判断是否构成三角形(10分)n1=input().split()a1,a2,a3=int(n1[0]),int(n1[1]),int(n1[2])if(a1+a2a3)and(a1+a3a2)and(a2+a3a1):print(yes)else:print(no)7-5jmu-python-分段函数1(10分)importmathb=f({:.3f})={:.3f}x=float(input())ifabs(x)=300:c=b.format(x,-1)else:c=b.format(x,pow(x,3)/math.log10(abs(x)+2.6))print(c)python-03-循环结构7-2jmu-python-九九乘法表(矩形)(10分)foriinrange(1,10):forjinrange(1,10):print({0}*{1}={2:4}.format(i,j,i*j),end=)print()7-3jmu_python_最大公约数&最小公倍数(10分)x,y=input().split(,)x,y=int(x),int(y)foriinrange(1,min(x,y)+1):if(y%i==0)&(x%i==0):gys=iprint(GCD:{},LCM:{}.format(gys,x*y//gys))辗转相除法defgcd(a,b):whileb!=0:temp=a%ba=bb=tempreturnax1,y1=input().split(,)x1,y1=int(x1),int(y1)print(GCD:{},LCM:{}.format(gcd(x1,y1),x1*y1//gcd(x1,y1)))7-4jmu-python-猜数游戏(10分)fromrandomimport*s=input().split()n,x=int(s[0]),int(s[1])seed(x)c=randint(1,10)times=0whileTrue:ifc!=n:times+=1else:breakc=randint(1,10)print({}timestogotit.format(times+1))fromrandomimport*seed(10)foriinrange(10):print(randint(1,10))python-04-函数6-1jmu-python-函数-圆形生成器(10分)importmatha=[]defgetCircleArea(r):returnfloat(math.pi*float(r)*float(r))defget_rList(n):foriinrange(n):a.append(input())returna/*请在这里填写答案*/n=int(input())rList=get_rList(n)foreinrList:print('{:10.3f}'.format(getCircleArea(e)))print(type(rList))6-2jmu-python-求所有参数的乘积(10分)defmulti(t):s=1foriint:s=s*ireturns/*请在这里填写答案*/ls=[float(x)forxininput().split(',')]print(multi(ls))6-3jmu-python-判断质数(10分)//2#质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数importmathdefisPrime(num):ifnotnum.isdigit():returnFalseelifint(num)==1:returnFalseelifint(num)==2:returnTrueelse:foriinrange(2,int(num)):ifint(num)%i==0:returnFalseelse:returnTrue7-1jmu-python-随机生成密码(10分)importstringfromrandomimport*x=int(input())#种子n=int(input())#n个m=int(input())#密码seed(x)s=list(string.ascii_letters+string.digits)foriinrange(n):forxinrange(m):print(choice(s),end=)print()7-2jmu-python-重复元素判定(20分)false,true=0,0n=int(input())defisrepeat(x):iflen(x)==len(set(x)):returnFalseelse:returnTrueforiinrange(n):s=input()ifisrepeat(s.split()):true+=1else:false+=1print(True={},False={}.format(true,false))7-3jmu-python-生日悖论(15分)fromrandomimport*s=input()x,n=s.split()num=0a=[]seed(int(x))foriinrange(int(n)):forjinrange(23):a.append(randint(1,365))iflen(a)==len(set(a)):passelse:num+=1a=[]print(rate={:.2f}.format(num/int(n)))7-4jmu-python-从列表中删除元素(15分)n=int(input())foriinrange(n):s=input()s1=input()x=s.replace(s1+,)x=x.replace(s1,)print(x.strip())n=int(input())foriinrange(n):ls=input().split()key=input()xs=[eforeinlsife!=key]print(.join(xs))7-5jmu-python-逆序输出(5分)importres=input().split()print(.join(s[::-1]))print(s)print(.join(s[::-1]))
本文标题:python
链接地址:https://www.777doc.com/doc-4210789 .html