因?yàn)楣P者有一些 c/c++ 語言的基礎(chǔ),所以應(yīng)該學(xué) python 會稍微簡單一些
格式化輸出的時(shí)候,保留2位小數(shù)的格式是
# 假設(shè)輸出 a ,并且 a 保留 2 位小數(shù)
print('%.2f'%a)
輸入
輸入的時(shí)候所有的輸入都是字符串類型,我們需要進(jìn)行類型轉(zhuǎn)換文章來源:http://www.zghlxwxcb.cn/news/detail-843188.html
# 表示的意思是輸入一個(gè)整數(shù)類型的變量 a
a=int(input())
第一題
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)
第二題
# 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)
文件操作
參見資源里面的第三題和第四題,為了方便起見,直接把代碼貼在下面文章來源地址http://www.zghlxwxcb.cn/news/detail-843188.html
第三題
# 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.")
第四題
# 打開文件并讀取內(nèi)容
with open("a.txt", "r") as file:
# 逐行讀取并顯示內(nèi)容
for line in file:
print(line.strip()) # 去除換行符并打印每一行的內(nèi)容
到了這里,關(guān)于python 第一次作業(yè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!