实验任务1:
task1_1.py
print('hey,u') print('hey','u') x,y,z = 1,2,3 print(x,y,z) print('x = %d, y = %d, z = %d'%(x,y,z)) print('x = {}, y = {},z = {}'.format(x,y,z)) print(f'x = {x} , y = {y}, z = {z}') print(x) print(y) print(z) print(x, end=' ') print(y, end=' ') print(z)
task2_2.py
x1, y1 = 1.2, 3.57 x2, y2 = 2.26, 8.7 print('{:-^40}'.format('输出1')) print('x1 = {},y1 = {}'.format(x1,y1)) print('x2 = {},y2 = {}'.format(x2,y2)) print('{:-^40}'.format('输出2')) print('x1 = {:.1f}, y1 = {:.1f}'.format(x1,y1)) print('x2 = {:.1f}, y2 = {:.1f}'.format(x2,y2)) print('{:-^40}'.format('输出3')) print('x1 = {:<15.1f}, y1 = {:<15.1f}'.format(x1,y1)) print('x2 = {:<15.1f}, y2 = {:<15.1f}'.format(x2,y2)) print('{:-^40}'.format('输出3')) print('x1 = {:>15.1f}, y1 = {:>15.1f}'.format(x1,y1)) print('x2 = {:>15.1f}, y2 = {:>15.1f}'.format(x2,y2))
task1_3.py
name1, age1 = 'Bill', 19 name2, age2 = 'Hellen', 18 title = 'Personnel Information' print(f'{title:=^40}') print(f'name: {name1:10} age: {age1:3}') print(f'name: {name2:10} age: {age2:3}') print(40*'=')
总结:
print()的几种常用用法:
1.输出 变量 = 赋值 常用输出形式:1.('x = %d' %x) 2.('{x}'.format(x)) 3.(f'x = {x}')
2.格式输出,包括分隔符 居中靠左靠右等
3.换行等
实验任务2
task2_1.py
r1 = eval('1 + 2') print(type(r1), r1) r2 = eval('1+2j') print(type(r2), r2) r3 = eval('"python"') print(type(r3), r3) r4 = eval('7, 42') print(type(r4), r4)
task2_2.py
# 组合使用内置函数eval()和input() x, y = eval(input('Enter two oprands: ')) ans = x + y print(f'{x} + {y} = {ans}') print(f'{type(x)} + {type(y)} = {type(ans)}')
总结:
内置函数eval()常用于数字类型的分配
input()常用于输入
实验任务3
task3_1.py
ans1 = 0.1 + 0.2 print(f'0.1 + 0.2 = {ans1}') import decimal ans2 = decimal.Decimal('0.1') + decimal.Decimal('0.2') print(f'0.1 + 0.2 = {ans2}')
task3_2.py
print(chr(0x1f604)) print(chr(10000), end=" ") print(chr(0x025b), end=" ") print(chr(0x2708), end=" ") print(chr(0x00A5), end=" ") print(chr(0x266b)) print(ord('a'), end = " ") print(ord('b'), end = " ") print(ord('c')) print(ord('A'), end = " ") print(ord('B'), end = " ") print(ord('C')) print(ord('0'), end = " ") print(ord('1'), end = " ") print(ord('2'))
总结:
chr()返回unicode编码对应的字符
ord返回字符的unicode编码
实验任务5
task5_1.py
import math n = float(input('输入一个数:')) # python中计算开平方的三种方式 ans1 = n**0.5 ans2 = pow(n, 0.5) ans3 = math.sqrt(n) print('%.2f的平方根是: %.2f' %(n, ans1)) print('{:.2f}的平方根是: {:.2f}'.format(n, ans2)) print(f'{n:.2f}的平方根是: {ans3:.2f}')
task5_2.py
import math text = ''' 好奇心是人的天性。 理想情况下,学习新东西是让人愉快的事。 但学校里的学习似乎有点像苦役。 有时候,需要画一个大饼,每次尝试学一些新鲜的,才会每天变得更好一点点。 ''' print(text) r = float(input('给学习画一个大饼,大饼要做的很大,半径要这么大: ')) circle = 2*math.pi*r print(f'绕起来,大饼的圆周有这么长, {circle}, 够不够激发你探索未知的动力...')
实验6
x = float(input()) y = x**356 print(f'{x}的365次方:{y:.2f}')
实验7
T0 = float(input()) p, c, K = eval('1.038, 3.7, 0.0054') M, m, Tw = eval('47, 67, 100') Ty = int('70') import math pi = math.pi t1 = (M**(2/3))*c*(p**(1/3)) t2 = (K*(pi**2)*(((4*pi)/3)**(2/3))) t3 = ((T0-Tw)/(Ty-Tw))*0.76 t4 = math.log(t3) t = (t1/t2)*t4 ta = t//60 tb = int(t - ta*60 +0.5) print(f'T0 = {T0}℃, t = {ta}分{tb}秒')
实验任务8
task8_1.py
""" 家用电器销售系统 v1.0 """ #欢迎信息 print('欢迎使用家用电器销售网络!') #产品信息列表 print('产品和价格信息如下:') print('*'*60) print('%-10s'%'编号', '%-10s'%'名称', '%-10s'%'品牌', '%-10s'%'价格', '%-10s'%'库存数量') print('-'*60) print('%-10s'%'0001', '%-10s'%'电视机', '%-10s'%'海尔', '%10.2f'%5999.00, '%10d'%20) print('%-10s'%'0002', '%-10s'%'冰箱', '%-10s'%'西门子', '%10.2f'%6998.00, '%10d'%15) print('%-10s'%'0003', '%-10s'%'洗衣机', '%-10s'%'小天鹅', '%10.2f'%1999.00, '%10d'%10) print('%-10s'%'0004', '%-10s'%'空调', '%-10s'%'格力', '%10.2f'%3900.00, '%10d'%0) print('%-10s'%'0005', '%-10s'%'热水器', '%-10s'%'美的', '%10.2f'%688.00, '%10d'%30) print('%-10s'%'0006', '%-10s'%'笔记本', '%-10s'%'联想', '%10.2f'%5699.00, '%10d'%10) print('%-10s'%'0007', '%-10s'%'微波炉', '%-10s'%'苏泊尔', '%10.2f'%480.50, '%10d'%33) print('%-10s'%'0008', '%-10s'%'投影仪', '%-10s'%'松下', '%10.2f'%1250.00, '%10d'%12) print('%-10s'%'0009', '%-10s'%'吸尘器', '%-10s'%'飞利浦', '%10.2f'%999.00, '%10d'%9) print('-'*60) #用户输入信息 product_id = input('请输入你购买产品的序号:') price = float(input('请输入你要购买的产品价格:')) count = int(input('请输入你要购买的产品数量')) #计算金额 print('购买成功,你需要支付',price*count,'元') #退出系统 print('谢谢你的光临,下次再见!')
task8_2.py
""" 家用电器销售系统 v1.0 """ #欢迎信息 print('欢迎使用家用电器销售网络!') #产品信息列表 print('产品和价格信息如下:') print('*'*60) print('%-10s'%'编号', '%-10s'%'名称', '%-10s'%'品牌', '%-10s'%'价格', '%-10s'%'库存数量') print('-'*60) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0001','电视机','海尔','5999.00','20')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0002','冰箱','西门子','6998.00','15')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0003','洗衣机','小天鹅','1999.00','10')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0004','空调','格力','3900.00','0')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0005','热水器','美的','688.00','30')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0006','笔记本','联想','5699.00','10')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0007','微波炉','苏泊尔','480.00','33')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0008','投影仪','松下','1250.00','12')) print('{:<10}{:<10}{:<10}{:<10}{:>10}'.format('0009','吸尘器','飞利浦','999.00','9')) print('-'*60) #用户输入信息 product_id = input('请输入你购买产品的序号:') price = float(input('请输入你要购买的产品价格:')) count = int(input('请输入你要购买的产品数量')) #计算金额 print('购买成功,你需要支付',price*count,'元') #退出系统 print('谢谢你的光临,下次再见!')
task8_3.py
""" 家用电器销售系统 v1.0 """ #欢迎信息 print('欢迎使用家用电器销售网络!') #产品信息列表 print('产品和价格信息如下:') print('*'*60) print('%-10s'%'编号', '%-10s'%'名称', '%-10s'%'品牌', '%-10s'%'价格', '%-10s'%'库存数量') print('-'*60) a1,a2,a3,a4,a5 = '0001','电视机','海尔','5999.00','20' b1,b2,b3,b4,b5 = '0002','冰箱','西门子','6998.00','15' c1,c2,c3,c4,c5 = '0003','洗衣机','小天鹅','1999.00','10' d1,d2,d3,d4,d5 = '0004','空调','格力','3900.00','0' e1,e2,e3,e4,e5 = '0005','热水器','美的','688.00','30' f1,f2,f3,f4,f5 = '0006','笔记本','联想','5699.00','10' g1,g2,g3,g4,g5 = '0007','微波炉','苏泊尔','480.00','33' h1,h2,h3,h4,h5 = '0008','投影仪','松下','1250.00','12' i1,i2,i3,i4,i5 = '0009','吸尘器','飞利浦','999.00','9' print(f'{a1:<10}{a2:<10}{a3:<10}{a4:<10}{a5:>10}') print(f'{b1:<10}{b2:<10}{b3:<10}{b4:<10}{b5:>10}') print(f'{c1:<10}{c2:<10}{c3:<10}{c4:<10}{c5:>10}') print(f'{d1:<10}{d2:<10}{d3:<10}{d4:<10}{d5:>10}') print(f'{e1:<10}{e2:<10}{e3:<10}{e4:<10}{e5:>10}') print(f'{f1:<10}{f2:<10}{f3:<10}{f4:<10}{f5:>10}') print(f'{g1:<10}{g2:<10}{g3:<10}{g4:<10}{g5:>10}') print(f'{h1:<10}{h2:<10}{h3:<10}{h4:<10}{h5:>10}') print(f'{i1:<10}{i2:<10}{i3:<10}{i4:<10}{i5:>10}') print('-'*60) #用户输入信息 product_id = input('请输入你购买产品的序号:') price = float(input('请输入你要购买的产品价格:')) count = int(input('请输入你要购买的产品数量')) #计算金额 print('购买成功,你需要支付',price*count,'元') #退出系统 print('谢谢你的光临,下次再见!')
标签:%-,10,初体验,format,python,编程,input,print,10s From: https://www.cnblogs.com/xycproduction/p/17191357.html