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())  # 去除换行符并打印每一行的内容
相关推荐
luj_17682 小时前
残熵算法实时化三大瓶颈突破
c语言·开发语言·网络·经验分享·算法
充钱大佬3 小时前
Python测试基础教程
python·log4j·apache
不听话坏3 小时前
Ignition篇(下 一) 动态执行前的事情
开发语言·前端·javascript
likeyi073 小时前
require 和 import的区别
开发语言·前端
远离UE43 小时前
UE5 compute shader 原子加
开发语言·c++·ue5
C+-C资深大佬3 小时前
C++ 显式类型转换详解:static_cast、dynamic_cast、const_cast、reinterpret_cast
开发语言·c++
KaMeidebaby4 小时前
卡梅德生物技术快报|抗体亲和力成熟工业化调控新机制:差异性浆细胞增殖工艺优化思路
java·开发语言·人工智能·算法·机器学习·架构·spark
luj_17685 小时前
心形曲线轨迹控制三大关键技术
c语言·开发语言·c++·经验分享·算法
初心丨哈士奇5 小时前
Python 四大基础容器|列表篇
python
Mininglamp_27185 小时前
Claude Code 封禁中国开发者之后:本地 AI 编程工具的替代方案实测
开发语言·人工智能·windows·开源软件·ai-native