实验1

实验任务1:
task1_1.py
#
print输出的几种用法 #用法1:用于输出单个字符或单个变量 print('hey,u') #用法2:用于输出多个数据项,用逗号分隔 print('hey,u') x,y,z=1,2,3 print(x,y,z) #用法3:用户混合字符串与变量值 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)
运行截图:
task1_2.py

# 使用字符串的format()方法,对输出数据项进行格式化
x1, y1 = 1.2, 3.57
x2, y2 = 2.26, 8.7
# 输出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('输出3'))
print('x1 = {:>15.1f}, y1 = {:>15.1f}'.format(x1, y1))
print('x2 = {:>15.1f}, y2 = {:>15.1f}'.format(x2, y2))
运行截图:

task1_3.py

# 使用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*'=')

运行截图:

print()的几种常用用法:

用法1:用于输出单个字符串或单个变量

用法2:用于输出多个数据项,用逗号分隔

用法3:用户混合字符串和变量值

用法4:使用字符串的format()方法,对输出数据项进行格式化

用法5:使用f-string方式输出数据并控制格式

实验任务2:

task2_1.py

# 内置函数eval()
r1 = eval('1 + 2')
print(type(r1), r1)
r2 = eval('[1, 6, 7.5]')
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的用法为把字符串转换为python表达式,相当于把圆括号中的内容,两侧引号去掉

实验任务3:

task3——_1.py

# 浮点数简单运算
ans1 = 0.1 + 0.2
print(f'0.1 + 0.2 = {ans1}')
from decimal import Decimal
ans2 = Decimal('0.1') + Decimal('0.2')
print(f'0.1 + 0.2 = {ans2}')

1.因为0.1与0.2在二进制浮点中没有精确的表示,其中的误差将妨碍可靠的相等性检验,并且误差还会不断累积,而对于 decimal 浮点数,会精确表示0.3

2.机器进行二进制计算引入的误差,为了消除这样的误差,进行更加精确的浮点计算,就要是用到decimal模块。我们直接导入decimal模块里的所有函数。现在我们可以通过Decimal函数构建十进制浮点数,并进行常规的数值计算。注意,Decimal函数传入的浮点数必须加引号(”),表示成字符串形式。传入整数时就不用加。Decimal函数的对象类型是decimal.Decimal,可以通过type()函数查看。

浮点数据转换成Decimal。Decimal函数并不建议传入浮点型数据,因为浮点型数据本身就是不精确的。

实验任务4:

task4_1.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'))

运行截图:

总结:chr()返回unicode编码对应的字符,而ord()返回字符的unicode编码

实验任务5:

task5_1.py

# 简单的数学计算
from math import sqrt
n = float(input('输入一个数:'))
# python中计算开平方的两种方式
ans1 = sqrt(n)
ans2 = n**0.5
print('%.2f的平方根是: %.2f' %(n, ans1))
print('{:.2f}的平方根是: {:.2f}'.format(n, ans2))
print(f'{n:.2f}的平方根是: {ans2:.2f}')

task5_2.py

# 神奇的pi, 与大饼
from math import pi
text = '''
好奇心是人的天性。
理想情况下,学习新东西是让人愉快的事。
但学校里的学习似乎有点像苦役。
有时候,需要画一个大饼,每次尝试学一些新鲜的,才会每天变得更好一点点。
'''
print(text)
r = float(input('给学习画一个大饼,大饼要做的很大,半径要这么大: '))
circle = 2*pi*r
print(f'绕起来,大饼的圆周有这么长, {circle}, 够不够激发你探索未知的动力...')

实验截图:

 

实验任务6:

task6_1.py

#task6_1.py
x=eval(input("输入x:"))
y=x**365
print("{}的365次方:".format(x),y)

实验任务7:

task7_1.py

 

#task7_1.py
from math import pi
T0=eval(input("输入T0:"))
M=67
p=1.038
c=3.7
K=0.054
Tw=100
Ty=70
import math
t=(M**(2/3)*c*p**(1/3))/(K*pi**2*(4*pi/3)**(2/3))*math.log(((0.76*(T0-Tw))/(Ty-Tw)),math.e)
T1=int(t)
T2=int((t-T1)/60)
print("T0={}℃,t=".format(T0),"{}分".format(T1),"{}秒".format(T2))

实验总结:

通过这次实验,我体验print()的不同用法,及其丰富的格式控制方式。在Python开发环境下,编写、运行以下Python代码,结合注释,观察运行结果,理解并学会使用内置函数eval(), input()。学习了解unicode编码,及内置函数chr()和ord()的功能。观察代码中是如何导入数学库math从而使用其内部工具的三引号的用法。


posted @ 2022-03-23 19:57  刘封云  阅读(64)  评论(2编辑  收藏  举报