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())  # 去除换行符并打印每一行的内容
相关推荐
不灭的黄金瞳1234 分钟前
Java数据类型与变量
java·开发语言·intellij-idea
贝猫说python17 分钟前
阿里云部署qwen 、第5步:阿里云服务器上大模型运行demo.py,固定提示词模板 拼接用户输入,输出符合要求格式结果
python
小白学大数据23 分钟前
Python 项目实战:用 Flask 构建生产级 MySQL 增删改查 REST API
开发语言·人工智能·python·mysql·flask
诺伦32 分钟前
AI营销从单点智能到系统协同:多Agent编排如何重塑增长运营
python·多agent系统·ai营销·agent编排·增长运营
96245634 分钟前
pywebview 指定 Edge Chromium,为什么打包后仍回退到 MSHTML?
python
ellenwan202635 分钟前
量化学习不能只补技术
人工智能·python
SamChan9036 分钟前
多栏PDF阅读顺序重建:用Python按坐标聚类还原双栏论文的翻译顺序
python·ai·pdf
Nil20837 分钟前
leetcode 20有效的括号
开发语言
赵民勇42 分钟前
Python collections.ChainMap 详解
python
三十岁老牛再出发1 小时前
9月16日总结
python·深度学习·机器学习