# 1.計算圓的面積 p = 3.1415926 r1 = 5 r2 = 10 r3 = 15 print("半徑爲r1的面積%f"%(p*r1*r1)) print("半徑爲r2的面積%f"%(p*r2*r2)) print("半徑爲r3的面積%f"%(p*r3*r3)) def area_or_circle(r): p = 3.1415926 return p*r*r r4 = input("Please input your r:") print("半徑爲%s的面積爲%f"%(r4,area_or_circle(int(r4)))) # 2.計算1-100的和 def sum_of_circle(a): sum = 0 while a < 101: sum += a a += 1 return sum sum1 = sum_of_circle(0) print("0~100的結果爲:%d"%sum1) def circleSum(a): sum = 0 list = range(101) while a < len(list): sum += a a += 1 return sum sum2 = circleSum(0) print("0-100的結果%d"%sum2) # 3.求絕對值 函數abs()只能傳入一個參數 absNumber = int(input("Please input the abs number:")) print("絕對值結果爲:%d",abs(absNumber)) # 4.求最大值 max() 能夠傳入多個參數而且輸出最大值注意傳入類型不對會報錯 maxSum = max(1,2,3,-1,-5) print("輸出最大值爲:%d"%maxSum) # 5.函數名賦給一個變量那麼這個變量就擁有了這個函數的功能 falseAbs = abs # 變量falseAbs 指向函數abs falseNumber = input("請輸入一個數用來測試函數別名的功能:") print("結果爲:",falseAbs(int(falseNumber))) # 6.練習hex()函數把一個整數轉換成十六進制表示的字符串: hexx = he string = input("Please input the string:") print("輸出十六進制數:",hexx(int(string)))
float.hex(1.2) #float轉成十六進制 binascii.b2a_hex("string".encode("utf-8")) #字符串轉成十六進制