task1_1.py
1 print('hey, u') 2 print('hey', ' u') 3 x,y,z = 1,2,3 4 print(x, y, z) 5 print('x = %d, y = %d, z = %d' %(x,y,z)) 6 print('x = {}, y = {}, z = {}'.format(x,y,z)) 7 print(f'x = {x}, y = {y}, z = {z}') 8 print(x) 9 print(y) 10 print(z) 11 print(x, end=' ') 12 print(y, end=' ') 13 print(z)
task1_2.py
1 x1, y1 = 1.2, 3.57 2 x2, y2 = 2.26, 8.7 3 print('{:-^40}'.format('输出1')) 4 print('x1 = {}, y1 = {}'.format(x1, y1)) 5 print('x2 = {}, y2 = {}'.format(x2, y2)) 6 print('{:-^40}'.format('输出2')) 7 print('x1 = {:.1f}, y1 = {:.1f}'.format(x1, y1)) 8 print('x2 = {:.1f}, y2 = {:.1f}'.format(x2, y2)) 9 print('{:-^40}'.format('输出3')) 10 print('x1 = {:<15.1f}, y1 = {:<15.1f}'.format(x1, y1)) 11 print('x2 = {:<15.1f}, y2 = {:<15.1f}'.format(x2, y2)) 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))
task1_3.py
1 name1, age1 = 'Bill', 19 2 name2, age2 = 'Hellen', 18 3 title = 'Personnel Information' 4 print(f'{title:=^40}') 5 print(f'name: {name1:10}, age: {age1:3}') 6 print(f'name: {name2:10}, age: {age2:3}') 7 print(40*'=')
实验一(总结):
print输出的几种用法:
1.输出单个字符串或单个变量
2.输出多个数据项,用逗号分隔
3.用户混合字符串和变量值
4.换行
2.实验任务2
task2_1.py
1 r1 = eval('1 + 2') 2 print(type(r1), r1) 3 r2 = eval('[1, 6, 7.5]') 4 print(type(r2), r2) 5 r3 = eval('"python"') 6 print(type(r3), r3) 7 r4 = eval('7, 42') 8 print(type(r4), r4)
task2_2.py
1 x, y = eval(input('Enter two oprands: ')) 2 ans = x + y 3 print(f'{x} + {y} = {ans}') 4 print(f'{type(x)} + {type(y)} = {type(ans)}')
函数eval()的用法:
1.把字符串转换为python表达式,相当于把圆括号中内容两侧的引号去掉 2.组合使用内置函数eval()和input()
3.实验任务3
task3.py
1 ans1 = 0.1 + 0.2 2 print(f'0.1 + 0.2 = {ans1}') 3 from decimal import Decimal 4 ans2 = Decimal('0.1') + Decimal('0.2') 5 print(f'0.1 + 0.2 = {ans2}')
line4存在误差:二进制计算中浮点数无法精确表示,只能舍入后存储
Decimal运算:基于整数或浮点数构造将执行该整数或浮点值的精确转换,以十进制方法计算,因此不会产生二进制计算中的误差。
4.实验任务4
task4.py
1 print(chr(0x1f600), end = " ") 2 print(chr(0x1f601), end = " ") 3 print(chr(0x1f602), end = " ") 4 print(chr(0x1f603), end = " ") 5 print(chr(0x1f604)) 6 print(chr(10000), end=" ") 7 print(chr(0x025b), end=" ") 8 print(chr(0x2708), end=" ") 9 print(chr(0x00A5), end=" ") 10 print(chr(0x266b)) 11 print(ord('a'), end = " ") 12 print(ord('b'), end = " ") 13 print(ord('c')) 14 print(ord('A'), end = " ") 15 print(ord('B'), end = " ") 16 print(ord('C')) 17 print(ord('0'), end = " ") 18 print(ord('1'), end = " ") 19 print(ord('2'))
chr()和ord()作用:
ord函数返回一个字符对应的unicode编码,chr函数反过来,返回一个unicode编码对应的字符。
5.实验任务5
task5_1.py
1 from math import sqrt 2 n = float(input('输入一个数:')) 3 ans1 = sqrt(n) 4 ans2 = n**0.5 5 print('%.2f的平方根是: %.2f' %(n, ans1)) 6 print('{:.2f}的平方根是: {:.2f}'.format(n, ans2)) 7 print(f'{n:.2f}的平方根是: {ans2:.2f}')
task5_2.py
1 from math import pi 2 text = ''' 3 好奇心是人的天性。 4 理想情况下,学习新东西是让人愉快的事。 5 但学校里的学习似乎有点像苦役。 6 有时候,需要画一个大饼,每次尝试学一些新鲜的,才会每天变得更好一点点。 7 ''' 8 print(text) 9 r = float(input('给学习画一个大饼,大饼要做的很大,半径要这么大: ')) 10 circle = 2*pi*r 11 print(f'绕起来,大饼的圆周有这么长, {circle}, 够不够激发你探索未知的动力...')
6.实验任务6
task6.py
1 x=float(input("从键盘输入:")) 2 y=x**(365) 3 print(x,"的365次方:",y)
7.实验任务7
task7.py
from math import pi from math import log p=1.038 c=3.7 K=5.4*10**(-3) M=67 Tw=100 Ty=70 To=float(input("从键盘输入To=:")) t=(M**(2/3)*c*p**(1/3))/(K*pi**2*((4*pi)/3)**(2/3))*log(0.76*((To-Tw)/(Ty-Tw))) print(f"To={To}°C,t={t//60}分{t%60}秒")
五、实验总结
·print(),eval(),chr(),ord()的用法,二进制浮点运算出现误差的原因,python的简洁,复杂函数的运算等。
我,感觉,好难......哭泣.jpg,但是又好开心