python 第一次作业


因为笔者有一些 c/c++ 语言的基础,所以应该学 python 会稍微简单一些

格式化输出的时候,保留2位小数的格式是

python 复制代码
# 假设输出 a ,并且 a 保留 2 位小数
print('%.2f'%a)

输入

输入的时候所有的输入都是字符串类型,我们需要进行类型转换

python 复制代码
# 表示的意思是输入一个整数类型的变量 a
a=int(input())

第一题

python 复制代码
ans=4.22*3600*30/72
ans=int(ans)
print(ans)

ans2=1000000000/365/24/3600
print('%.3f'%ans2)

ans3=35.27*1.15/3
print('%.2f'%ans3)

s=12.5*16.7
c=(12.5+16.7)*2
print("面积=",'%.2f'%s,",周长=",'%.2f'%c)

第二题

python 复制代码
# a
a=int(input())
b=int(input())
c=int(input())
d=int(input())
ans=a*b-c*d
print(ans)

# b
a=int(input())
ans=5/9*(a-32)
print('%.2f'%ans)

文件操作

参见资源里面的第三题和第四题,为了方便起见,直接把代码贴在下面

第三题

python 复制代码
# Open the file in write mode
with open("a.txt", "w") as file:
    # Write student data to the file
    for i in range(5):
        student_id = input()
        student_name = input()
        student_height = input()

        # Write student data to the file
        file.write(f"{student_id},{student_name},{student_height}\n")

# print("Student data has been written to a.txt file.")

第四题

python 复制代码
# 打开文件并读取内容
with open("a.txt", "r") as file:
    # 逐行读取并显示内容
    for line in file:
        print(line.strip())  # 去除换行符并打印每一行的内容
相关推荐
weixin_568996061 分钟前
Golang怎么用K8s Job执行一次性任务_Golang如何用Job资源运行批处理和迁移任务【操作】
jvm·数据库·python
耿雨飞4 分钟前
Python 后端开发技术博客专栏 | 第 09 篇 GIL 深度解析与并发编程实战 -- 多线程、多进程、协程的选型
开发语言·python
Stark-C5 分钟前
NAS音乐必备神器,全平台音乐收割机!极空间部署『Go Music DL』
开发语言·后端·golang
Ulyanov9 分钟前
像素迷宫:路径规划算法的可视化与实战
大数据·开发语言·python·算法
源码之家10 分钟前
计算机毕业设计:Python农产品销售数据可视化分析系统 Django框架 数据分析 可视化 大数据 大模型 机器学习(建议收藏)✅
python·信息可视化·数据分析·django·flask·课程设计
2401_8877245011 分钟前
如何通过JDBC写入BLOB文件_setBinaryStream上传图片与大文件至数据库
jvm·数据库·python
2301_8166602114 分钟前
如何重命名数据表_Operations面板表名修改方法
jvm·数据库·python
weixin_5860614622 分钟前
Navicat导入Excel表格报错怎么跳过_忽略错误记录高级选项
jvm·数据库·python
2301_7735536234 分钟前
golang如何理解编译指示pragma_golang编译指示pragma策略
jvm·数据库·python
qq_3422958235 分钟前
c++字符串运算_连接、比较、输入输出等运算符重载应用
jvm·数据库·python