首页 > 编程语言 >实验1 Python开发环境使用和编程初体验

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

时间:2023-03-11 18:24:15浏览次数:46  
标签:%- 10 初体验 end format Python 编程 print 10s

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

实验源码:

#task1_2 用format对输出数据进行格式化

x1,y1 = 1.2, 3.57
x2,y2 = 2.26 , 8.7



#输出1
print('{:-^40}'.format('输出1')) #{:-^40}:宽度40列,“^”居中对齐,“-”空白用-补齐
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))#“:.1f”:保留一位小数
print('x2 = {:.1f} , y2 = {:.1f}'.format(x2,y2))

#输出3
print('{:-^40}'.format('输出3'))
print('x1 = {:<15.1f} , y1 = {:<15.1f}'.format(x1,y1))#“.<15.1f”:宽度15列,“<”左对齐,保留一位小数,空白补空格
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))#“.<15.1f”:宽度15列,“>”右对齐,保留一位小数,空白补空格
print('x2 = {:>15.1f} , y2 = {:>15.1f}'.format(x2,y2))

运行测试截图:

 

task1_3

实验源码:

#"f-string"

name1, age1 = 'Bill' , 19
name2, age2 = 'Hellen' , 18
title = 'Personnel Inforamtion'


print(f'{title:=^40}')#输出title的值,“=^40”宽度40列,居中对齐,用=补充空白
print(f'name = {name1:10} age: {age1:3}')#10 3 表示输出宽度
print(f'name = {name2:10} age: {age2:3}')
print(40*'=')

运行测试截图:

实验总结:

输出多个数据项用逗号隔开

不同行的print()需要end=‘ ’来实现不换行

变量和字符串一起输出需要特殊格式如print(f'x={x}')

format用于格式化输出

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

运行测试截图:

实验总结:

eavl()用于将字符串转换为数字、列表、元组等形式

实验任务3:

task3_1

实验源码:

#浮点数运算

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}')

运行测试截图:

计算机内部是二进制运算,所以会有误差,

出现误差可以使用decimal处理,decimal处理时需要转化成字符串

实验任务4:

task4

实验源码:

#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()输入0到255之间的整数,返回对应的字符

ord()输入字符,返回字符串对应的ASCII值或Unicode值

实验任务5:

task5_1

实验源码:

import math

n = float(input('enter a number'))

#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))#str.format()
print(f'{n:.2f}的平方根是:{ans3:.2f}')#f-string

运行测试截图:

task5_2

实验源码:

# 神奇的pi,与大饼

import math

text = '''
        好奇心是人的天性。
        理想情况下,学习新东西是让人愉快的事。
        但学校里的学习似乎有点像苦役。
        有时候,需要画一个大饼,每次尝试学一些新鲜的,才会每天变得更好一点点。
       '''
print(text)

r = float(input('给学习画一个大饼,大饼要做的很大,半径要这么大: '))

circle = 2*math.pi*r

print(f'绕起来,大饼的圆周有这么长, {circle}, 够不够激发你探索未知的动力...')

运行测试截图:

实验任务6:

task6:

实验源码:

x = float(input('enter a number'))
y = x** 365
print(f'{x}的365次方:{y}')

运行测试截图:

实验任务7:

task7

实验源码:

import math
p = 1.038
c = 3.7
K = 0.0054
Tw = 100
Ty = 70
T0 = int(input('enter your temperture'))
M = int(input('enter an egg'))

x1=M**(2/3)*c*p**(1/3)
x2=(0.76 * (T0 - Tw) / (Ty - Tw))
a=math.pi
x3=K*a**2*(4*a/3)**(2/3)
x4=math.log(x2)
t=x1/x3*x4
min = t // 60
sec = t % 60

print(f'T0 = {T0:.2f}℃',f't={min:.2f}分{sec:.2f}秒')

运行测试截图:

实验任务8:

task8_1

实验源码:

"""
家用电器销售系统
V1.0
"""

#欢迎信息
print('欢迎使用家用电器销售系统!')

#产品信息列表
print('产品和价格信息如下:')
print('*********************************************************')
print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
print('---------------------------------------------------------------------')
print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%-10.2f'%5999.00,'%10d'%20)
print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%-10.2f'%6998.00,'%10d'%15)
print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%-10.2f'%1999.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.00,'%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('-----------------------------------------------------------------------')

#用户输人信息
prodect_id=input('请输入您要购买的产品编号:')
price=float(input('请输入您要购买的产品价格:'))
count=int(input('请输入您要购买的产品数量:'))

#计算金额
print('购买成功,您需要支付',price * count,'元')

#退出系统
print("谢谢您的光临,下次再见!")

运行测试截图:

task8_2

实验源码:

"""
家用电器销售系统
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('-----------------------------------------------------------------------')

#用户输人信息
prodect_id=input('请输入您要购买的产品编号:')
price=float(input('请输入您要购买的产品价格:'))
count=int(input('请输入您要购买的产品数量:'))

#计算金额
print('购买成功,您需要支付',price * count,'元')

#退出系统
print("谢谢您的光临,下次再见!")

运行测试截图:

 

task8_3

实验源码:

"""
家用电器销售系统
v1.0
"""

bh = '编号'
mc = '名称'
pp = '品牌'
jg = '价格'
kcs = '库存数量'
x1 = '电视机'
x2 ='冰箱'
x3 = '洗衣机'
x4 = '空调'
x5 = '热水器'
x6 = '笔记本'
x7 = '微波炉'
x8 = '投影仪'
x9 = '吸尘器'
A = '0001'
B = '0002'
C = '0003'
D = '0004'
E = '0005'
G = '0006'
H = '0007'
I = '0008'
J = '0009'
hr = '海尔'
xmz = '西门子'
xte = '小天鹅'
gl = '格力'
md = '美的'
lx = '联想'
sbe = '苏泊尔'
sx = '松下'
flp = '飞利浦'

#欢迎信息
print('欢迎使用家用电器销售系统!')
#产品信息列表
print('产品和价格信息如下:')
print('*********************************************************')
print('---------------------------------------------------------------------')
print(f'{bh:<10}',f'{mc:<10}',f'{pp:<10}',f'{jg:<10}',f'{kcs:>10}')
print(f'{A:<10}',f'{x1:<10}',f'{hr:<10}',f'{5999.00:<10.2f}',f'{20:>10}')
print(f'{B:<10}',f'{x2:<10}',f'{xmz:<10}',f'{6998.00:<10.2f}',f'{15:>10}')
print(f'{C:<10}',f'{x3:<10}',f'{xte:<10}',f'{1999.00:<10.2f}',f'{10:>10}')
print(f'{D:<10}',f'{x4:<10}',f'{gl:<10}',f'{3900.00:<10.2f}',f'{0:>10}')
print(f'{E:<10}',f'{x5:<10}',f'{md:<10}',f'{688.00:<10.2f}',f'{30:>10}')
print(f'{G:<10}',f'{x6:<10}',f'{lx:<10}',f'{5699.00:<10.2f}',f'{10:>10}')
print(f'{H:<10}',f'{x7:<10}',f'{sbe:<10}',f'{480.50:<10.2f}',f'{33:>10}')
print(f'{I:<10}',f'{x8:<10}',f'{sx:<10}',f'{1250.00:<10.2f}',f'{12:>10}')
print(f'{J:<10}',f'{x9:<10}',f'{flp:<10}',f'{999.00:<10.2f}',f'{9:>10}')
print('-----------------------------------------------------------------------')

#用户输人信息
prodect_id=input('请输入您要购买的产品编号:')
price=float(input('请输入您要购买的产品价格:'))
count=int(input('请输入您要购买的产品数量:'))

#计算金额
print('购买成功,您需要支付',price * count,'元')
#退出系统
print("谢谢您的光临,下次再见!")

运行测试截图:

 

标签:%-,10,初体验,end,format,Python,编程,print,10s
From: https://www.cnblogs.com/sxy59e/p/17206630.html

相关文章

  • 关于python中使用break语句跳出两层循环的方法实现
    break多用于for循环语句中,用于跳出当前for循环,不对当前for循环后面的元素进行遍历操作了for循环语句中,如果使用到break语句,一般都是因为达到了某个条件,才执行break的,因此......
  • [STL] 3 迭代器概念与 traits 编程技法
    3迭代器概念与traits编程技法3.1迭代器设计思维——STL关键所在中心思想在于将数据容器和算法分开。3.2迭代器是一种智能指针迭代器最重要的编程工作是对opera......
  • 第 1 章 C++编程基础 Basic C++ programming
    1.1如何撰写C++程序_HowtoWriteaC++Program练习1.4,在终端上让用户输入fastname和lastname并打印出来练习1.4#include<iostream>#include<vector>#include......
  • 创建python虚拟环境(深度学习)
    本文根据杨力-中量大AI老师的配置进行记录学习:1.打开cmd     2.创建虚拟环境  3.激活虚拟环境 4.文件夹中的目录结构  以后想用python就来这里就......
  • 【Python】main函数 if name=='main' 详解
    引言Python代码print('hellowword')代码执行顺序我们可以看到Python仅仅用了一行代码即可完成其他编程语言多行的输出Hello,World其他的编程语言像C/C++/C#/JA......
  • Python 网络爬虫
    必考点1.导入库2.定义变量url=''''link=''''headers={user-agent:} 3.执行代码r=requests.get('''地址''',headers=headers,timeout=10)response.html ......
  • 搭建良好编写体验的webgl编程环境 vscode+vit
    因为webgl代码是以字符串的形式嵌入在javascript代码中,这对于我们编写webgl代码的体验不友好,本文介绍如何搭建友好webgl编程环境:需要安装的vscode插件WebGLGLSLEdito......
  • 并发编程-进程
    进程线程线程和进程的区别进程间通讯管道消息队列信号量共享内存套接字消息队列和管道的区别没有行动,懒惰就会生根发芽。没有梦想,堕落就......
  • 让Python PYQT5使用微软流畅设计体系UI
    PyQt-Fluent-Widgets项目地址: https://github.com/zhiyiYo/PyQt-Fluent-Widgets总所周知,Python一般的UI(比如Tkinter)肥肠的丑陋,肥肠的不现代化。蛋是现在微软有流畅设......
  • c#异步编程学习记录之一 async和await
    async放在方法名前面,表示当前方法是一个异步的方法await等待返回结果,一般这个后面会跟着一个比较耗时的操作示例如下:Console.WriteLine("Hello,World!")......