python3练习实例(经典).doc
Python实例第一个例子 求乘积print(2*2)输出结果为:4print(2*2)输出结果为:2*2第二个例子 求最大值print("80, 100, 1000 最大值为: ", max(80, 100, 1000)输出结果为:80, 100, 1000 最大值为: 1000print(max(80, 100, 1000)输出结果为:1000第三个例子 求两数之和print('两数之和为 %.1f' %(float(input('输入第一个数字:')+float(input('输入第二个数字:')第四个例子 求圆面积>>>PI = 3.14(回车)>>>r = input("输入一个半径 r 的值:") (回车,之后输入一个值)输入一个半径r的值7(输入7)>>> if r.isdigit():(回车)s = PI * pow(float(r),2) (连续回车再回车)>>> print("半径为 的圆面积为::.3f".format(r,s) (回车)半径为7的圆面积为:153.860第五个例子 九九乘法表>>> for i in range(1, 10):for j in range(1, i+1):print('x=t'.format(j, i, i*j), end='')1x1=11x2=22x2=41x3=32x3=63x3=91x4=42x4=83x4=124x4=161x5=52x5=103x5=154x5=205x5=251x6=62x6=123x6=184x6=245x6=306x6=361x7=72x7=143x7=214x7=285x7=356x7=427x7=491x8=82x8=163x8=244x8=325x8=406x8=487x8=568x8=641x9=92x9=183x9=274x9=365x9=456x9=547x9=638x9=729x9=81第六个例子 Python 生成日历>>> import calendar>>> yy = int(input("输入年份: ")输入年份: 2019>>> mm = int(input("输入月份: ")输入月份: 8>>> print(calendar.month(yy,mm)第七个例子 计算每个月天数>>> import calendar>>> monthRange = calendar.monthrange(2019,7)>>> print(monthRange)(0, 31)(前一个数字代表输出的是星期几:0-6,0代表星期一,6代表星期天,第二个数字代表这个月的天数)第八个例子 判断奇数偶数>>> num = int(input("输入一个数字: ")(回车)输入一个数字: 57(按提示输入数字,然后回车)>>> if (num % 2) = 0:(回车) print("0 是偶数".format(num) (回车)else: (回车) print("0 是奇数".format(num) (回车,再回车) 57 是奇数第九个例子 删除列表元素>>> list1 = 'physics', 'chemistry', 1997, 2000>>> print('list1 del: ',list12)list1 del: 1997第十个例子 添加列表元素>>> list = # 空列表>>> list.append('Google') # 使用 append() 添加元素>>> list.append('Runoob')>>> print(list)'Google', 'Runoob'第十一个例子 更新列表>>> list = 'Google', 'Runoob', 1997, 2000>>> >>> print ("第三个元素为 : ", list2)第三个元素为 : 1997>>> list2 = 2001>>> print ("更新后的第三个元素为 : ", list2)更新后的第三个元素为 : 2001第十二个例子 列表拼接>>> squares = 1, 4, 9, 16, 90>>> squares += 36, 49, 64, 81, 100>>> squares1, 4, 9, 16, 90, 36, 49, 64, 81, 100第十三个例子 嵌套列表(列表里再建列表)>>>a = 'a', 'b', 'c' >>> n = 1, 2, 3 >>> x = a, n >>> x 'a', 'b', 'c', 1, 2, 3 >>> x0 (x0表示x列表中的第一个列表,即'a', 'b', 'c',也可以把列表中的列表当做一个元素。)'a', 'b', 'c' >>> x01 (x0 1表示x列表中的第一个列表,即'a', 'b', 'c'中的第一个元素即b)'b'第十四个例子 元组最大最小值>>> tuple2 = (5, 4, 8)>>> max(tuple2)8>>> tup = (5, 4, 8)>>> min(tup)4第十五个例子 把列表转换成元组>>> list1= 'Google', 'Taobao', 'Runoob', 'Baidu'>>> tuple1=tuple(list1)>>> tuple1('Google', 'Taobao', 'Runoob', 'Baidu')第十六个例子 修改字典>>> dict = 'Name': 'Runoob', 'Age': 7, 'Class': 'First'>>> dict'Age' = 8 # 更新>>> dict'School' = "菜鸟教程" # 添加信息>>> print ("dict'Age': ", dict'Age')dict'Age': 8>>> print ("dict'School': ", dict'School')dict'School': 菜鸟教程第十七个例子 数字猜谜游戏优化>>> a=1>>> i=0>>> while a !=20:a=int(input('请输入你的数字:')i +=1if a = 20:if i<3:print('真厉害,这么快就猜对了!')else:print('总算猜对了,恭喜恭喜!')elif a<20:print('你猜的数字小了,不要灰心,继续努力')else:print('你猜的数字大了,不要灰心,继续加油')请输入你的数字:12你猜的数字小了,不要灰心,继续努力请输入你的数字:2你猜的数字小了,不要灰心,继续努力请输入你的数字:22你猜的数字大了,不要灰心,继续加油请输入你的数字:20总算猜对了,恭喜恭喜!第十八个例子 使用循环嵌套来实现99乘法法则>>> i=1>>> while i<=9:#里面一层循环控制每一行中的列数j=1while j<=i:mut =j*iprint("%d*%d=%d"%(j,i,mut), end=" ")j+=1print("")i+=11*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 >>>第十九个例子 计算面积函数>>> def area(width,height):return width*height>>> def print_welcome(name):print('welcome',name)>>> print_welcome('Runood')welcome Runood>>> w=4>>> h=5>>> print('width=',w,'height=',h,'area=',area(w,h)width= 4 height= 5 area= 20>>>第二十个例子 实现秒表功能while True:try:input()starttime = time.time()print('开始')while True:print('计时: ', round(time.time() - starttime, 0), '秒', end="r")time.sleep(1)except KeyboardInterrupt:print('结束')endtime = time.time()print('总共的时间为:', round(endtime - starttime, 2),'secs')breakwhile True:try:input()starttime = time.time()print('开始')while True:print('计时: ', round(time.time() - starttime, 0), '秒', end="r")time.sleep(1)except KeyboardInterrupt:print('结束')endtime = time.time()print('总共的时间为:', round(endtime - starttime, 2),'secs')break'while True:'开始计时: 0.0 秒计时: 1.0 秒计时: 2.0 秒计时: 3.0 秒计时: 4.0 秒计时: 5.0 秒计时: 6.0 秒计时: 8.0 秒计时: 9.0 秒计时: 10.0 秒计时: 11.0 秒计时: 12.0 秒计时: 13.0 秒计时: 14.0 秒计时: 15.0 秒计时: 17.0 秒计时: 18.0 秒计时: 20.0 秒计时: 22.0 秒计时: 24.0 秒结束总共的时间为: 25.6 secs>>>第二十一个例子 约瑟夫生者死者小游戏30 个人在一条船上,超载,需要 15 人下船。于是人们排成一队,排队的位置即为他们的编号。报数,从 1 开始,数到 9 的人下船。如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?>>> people=>>> for x in range(1,31):peoplex=1>>> # print(people)>>> check=0>>> i=1>>> j=0>>> while i<=31:if i=31:i=1elif j=15:breakelse:if peoplei=0:i+=1continueelse:check+=1if check=9:peoplei=0check=0print('号下船了'.format(i)j+=1else:i+=1continue9号下船了18号下船了27号下船了6号下船了16号下船了26号下船了7号下船了19号下船了30号下船了12号下船了24号下船了8号下船了22号下船了5号下船了23号下船了>>>第二十二个例子 太阳花。>>> from turtle import *>>> color('red', 'yellow')>>> begin_fill()>>> while True:forward(200)left(170)if abs(pos() < 1:break>>> end_fill()>>> done()第二十三个例子 python玩21点>>> import random>>> user1 = input('user1name:')user1name:青铜>>> user2 = input('user2name:')user2name:王者>>> user_info = user1: 'win':0, user2: 'win':0 >>> while True:computer1_num = random.randint(1,10)print(f'电脑随机产生一个数:computer1_num')computer2_num = random.randint(1,10)print(f'电脑随机产生一个数:computer2_num')user1_num = input(f'user1请猜一个数(按q结束):')user2_num = input(f'user2请猜一个数(按q结束):')if user1_num = 'q' or user1_num = 'q':print('end')breakelse:user1_sum = int(computer1_num) + int(computer2_num) + int(user1_num)user2_sum = int(computer1_num) + int(computer2_num) + int(user2_num)if abs(user1_sum-21) > abs(user2_sum-21):print(f'user1点数:user1_sum,user2点数:user2_sum,结果:user2获胜')user_infouser2'win'+=1print(user_info)else:print(f'user1点数:user1_sum,user2点数:user2_sum,结果:user1获胜')user_infouser1'win'+=1print(user_info)电脑随机产生一个数:3电脑随机产生一个数:9青铜请猜一个数(按q结束):9王者请猜一个数(按q结束):10青铜点数:21,王者点数:22,结果:青铜获胜'青铜': 'win': 1, '王者': 'win': 0电脑随机产生一个数:7电脑随机产生一个数:8青铜请猜一个数(按q结束):12王者请猜一个数(按q结束):10青铜点数:27,王者点数:25,结果:王者获胜'青铜': 'win': 1, '王者': 'win': 1电脑随机产生一个数:8电脑随机产生一个数:6青铜请猜一个数(按q结束):4王者请猜一个数(按q结束):5青铜点数:18,王者点数:19,结果:王者获胜'青铜': 'win': 1, '王者': 'win': 2电脑随机产生一个数:5电脑随机产生一个数:10青铜请猜一个数(按q结束):q王者请猜一个数(按q结束):qend第二十四个例子 画个心形>>> print('n'.join(''.join('Love'(x-y) % len('Love') if(x*0.05)*2+(y*0.1)*2-1)*3-(x*0.05)*2*(y*0.1)*3 <= 0 else ' ') for x in range(-30, 30) for y in range(30, -30, -1) veLoveLov veLoveLov eLoveLoveLoveLove eLoveLoveLoveLove veLoveLoveLoveLoveLoveLoveLoveLoveLoveLov veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLov eLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove LoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL oveLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLo veLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLov eLoveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove oveLoveLoveLoveLoveLoveLoveLoveLoveLoveLove eLoveLoveLoveLoveLoveLoveLoveLoveLoveLove LoveLoveLoveLoveLoveLoveLoveLoveLoveLoveL eLoveLoveLoveLoveLoveLoveLoveLoveLove oveLoveLoveLoveLoveLoveLoveLoveLove eLoveLoveLoveLoveLoveLoveLoveLove veLoveLoveLoveLoveLoveLoveLov oveLoveLoveLoveLoveLoveLo LoveLoveLoveLoveLoveL LoveLoveLoveLov LoveLoveL Lov v