实验任务1:
task1_1
实验源码:
#task1_1 print的使用 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))#传统c风格 print('x = {} , y = {} , z = {}'.format(x,y,z))#s.format print(f'x = {x} , y = {y} , z = {z}')#f-string #关于换行 print(x) print(y) print(z)#默认输出后换一行 print(x,end=' ') print(y,end=' ') print(z)#输出结束后不换行,“end”
运行截图:
task1-2:
源码:
# 输出1 print('{:-^40}'.format('输出1')) print('x1 = {}, y1 = {}'.format(x1, y1)) print('x2 = {}, y2 = {}'.format(x2, y2)) # 输出2 print('{:-^40}'.format('输出2')) print('x1 = {:.1f}, y1 = {:.1f}'.format(x1, y1)) print('x2 = {:.1f}, y2 = {:.1f}'.format(x2, y2)) # 输出3 print('{:-^40}'.format('输出3')) print('x1 = {:<15.1f}, y1 = {:<15.1f}'.format(x1, y1)) print('x2 = {:<15.1f}, y2 = {:<15.1f}'.format(x2, y2)) # 输出4 print('{:-^40}'.format('输出4')) print('x1 = {:>15.1f}, y1 = {:>15.1f}'.format(x1, y1)) print('x2 = {:>15.1f}, y2 = {:>15.1f}'.format(x2, y2))
运行截图:
task1-3:
源码:
# f-string方式输出数据并控制格式 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*'=')
运行截图:
实验任务2:
task2_1
实验源码:
#eval 字符串变成python表达式 r1 = eval('1 + 2')#r1 = 1 + 2 print(type(r1),r1) r2 = eval('1+2j')#r2 = 1+2j print(type(r2),r2) r3 = eval('"python"')#r3 = "python" print(type(r3),r3) r4 = eval('7,42')#r4 = 7 ,42 print(type(r4),r4)
运行截图:
task2_2
实验源码:
#组合使用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)}')
运行截图:
实验任务3
task3.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}')
运行截图:
实验任务4
task4.py
实验源码
#字符编码相关 #chr()返回unicode编码对应的字符 print(chr(0x1f600), end = " ") print(chr(0x1f601), end = " ") print(chr(0x1f602), end = " ") print(chr(0x1f603), end = " ") print(chr(0x1f604)) print(chr(10000), end=" ") print(chr(0x025b), end = " ") print(chr(0x2708), end = " ") print(chr(0x00A5), end = " ") print(chr(0x266b)) #ord()返回字符的unicode编码 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'))
运行截图:
实验任务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}')
运行截图:
5-2
源码
#神奇的pi, 与大饼 import math text = ''' 好奇心是人的天性。 理想情况下,学习新东西是让人愉快的事。 但在学校里的学习似乎有点像苦役。 有时候,需要画一个大饼,每次尝试学一些新鲜的,才会每天变得更好一点点。 ''' print(text) r = float(input('给学习画一个大饼,大饼要做的很大,半径要这么大:')) circle = 2*math.pi*r print(f'绕起来,大饼的圆周有这么长,{circle},够不够激发你探索未知的动力...')
运行截图:
实验任务6
task6.py
实验源码
#计算y = x的365次方 1 x = eval(input()) 2 y = pow(x, 365) 3 print(f'{x:.2f}的365次方:{y:.2f}')
运行截图:
实验任务7
task7.py
实验源码
#task7 T0=eval(input()) p,c,k = 1.038,3.7,5.4*10**(-3) M_small,M_big = 47,67 #M是质量,p是密度,c是比热容,k是热导率 Tw = 100 #Tw是水沸腾的温度(以℃计) Ty = 70 #Ty是放入水中之前的原始温度(以℃计) import math a=M_big **(2/3)*c*p**(1/3) b=k*((math.pi)**2)*(4*(math.pi)/3)**(2/3) c=math.log(0.76*(T0-Tw)/(Ty-Tw)) t= a/b*c minute = int(t // 60) second = round(t % 60) print('T0={}℃,t={}分{}秒'.format(T0,minute,second))
运行截图
标签:截图,初体验,end,format,Python,编程,chr,源码,print From: https://www.cnblogs.com/zyaiyj-www-2003/p/17209786.html