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())  # 去除换行符并打印每一行的内容
相关推荐
SiYuanFeng2 分钟前
大厂面试python复习基础100题
python·面试·职场和发展
高洁013 分钟前
数字孪生:虚拟调试,真实交付
python·深度学习·机器学习·transformer·知识图谱
江畔柳前堤4 分钟前
XZ09_Word和MD格式转换
开发语言·数据库·人工智能·python·深度学习·word
ZenosDoron4 分钟前
malloc规范
java·开发语言
codeejun4 分钟前
每日一Go-71、理论知识:CAP 、一致性原理 、Raft 机制(简化实现一个 Raft)
java·开发语言·golang
Aotman_6 分钟前
JavaScript数组对象中指定字段转换
java·开发语言·前端·javascript·vue.js·前端框架·es6
星河漫步Lu6 分钟前
Anaconda搭建深度学习虚拟环境
开发语言·python·深度学习
张一西7 分钟前
2026 新春快乐破解活动五之windows中级题
python·反编译·nuitka·flag
GEO_youxuan7 分钟前
财务报表分析软件哪个好?2026年主流产品实测对比
python
为爱停留8 分钟前
我的第一个智能问答:给智能体接上网页 + 流式输出
python