python3—廖雪峰之練習(一)


變量練習
小明的成績從去年的72分提升到今年的85分,請計算小明成績提升的百分點。並用
字符串格式化顯示出'xx.x%',只保留小數點后一位:

s1 = 72
s2 = 85
r = (85-72)/72*100    #小明成績進步的百分比
print('%.1f%%' % r)	#格式化輸出xx.x%,保留小數點后一位

使用list和tuple練習
請用索引去出下面list的指定元素:

L = [
    ['Apple', 'Google', 'Microsoft'],
    ['Java', 'Python', 'Ruby', 'PHP']
    ['Adam', 'Bart', 'Lisa']
]
#打印Apple:
print(L[0][0])
#打印Python
print(L[1][1])
#打印Lisa
print(L[2][2])

條件判斷練習
小明身高1.75, 體重80.5kg。請根據BMI公式(體重除以身高的平方)幫小明就按

他的BIM指數,並根據BMI指數:

  • 低於18.5: 過低
  • 18.5-25:正常
  • 25-28:過重
  • 28—32:肥胖
  • 高於32:嚴重肥胖

用if-elif判斷並打印結果:

height = 1.75
weight = 80.5
bmi = weight / pow(height,2)
if bmi < 18.5:
    print('your weight is too light')
elif bmi > 18.5 and bmi < 25:
    print('your weight is normal')
elif bmi > 25 and bmi < 28:
    print('your weight is overweight')
elif bmi > 28 and bmi < 32:
    print('fat')
else:
    print('you are too fat')

循環練習
請利用循環依次對list中的每個名字打印出Hello,xxx!

L = ['Bart', 'Lisa', 'Adam']
for name in L:
    print('hello,',name)

調用函數練習

n1 = 255
n2 = 1000
print(hex(n1))
print(hex(n2))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM