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())  # 去除换行符并打印每一行的内容
相关推荐
茉莉玫瑰花茶33 分钟前
GO [ 类型 ]
开发语言·数据库·golang
用户83562907805133 分钟前
使用 Python 拆分和提取 PDF 页面
后端·python
用户83562907805134 分钟前
使用 Python 操作 PowerPoint 中的图片
后端·python
梅雅达编程笔记38 分钟前
02_BeautifulSoup网页数据提取
开发语言·爬虫·python·beautifulsoup·数据采集
Escalating_xu42 分钟前
【C 语言】深入理解指针(3):字符指针、数组指针、二维数组传参、函数指针与转移表
java·c语言·开发语言
ai小陈42 分钟前
深度学习CUDA OOM排查:显存占用与碎片问题实战
服务器·人工智能·python·深度学习·ai·gpu算力
可涵不会debug1 小时前
C++ std::bind 实战教程:线程池、回调场景全覆盖
linux·开发语言·c++
不正经的码狗1 小时前
Java 开发环境搭建与 Eclipse 使用速通教程
java·开发语言·eclipse
小小张说故事1 小时前
pandas 读大 CSV 太慢?6 个实测提速技巧(dtype / 分块 / 引擎选择)
后端·python·pandas
yi0111 小时前
DAY17: LeetCode 139|单词拆分:从“把单词删掉”到用 DP 记录合法切口
人工智能·笔记·python·算法·leetcode·动态规划