您現在的位置是:首頁 > 網路遊戲首頁網路遊戲
python3的基礎學習之數學(3)
- 2022-05-22
歐幾里得範數定義
可以得到圓周率π值小數點後面任意位的值喲!!
有點長,慢慢來,小白看了都會,簡單。
1.取絕對值
1。1 程式碼:
import mathx=int(input(‘請輸入數字:’)) #如果是負數,就返回正數,即絕對值print(abs(x)) #abs是返回絕對值,得到10
1。2
你輸入的是10,得到就是10
你輸入的是-10,得到的就是-10的絕對值,正數10
1。3 注意浮點數取絕對值
程式碼:
import mathx=float(input(‘請輸入數字:’)) #如果是小數,就是浮點數,需要float# 浮點數的形式返回絕對值print(math。fabs(x)) #比如輸入-10。5,得到就是10。5
——————————————————————-
2.取整
程式碼:
import mathx=float(input(‘請輸入數字:’)) #如果是小數,就是浮點數,需要float# 返回數字向上取整的結果#print(math。ceil(x)) #如輸入1。3,向上取整就是2# 返回向下取整的結果print(math。floor(x)) #比如輸入3。3,向下取整就是3
——————————————————————-
3.取最大值和最小值
程式碼
import math
x1=int(input(‘請輸入數字:’)) x2=int(input(‘請輸入數字:’))x3=int(input(‘請輸入數字:’)) x4=int(input(‘請輸入數字:’)) x5=int(input(‘請輸入數字:’)) print(max(x1, x2, x3, x4, x5)) #取最大值#print(min(x1, x2, x3, x4, x5)) #取最小值
4.除數,保留小數點
程式碼:
import math#x1=int(input(‘請輸入數字:’)) #這裡不能設定取整,否則小數之間的除法就不能使用x1=float(input(‘請輸入數字:’))x2=float(input(‘請輸入數字:’))x3=x1/x2print(float(x3))
5.除數,帶商和餘數的
程式碼:
import mathx1=float(input(‘請輸入數字:’))x2=float(input(‘請輸入數字:’))# 返回模除以後的商和餘數print(‘(商,餘數【帶浮點的】)’)print(divmod(x1, x2))
6。除法,小數部分和整數部分
程式碼:
import mathx1=float(input(‘請輸入數字:’))x2=float(input(‘請輸入數字:’))x3=x1/x2# 返回一個浮點數的小數部分和整數部分print(x3)print(‘小數部分,整數部分’)print(“modf”, math。modf(x3))
7.除法,取餘數
程式碼:
import mathx1=float(input(‘請輸入數字:’))x2=float(input(‘請輸入數字:’))x3=x1%x2 #%在python中是取餘,即x3是餘數print(x3)
8.除法或者小數的四捨五入
程式碼:
import mathx1=float(input(‘請輸入數字:’))x2=float(input(‘請輸入數字:’))x3=x1/x2 # 四捨五入,後面指定位數print(round(x3, 2))#x3是一個小數,2是保留小數後面2位
9.求平方根
程式碼:
import mathx=float(input(‘請輸入數字:’))# 求平方根,比如√4=2,√16=4,注意浮點小數y=math。sqrt(x)print(y) #帶小數點後面,預設15位# 四捨五入,後面指定位數print(round(y, 4))#y是一個小數,4是保留小數後面2位#注意如果y是正數,則只顯示小數點後面1位
示意圖:
請輸入數字:15
3。872983346207417
3。873
——————————
請輸入數字:4
2。0
2。0
10.求冪
程式碼:
import mathx=float(input(‘請輸入數字:’))y=float(input(‘請輸入數字:’))# 求x的y次方,同x**y#z=x**y#print(z)print(pow(x, y))
11.三角函式sin、cos等
程式碼:
import mathx=float(input(‘請輸入數字:’))#sin#print(math。sin(x))#cosprint(math。cos(x))# 反餘弦#print(math。acos(x))# 反正弦#print(math。asin(x))
12.歐幾里得範數
程式碼:
import mathx=float(input(‘請輸入數字:’))y=float(input(‘請輸入數字:’))# 返回歐幾里得範數,也就是sqrt(x*x + y*y)。print(math。hypot(x, y)) #這種寫法就對z=x*x + y*yprint(z)#print(math。hypot(z))這種寫法不對print(math。sqrt(z))
結果如下:
請輸入數字:2
請輸入數字:3
3。6055512754639896
13。0
3。605551275463989
13.對數
程式碼:
import math#x=float(input(‘請輸入數字:’))y=float(input(‘請輸入數字:’))# 返回以2為底的y對數結果print(math。log2(y))#注意對數,以2為底的對數是log2,以10為底的對數是log10,暫時沒有以x為底的對數logx函式
14.e自然常數
程式碼:
import mathe=math。e #e為自然常數#x=float(input(‘請輸入數字:’))#y=float(input(‘請輸入數字:’))print(e) #得到2。718281828459045# 返回以e為底的對數結果#print(math。log(math。e)) #得到1
15,圓周率π
程式碼一:
import mathp=math。pi #pi為圓周率πprint(p) #預設得到3。141592653589793
程式碼二:#這個求任意小數點後的π的值,且未用到math(因為其中的π值是預設小數點後面位數的)
#匯入時間模組import time#計算當前時間time1=time。time()#演算法根據馬青公式計算圓周率number = int(input(‘請輸入想要計算到小數點後的位數n:’))# 多計算10位,防止尾數取捨的影響number1 = number+10# 算到小數點後number1位b = 10**number1# 求含4/5的首項x1 = b*4//5# 求含1/239的首項x2 = b// -239# 求第一大項he = x1+x2#設定下面迴圈的終點,即共計算n項number *= 2#迴圈初值=3,末值2n,步長=2for i in range(3,number,2): # 求每個含1/5的項及符號 x1 //= -25 # 求每個含1/239的項及符號 x2 //= -57121 # 求兩項之和 x = (x1+x2) // i # 求總和 he += x# 求出πpai = he*4#舍掉後十位pai //= 10**10# 輸出圓周率π的值paistring=str(pai)result=paistring[0]+str(‘。’)+paistring[1:len(paistring)]print (result)time2=time。time()print (u‘總共耗時:’ + str(time2 - time1) + ‘s’)
比如想要得到π值的小數點後面100位,直接輸入100,得到如下情況:
----------輸出情況如下-------------------------------
請輸入想要計算到小數點後的位數n:100
3。1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
總共耗時:3。2010533809661865s
——————-喜不喜歡?——————————————