实验1 python开发环境使用和程编初体验

实验任务一

        源代码task1_1.py

         View Code

        运行截图

             

         源代码task1_2.py

          View Code

         运行截图

            

        源代码task1_3.py

    
1 name1,age1='Bill',19
2 name2,age2='Hellen',18
3 title='personnel Information'
4 
5 print(f'{title:=^40}')
6 print(f'name1:{name1:10} age1:{age1:3}')
7 print(f'name2:{name2:10} age2:{age2:3}')
8 print('='*40)
View Code

          运行截图

          

         

          总结:print用法

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

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

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

 

实验任务二

       源代码task2_1.py   

  
 1 r1=eval('1+2')
 2 print(type(r1),r1)
 3 
 4 r2=eval('1+2j')
 5 print(type(r2),r2)
 6 
 7 r3=eval('"python"')
 8 print(type(r3),r3)
 9 
10 r4=eval('7,42')
11 print(type(r4),r4)
View Code

       运行截图

        

      源代码task2_2.py

 
1 x,y=eval(input('Enter two oprands:'))
2 ans=x+y
3 
4 print(f'{x}+{y}={ans}')
5 print(f'{type(x)}+{type(y)}={type(ans)}')
View Code

      运行截图  

       

 

      总结:eval()的用法:

      把字符串转换为python表达式,相当于把圆括号中的内容,两侧引号去掉

实验任务三

     源代码task3.py

  
1 ans1=0.1+0.2
2 print(f'0.1+0.2={ans1}')
3 
4 import decimal
5 
6 ans2=decimal.Decimal('0.1')+decimal.Decimal('0.2')
7 print(f'0.1+0.2={ans2}')
View Code

     运行截图

      

     为什么line4输出结果显示存在误差?

     因为计算机以二进制方式存储数据,对于小数的运算有天然的缺陷,计算机只能以极大限度的进行近似取值。

     查阅decimal模块资料,尝试分析和思考,decimal模块中的工具Decimal()是如何进行浮点运算的。

      Python中,小数加减法出现的精度问题非常常见,一般使用decimal模块进行处理,因为decimal.Decimal是基于字符串进行处理的,所以在使用时一定注意前期转换为字符串, 后期转换会对应的浮点类型。

实验任务四

    源代码task4.py

  
 1 print(chr(0x1f660),end="")
 2 print(chr(0x1f601),end="")
 3 print(chr(0x1f602),end="")
 4 print(chr(0x1f603),end="")
 5 print(chr(0x1f604))
 6 
 7 print(chr(10000),end="")
 8 print(chr(0x025b),end="")
 9 print(chr(0x2708),end="")
10 print(chr(0x00A5),end="")
11 print(chr(0x266b))
12 
13 print(ord('a'),end="")
14 print(ord('b'),end="")
15 print(ord('c'))
16 
17 print(ord('A'),end="")
18 print(ord('B'),end="")
19 print(ord('C'))
20 
21 print(ord('0'),end="")
22 print(ord('1'),end="")
23 print(ord('2'))
View Code

    运行截图    

     

    总结:

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

    ord()返回字符的unicode编码

实验任务五

    源代码task5_1.py

  
 1 import math
 2 
 3 n=float(input('输入一个数:'))
 4 
 5 ans1=n**0.5
 6 ans2=pow(n,0.5)
 7 ans3=math.sqrt(n)
 8 
 9 print('%.2f的平方根是:%.2f'%(n,ans1))
10 print('{:.2f}的平方根是:{:.2f}'.format(n,ans2))
11 print(f'{n:.2f}的平方根是:{ans3:.2f}')
View Code

    运行截图    

    

   源代码task5_2.py

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

    运行截图     

    

实验任务六

    源代码task6.py

 
1 x=float(input())
2 y=x**365
3 print('{}的365次方:{}'.format(x,y))
View Code

   运行截图

     

实验任务七

    源代码task7.py

 
 1 import math
 2 M=67
 3 c=3.7
 4 k=0.0054
 5 a=1.038
 6 b=math.pi
 7 Tw=100
 8 Ty=70
 9 T0=float(input())
10 ans1=(M**(2/3))*c*(a**(1/3))
11 ans2=(k*b*b*((4*b/3)**(2/3)))
12 ans3=math.log(0.76*(T0-Tw)/(Ty-Tw))
13 T=ans1*ans3/ans2
14 f=T//60
15 m=int((T%60)+0.5)
16 print('T0={}°C,t={:.0f}分{}秒'.format(T0,f,m))
View Code

    运行截图

    

 实验任务八

     源代码task8_1.py

  
 1 print('欢迎使用家用电器销售系统!')
 2 print('*'*60)
 3 print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
 4 print('-'*60)
 5 print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%10.2f'%5999.00,'%10d'%20)
 6 print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%10.2f'%6998.00,'%10d'%15)
 7 print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%10.2f'%1999.00,'%10d'%10)
 8 print('%-10s'%'0004','%-10s'%'空调','%-10s'%'格力','%10.2f'%3900.00,'%10d'%0)
 9 print('%-10s'%'0005','%-10s'%'热水器','%-10s'%'美的','%10.2f'%688.00,'%10d'%30)
10 print('%-10s'%'0006','%-10s'%'笔记本','%-10s'%'联想','%10.2f'%5699.00,'%10d'%10)
11 print('%-10s'%'0007','%-10s'%'微波炉','%-10s'%'苏泊尔','%10.2f'%480.50,'%10d'%33)
12 print('%-10s'%'0008','%-10s'%'投影仪','%-10s'%'松下','%10.2f'%1250.00,'%10d'%12)
13 print('%-10s'%'0009','%-10s'%'吸尘器','%-10s'%'飞利浦','%10.2f'%999.00,'%10d'%9)
14 print('-'*60)
15 
16 product_id=input('请输入您要购买的产品编号:')
17 price=float(input('请输入您要购买的产品价格:'))
18 count=int(input('请输入您要购买的产品数量:'))
19 
20 print('购买成功,您需要支付',price*count,'')
21 print('谢谢您的光临,下次再见!')
View Code

      运行截图

      

    源代码task8_2.py

 
 1 print('欢迎使用家用电器销售系统!')
 2 print('产品和价格信息如下:')
 3 print('*'*60)
 4 print('{:<10}'.format('编号'),'{:<10}'.format('名称'),'{:<10}'.format('品牌'),'{:<10}'.format('价格'),'{:<10}'.format('库存数量'))
 5 print('-'*60)
 6 print('{:<10}'.format('0001'),'{:<10}'.format('电视机'),'{:<10}'.format('海尔'),'{:<10.2f}'.format(5999.00),'{:>10}'.format(20))
 7 print('{:<10}'.format('0002'),'{:<10}'.format('冰箱'),'{:<10}'.format('西门子'),'{:<10.2f}'.format(6998.00),'{:>10}'.format(15))
 8 print('{:<10}'.format('0003'),'{:<10}'.format('洗衣机'),'{:<10}'.format('小天鹅'),'{:<10.2f}'.format(1999.00),'{:>10}'.format(10))
 9 print('{:<10}'.format('0004'),'{:<10}'.format('空调'),'{:<10}'.format('格力'),'{:<10.2f}'.format(3900.00),'{:>10}'.format(0))
10 print('{:<10}'.format('0005'),'{:<10}'.format('热水器'),'{:<10}'.format('美的'),'{:<10.2f}'.format(688.00),'{:>10}'.format(30))
11 print('{:<10}'.format('0006'),'{:<10}'.format('笔记本'),'{:<10}'.format('联想'),'{:<10.2f}'.format(5699.00),'{:>10}'.format(10))
12 print('{:<10}'.format('0007'),'{:<10}'.format('微波炉'),'{:<10}'.format('苏泊尔'),'{:<10.2f}'.format(480.50),'{:>10}'.format(33))
13 print('{:<10}'.format('0008'),'{:<10}'.format('投影仪'),'{:<10}'.format('松下'),'{:<10.2f}'.format(1250.00),'{:>10}'.format(12))
14 print('{:<10}'.format('0009'),'{:<10}'.format('吸尘器'),'{:<10}'.format('飞利浦'),'{:<10.2f}'.format(999.00),'{:>10}'.format(9))
15 print('-'*60)
16 product_id = input('请输入您要购买的产品编号:')
17 price = float(input('请输入您要购买的产品价格:'))
18 count = int(input('请输入您要购买的产品数量:'))
19 print('购买成功,您需支付', price*count, '')
20 print('谢谢您的光临,下次再见!')
View Code

   

   源代码task8_3.py

 
 1 print('欢迎使用家用电器销售系统!')
 2 print('产品和价格信息如下:')
 3 print('*' * 60)
 4 print('-' * 60)
 5 print(f'{"编号":<10}{"名称":<10}{"品牌":<10}{"价格":<10}{"库存数量":<10}')
 6 print(f'{"0001":<10}{"电视机":<10}{"海尔":<10}{5999.00:<10.2f}{20:>10}')
 7 print(f'{"0002":<10}{"冰箱":<10}{"西门子":<10}{6998.00:<10.2f}{15:>10}')
 8 print(f'{"0003":<10}{"洗衣机":<10}{"小天鹅":<10}{1999.00:<10.2f}{10:>10}')
 9 print(f'{"0004":<10}{"空调":<10}{"格力":<10}{3900.00:<10.2f}{0:>10}')
10 print(f'{"0005":<10}{"热水器":<10}{"美的":<10}{688.00:<10.2f}{30:>10}')
11 print(f'{"0006":<10}{"笔记本":<10}{"联想":<10}{5699.00:<10.2f}{10:>10}')
12 print(f'{"0007":<10}{"微波炉":<10}{"苏泊尔":<10}{480.50:<10.2f}{22:>10}')
13 print(f'{"0008":<10}{"投影仪":<10}{"松下":<10}{1250.00:<10.2f}{12:>10}')
14 print(f'{"0009":<10}{"吸尘器":<10}{"飞利浦":<10}{999.00:<10.2f}{9:>10}')
15 print('-' * 60)
16 product_id = input('请输入您要购买的产品编号:')
17 price = float(input('请输入您要购买的产品价格:'))
18 count = int(input('请输入您要购买的产品数量:'))
19 print('购买成功,您需要支付', price * count, '')
20 print("谢谢您的光临,下次再见!")
View Code

    

 

posted @ 2023-03-15 00:50  娄泽涛  阅读(42)  评论(0)    收藏  举报