实验任务1
task1:
task1-1源码:
1 # print输出的几种用法 2 3 # 用法1:用于输出单个字符串或单个变量 4 print('hey,u') 5 6 # 用法2:用于输出多个数据项,用逗号分隔 7 print('hey','u') 8 x,y,z = 1,2,3 9 print(x,y,z) 10 11 # 用法3:用户混合字符串和变量值 12 print('x = %d, y = %d, z = %d' %(x,y,z)) # 方式1:传统c风格 13 print('x = {}, y = {}, z = {}'.format(x,y,z)) #方式2:s.format()方法 14 print(f'x = {x}, y = {y}, z = {z}') #方式3:f-string方式 15 16 # 其他:输出后是否换行 17 print(x) # 默认输出后换一行 18 print(y) 19 print(z) 20 21 print(x, end=' ') #输出结束后,不换行;通过end指定数据项之间的分隔符 22 print(y, end=' ') 23 print(z)
运行结果:
task2:
task1-2源码:
1 # 输出1 2 print('{:-^40}'.format('输出1')) 3 print('x1 = {}, y1 = {}'.format(x1, y1)) 4 print('x2 = {}, y2 = {}'.format(x2, y2)) 5 6 # 输出2 7 print('{:-^40}'.format('输出2')) 8 print('x1 = {:.1f}, y1 = {:.1f}'.format(x1, y1)) 9 print('x2 = {:.1f}, y2 = {:.1f}'.format(x2, y2)) 10 11 # 输出3 12 print('{:-^40}'.format('输出3')) 13 print('x1 = {:<15.1f}, y1 = {:<15.1f}'.format(x1, y1)) 14 print('x2 = {:<15.1f}, y2 = {:<15.1f}'.format(x2, y2)) 15 16 # 输出4 17 print('{:-^40}'.format('输出4')) 18 print('x1 = {:>15.1f}, y1 = {:>15.1f}'.format(x1, y1)) 19 print('x2 = {:>15.1f}, y2 = {:>15.1f}'.format(x2, y2))
运行结果:
task3:
task1-3源码:
1 # f-string方式输出数据并控制格式 2 3 name1, age1 = 'Bill', 19 4 name2, age2 = 'Hellen', 18 5 title = 'Personnel Information' 6 7 print(f'{title:=^40}') 8 print(f'name: {name1:10} age: {age1:3}') 9 print(f'name: {name2:10} age: {age2:3}') 10 print(40*'=')
运行结果:
实验任务2:
task1:
task2-1源码:
1 # 内置函数 2 # 功能:把字符串转换为python表达式 3 # 理解:相当于把圆括号中的内容,两侧引号去掉 4 5 r1 = eval('1 + 2') 6 print(type(r1), r1) 7 8 r2 = eval('1 + 2j') 9 print(type(r2), r2) 10 11 r3 = eval('"python"') 12 print(type(r3), r3) 13 14 r4 = eval('7, 42') 15 print(type(r4), r4)
运行结果:
task2:
task2-2源码:
1 # 组合使用内置函数eval()和input() 2 3 x,y = eval(input('Enter two oprands:')) 4 ans = x + y 5 6 print(f'{x} + {y} = {ans}') 7 print(f'{type(x)} + {type(y)} = {type(ans)}')
运行结果:
实验任务3:
task1:
task3-1源码:
1 # 浮点数简单运算 2 3 ans1 = 0.1 + 0.2 4 print(f'0.1 + 0.2 = {ans1}') 5 6 import decimal 7 8 ans2 = decimal.Decimal('0.1') 9 print(f'0.1 + 0.2 = {ans2}')
运行结果:
实验任务4:
task1:
task4-1源码:
1 # 字符编码相关 2 # chr()返回unicode编码对应的字符 3 4 print(chr(0x1f600), end = " ") 5 print(chr(0x1f601), end = " ") 6 print(chr(0x1f602), end = " ") 7 print(chr(0x1f603), end = " ") 8 print(chr(0x1f604)) 9 10 print(chr(10000), end = " ") 11 print(chr(0x025b), end = " ") 12 print(chr(0x2708), end = " ") 13 print(chr(0x00A5), end = " ") 14 print(chr(0x266b)) 15 16 # ord()返回字符的unicode编码 17 print(ord('a'), end = " ") 18 print(ord('b'), end = " ") 19 print(ord('c')) 20 21 print(ord('A'), end = " ") 22 print(ord('B'), end = " ") 23 print(ord('C')) 24 25 print(ord('0'), end = " ") 26 print(ord('1'), end = " ") 27 print(ord('2'))
运行结果:
实验任务5:
task1:
task5-1源码:
1 # 简单的数学计算 2 import math 3 4 n = float(input('输入一个数:')) 5 6 # python中计算开平方的三种方式 7 ans1 = n**0.5 8 ans2 = pow(n, 0.5) 9 ans3 = math.sqrt(n) 10 11 print('%.2f的平方根是:%.2f' %(n, ans1)) 12 print('{:.2f}的平方根是:{:.2f}'.format(n, ans2)) 13 print(f'{n:.2f}的平方根是:{ans3:.2f}')
运行结果:
task2:
task5-2源码:
1 # 神奇的pi, 与大饼 2 3 import math 4 5 text = ''' 6 好奇心是人的天性。 7 理想情况下,学习新东西是让人愉快的事。 8 但学校里的学习似乎有点像苦役。 9 有时候,需要画一个大饼,每次尝试学一些新鲜的,才会每天变得更好一点点。 10 ''' 11 print(text) 12 13 r = float(input('给学习画一个大饼,大饼要做的很大,半径要这么大:')) 14 15 circle = 2*math.pi*r 16 17 print(f'绕起来,大饼的圆周有这么长,{circle},够不够激发你探索未知的动力......')
运行结果:
实验任务6:
task1:
task6-1源码:
1 x = eval(input()) 2 y = pow(x,365) 3 print(f'{x:.2f}的365次方:{y:.2f}')
运行结果:
实验任务7:
task1:
task7-1源码:
1 # 煮出一个完美鸡蛋 2 p,c,K,Tw,Ty = eval('1.038, 3.7, 5.4*0.001, 100, 70') 3 M,T0 = eval(input()) 4 import math 5 T1 = ((pow(M, 2/3))*c*(pow(p, 1/3)))/(K*(math.pi**2)*(pow(((4*math.pi)/3), 2/3))) 6 T2 = math.log(0.76*((T0-Tw)/(Ty-Tw))) 7 t = T1*T2 8 a = int(t) 9 b = (t-a)*100 10 print(f'在原始温度T0 = {T0} 0C的条件下, 煮出完美鸡蛋需要t = {a}分{b:.0f}秒')
运行结果:
实验任务8:
task1:
task8-1源码:
1 """ 2 家用电器销售系统 3 v1.0 4 """ 5 6 # 欢迎信息 7 print('欢迎使用家用电器销售系统!') 8 9 # 产品信息列表 10 print('产品和价格信息如下:') 11 print('*'*60) 12 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量') 13 print('-'*60) 14 print('%-10s'%'0001','%-10s' %'电视机','%-10s' %'海尔', '%10.2f' %5999.00, '%10d' %20) 15 print('%-10s'%'0002','%-10s' %'冰箱', '%-10s' %'西门子','%10.2f' %6998.00, '%10d' %15) 16 print('%-10s'%'0003','%-10s' %'洗衣机','%-10s' %'小天鹅', '%10.2f' %1999.00, '%10d' %10) 17 print('%-10s'%'0004','%-10s' %'空调', '%-10s' %'格力', '%10.2f' %3900.00, '%10d' %0) 18 print('%-10s'%'0005','%-10s' %'热水器','%-10s' %'美的', '%10.2f' %688.00, '%10d' %30) 19 print('%-10s'%'0006','%-10s' %'笔记本','%-10s' %'联想', '%10.2f' %5699.00, '%10d' %10) 20 print('%-10s'%'0007','%-10s' %'微波炉','%-10s' %'苏泊尔','%10.2f' %480.50, '%10d' %33) 21 print('%-10s'%'0008','%-10s' %'投影仪','%-10s' %'松下', '%10.2f' %1250.00, '%10d' %12) 22 print('%-10s'%'0009','%-10s' %'吸尘器','%-10s' %'飞利浦','%10.2f' %999.00, '%10d' %9) 23 print('-'*60) 24 25 # 用户输入信息 26 product_id = input('请输入您要购买的产品编号:') 27 price = float(input('请输入您要购买的产品价格:')) 28 count = int(input('请输入您要购买的产品数量:')) 29 30 # 计算金额 31 print('购买成功,您需要支付',price*count,'元') 32 33 # 退出系统 34 print('谢谢您的光临,下次再见!')
运行结果:
task2:
task8-2源码:
""" 家用电器销售系统 v1.0 """ # 欢迎信息 print('欢迎使用家用电器销售系统!') # 产品信息列表 print('产品和价格信息如下:') print('*'*60) print('{:<10s}'.format('编号'),'{:<10s}'.format('名称'),'{:<10s}'.format('品牌'),'{:<10s}'.format('价格'),'{:<10s}'.format('库存数量')) print('-'*60) print('{:<10s}'.format('0001'),'{:<10s}' .format('电视机'),'{:<10}'.format('海尔'), '{:<10.2f}'.format(5999.00), '{:<10d}'.format(20)) print('{:<10s}'.format('0002'),'{:<10s}' .format('冰箱'),'{:<10}'.format('西门子'), '{:<10.2f}'.format(6998.00), '{:<10d}'.format(15)) print('{:<10s}'.format('0003'),'{:<10s}' .format('洗衣机'),'{:<10}'.format('小天鹅'), '{:<10.2f}'.format(1999.00), '{:<10d}'.format(10)) print('{:<10s}'.format('0004'),'{:<10s}' .format('空调'),'{:<10}'.format('格力'), '{:<10.2f}'.format(3900.00), '{:<10d}'.format(0)) print('{:<10s}'.format('0005'),'{:<10s}' .format('热水器'),'{:<10}'.format('美的'), '{:<10.2f}'.format(688.00), '{:<10d}'.format(30)) print('{:<10s}'.format('0006'),'{:<10s}' .format('笔记本'),'{:<10}'.format('联想'), '{:<10.2f}'.format(5699.00), '{:<10d}'.format(10)) print('{:<10s}'.format('0007'),'{:<10s}' .format('微波炉'),'{:<10}'.format('苏泊尔'), '{:<10.2f}'.format(480.50), '{:<10d}'.format(33)) print('{:<10s}'.format('0008'),'{:<10s}' .format('投影仪'),'{:<10}'.format('松下'), '{:<10.2f}'.format(1250.00), '{:<10d}'.format(12)) print('{:<10s}'.format('0009'),'{:<10s}' .format('吸尘器'),'{:<10}'.format('飞利浦'), '{:<10.2f}'.format(999.00), '{:<10d}'.format(9)) print('-'*60) # 用户输入信息 product_id = input('请输入您要购买的产品编号:') price = float(input('请输入您要购买的产品价格:')) count = int(input('请输入您要购买的产品数量:')) # 计算金额 print('购买成功,您需要支付',price*count,'元') # 退出系统 print('谢谢您的光临,下次再见!')
运行结果:
task3:
task8-3源码:
1 """ 2 家用电器销售系统 3 v1.0 4 """ 5 6 # 欢迎信息 7 print('欢迎使用家用电器销售系统!') 8 9 # 产品信息列表 10 print('产品和价格信息如下:') 11 print('*'*60) 12 a = ['编号','名称','品牌','价格','库存数量'] 13 b = ["0001","0002","0003","0004","0005","0006","0007","0008","0009"] 14 c = ['电视机','冰箱','洗衣机','空调','热水器','笔记本','微波炉','投影仪','吸尘器'] 15 d = ['海尔','西门子','小天鹅','格力','美的','联想','苏泊尔','松下','飞利浦'] 16 e = [5999.00,6998.00,1999.00,3900.00,688.00,5699.00,480.50,1250.00,999.00] 17 f = [20,15,10,0,30,10,33,12,9] 18 19 print(f'{a[0]:10s}{a[1]:10s}{a[2]:10s}{a[3]:10s}{a[4]:10s}') 20 print('-'*60) 21 print(f'{b[0]:10s}{c[0]:10s}{d[0]:10s}{e[0]:10.2f}{f[0]:10d}') 22 print(f'{b[1]:10s}{c[1]:10s}{d[1]:10s}{e[1]:10.2f}{f[1]:10d}') 23 print(f'{b[2]:10s}{c[2]:10s}{d[2]:10s}{e[2]:10.2f}{f[2]:10d}') 24 print(f'{b[3]:10s}{c[3]:10s}{d[3]:10s}{e[3]:10.2f}{f[3]:10d}') 25 print(f'{b[4]:10s}{c[4]:10s}{d[4]:10s}{e[4]:10.2f}{f[4]:10d}') 26 print(f'{b[5]:10s}{c[5]:10s}{d[5]:10s}{e[5]:10.2f}{f[5]:10d}') 27 print(f'{b[6]:10s}{c[6]:10s}{d[6]:10s}{e[6]:10.2f}{f[6]:10d}') 28 print(f'{b[7]:10s}{c[7]:10s}{d[7]:10s}{e[7]:10.2f}{f[7]:10d}') 29 print(f'{b[8]:10s}{c[8]:10s}{d[8]:10s}{e[8]:10.2f}{f[8]:10d}') 30 print('-'*60) 31 32 # 用户输入信息 33 product_id = input('请输入您要购买的产品编号:') 34 price = float(input('请输入您要购买的产品价格:')) 35 count = int(input('请输入您要购买的产品数量:')) 36 37 # 计算金额 38 print('购买成功,您需要支付',price*count,'元') 39 40 # 退出系统 41 print('谢谢您的光临,下次再见!')
运行结果:
标签:%-,初体验,end,编程,10d,pyhton,10.2,print,10s From: https://www.cnblogs.com/dtong2468/p/17191325.html