实验1

实验任务1

task1_1.py

实验代码:

#用法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

实验代码:

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('输出4'))
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.用于输出单个字符串或单个变量

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

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

4.输出后是否换行

 

实验任务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

实验代码:

x, y = eval(input('Enter two oprands: ')) # 输入的两个操作数之间用逗号分隔(英文半角输入法下的逗号)
ans = x + y
print(f'{x} + {y} = {ans}')
print(f'{type(x)} + {type(y)}  = {type(ans)}')

 

 
实验结果截图:

 

 实验总结:函数eval()的用法

1.把字符串转化为python表达式

2.与input()函数组合使用

 

实验任务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

实验代码:

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))
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()和ord()的用法

1.chr()返回unicode编码对应的字符

2.ord()返回字符的unicode编码

 

实验任务5

task5_1.py

实验代码:

import math

n = float(input('输入一个数:'))
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

task6.py

实验代码:

x = eval(input('输入一个在集合{1.0, 1.01, 0.99}之间的数'))
y = x**365
print(f'{x:.2f}的365次方:{y:.2f}')
 

 

实验结果截图:

 

 

实验任务7

task7.py

实验代码:

from math import pi
p = 1.038
c = 3.7
K = 5.4*10**(-3)
M = 67
Tw = 100
Ty = 70
T0 = eval(input('请输入温度:'))
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)
a = int(t//60)
b = int(t%60)
print('T0=',T0,'℃,t=',a,'',b,'')

 

 
实验结果截图:

 

 

实验任务8

task8_1.py

实验代码:

"""
家用电器销售系统
v1.0
"""
#欢迎信息
print('欢迎使用家用电器销售系统!')
#产品信息列表
print('产品和价格信息如下: ')
print('***********************************************************')
print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
print('-----------------------------------------------------------')
print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%10.2f'%5900.00,'%10d'%20)
print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%10.2f'%6998.00,'%10d'%15)
print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%10.2f'%1900.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('-----------------------------------------------------------')

#用户输入信息
product_id=input('请输入您要购买的产品编号:')
price=float(input('请输入您要购买的产品价格:'))
count=int(input('请输入您要购买的产品数量:'))
#计算金额
print('购买成功,您需要支付',price*count,'')
#退出系统
print('谢谢您的光临,下次再见!')

 

 
实验结果截图:

 

 

task8_2.py

实验代码:

"""
家用电器销售系统
v1.0
"""
#欢迎信息
print('欢迎使用家用电器销售系统!')
#产品信息列表
print('产品和价格信息如下: ')
print('***********************************************************')
print('{:<10}'.format('编号'),'{:<10}'.format('名称'),'{:<10}'.format('品牌'),'{:<10}'.format('价格'),'{:<10}'.format('库存数量'))
print('---------------------------------------------------------------------')
print('{:<10}'.format('0001'),'{:<10}'.format('电视机'),'{:<10}'.format('海尔'),'{:<10.2f}'.format(5999.00),'{:>10}'.format(20))
print('{:<10}'.format('0002'),'{:<10}'.format('冰箱'),'{:<10}'.format('西门子'),'{:<10.2f}'.format(6998.00),'{:>10}'.format(15))
print('{:<10}'.format('0003'),'{:<10}'.format('洗衣机'),'{:<10}'.format('小天鹅'),'{:<10.2f}'.format(1999.00),'{:>10}'.format(10))
print('{:<10}'.format('0004'),'{:<10}'.format('空调'),'{:<10}'.format('格力'),'{:<10.2f}'.format(3900.00),'{:>10}'.format(0))
print('{:<10}'.format('0005'),'{:<10}'.format('热水器'),'{:<10}'.format('美的'),'{:<10.2f}'.format(688.00),'{:>10}'.format(30))
print('{:<10}'.format('0006'),'{:<10}'.format('笔记本'),'{:<10}'.format('联想'),'{:<10.2f}'.format(5699.00),'{:>10}'.format(10))
print('{:<10}'.format('0007'),'{:<10}'.format('微波炉'),'{:<10}'.format('苏泊尔'),'{:<10.2f}'.format(480.50),'{:>10}'.format(33))
print('{:<10}'.format('0008'),'{:<10}'.format('投影仪'),'{:<10}'.format('松下'),'{:<10.2f}'.format(1250.00),'{:>10}'.format(12))
print('{:<10}'.format('0009'),'{:<10}'.format('吸尘器'),'{:<10}'.format('飞利浦'),'{:<10.2f}'.format(999.00),'{:>10}'.format(9))
print('-----------------------------------------------------------')

#用户输入信息
product_id=input('请输入您要购买的产品编号:')
price=float(input('请输入您要购买的产品价格:'))
count=int(input('请输入您要购买的产品数量:'))
#计算金额
print('购买成功,您需要支付',price*count,'')
#退出系统
print('谢谢您的光临,下次再见!')
 

 

实验结果截图:

 

 

task8_3.py

实验代码:

"""
家用电器销售系统
v1.0
"""
#欢迎信息
print('欢迎使用家用电器销售系统!')
#产品信息列表
print('产品和价格信息如下: ')
print('***********************************************************')
a = ['编号','名称','品牌','价格','库存数量']
b = ["0001","0002","0003","0004","0005","0006","0007","0008","0009"]
c = ['电视机','冰箱','洗衣机','空调','热水器','笔记本','微波炉','投影仪','吸尘器']
d = ['海尔','西门子','小天鹅','格力','美的','联想','苏泊尔','松下','飞利浦']
e = [5999.00,6998.00,1999.00,3900.00,688.00,5699.00,480.50,1250.00,999.00]
f = [20,15,10,0,30,10,33,12,9]
print(f'{a[0]:10s}{a[1]:10s}{a[2]:10s}{a[3]:10s}{a[4]:10s}')
print('-----------------------------------------------------------')
print(f'{b[0]:10s}{c[0]:10s}{d[0]:10s}{e[0]:10.2f}{f[0]:10d}')
print(f'{b[1]:10s}{c[1]:10s}{d[1]:10s}{e[1]:10.2f}{f[1]:10d}')
print(f'{b[2]:10s}{c[2]:10s}{d[2]:10s}{e[2]:10.2f}{f[2]:10d}')
print(f'{b[3]:10s}{c[3]:10s}{d[3]:10s}{e[3]:10.2f}{f[3]:10d}')
print(f'{b[4]:10s}{c[4]:10s}{d[4]:10s}{e[4]:10.2f}{f[4]:10d}')
print(f'{b[5]:10s}{c[5]:10s}{d[5]:10s}{e[5]:10.2f}{f[5]:10d}')
print(f'{b[6]:10s}{c[6]:10s}{d[6]:10s}{e[6]:10.2f}{f[6]:10d}')
print(f'{b[7]:10s}{c[7]:10s}{d[7]:10s}{e[7]:10.2f}{f[7]:10d}')
print(f'{b[8]:10s}{c[8]:10s}{d[8]:10s}{e[8]:10.2f}{f[8]:10d}')
print('-----------------------------------------------------------')

#用户输入信息
product_id=input('请输入您要购买的产品编号:')
price=float(input('请输入您要购买的产品价格:'))
count=int(input('请输入您要购买的产品数量:'))
#计算金额
print('购买成功,您需要支付',price*count,'')
#退出系统
print('谢谢您的光临,下次再见!')

 

 
实验结果截图:

 

 

 

 

posted @ 2023-03-12 20:51  小熊tot  阅读(31)  评论(0)    收藏  举报