python3练习实例(经典).doc





《python3练习实例(经典).doc》由会员分享,可在线阅读,更多相关《python3练习实例(经典).doc(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、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 的值:) (回车,之后输
2、入一个值)输入一个半径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=2
3、51x6=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
4、)第七个例子 计算每个月天数 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) (回车,
5、再回车) 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 lis
6、t2 = 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
7、 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 dictAge = 8 # 更新 dictSchool =
8、菜鸟教程 # 添加信息 print (dictAge: , dictAge)dictAge: 8 print (dictSchool: , dictSchool)dictSchool: 菜鸟教程第十七个例子 数字猜谜游戏优化 a=1 i=0 while a !=20:a=int(input(请输入你的数字:)i +=1if a = 20:if i3:print(真厉害,这么快就猜对了!)else:print(总算猜对了,恭喜恭喜!)elif a i=1 while i=9:#里面一层循环控制每一行中的列数j=1while j第十九个例子 计算面积函数 def area(width,height
9、):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)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python3 练习 实例 经典

限制150内