欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

代碼詳解Python的函數(shù)基礎(chǔ)(1)

 更新時(shí)間:2022年01月25日 11:41:27   作者:FUXI_Willard  
這篇文章主要為大家詳細(xì)介紹了Python的函數(shù)基礎(chǔ),使用了函數(shù)調(diào)用和函數(shù)定義,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1.函數(shù)調(diào)用

# 1.調(diào)用函數(shù),需要知道函數(shù)的名稱和參數(shù)
# 2.調(diào)用函數(shù)傳入的參數(shù)需要和函數(shù)定義的參數(shù)數(shù)量和類型一致
# 如調(diào)用abs函數(shù)
print("-2的絕對(duì)值為:",abs(-2))
print("100的絕對(duì)值為:",abs(100))
# 3.函數(shù)名是指向一個(gè)函數(shù)對(duì)象的引用,可以把函數(shù)名賦給一個(gè)變量,相當(dāng)于給這個(gè)函數(shù)起別名
abs1 = abs    # 變量abs1指向abs函數(shù)
print("-1的絕對(duì)值為:",abs1(-1))

# 結(jié)果輸出:
-2的絕對(duì)值為: 2
100的絕對(duì)值為: 100
-1的絕對(duì)值為: 1

2.函數(shù)定義

# 定義函數(shù)使用def
# 語法:
"""
def 函數(shù)名(參數(shù)1,參數(shù)2,...):
    函數(shù)體
    return 返回值
"""
def compareAB(a,b):
    if a > b:
        print("a值大于b值!")
    elif a == b:
        print("a值等于b值!")
    else:
        print("a值小于b值!")
# 調(diào)用函數(shù)
compareAB(5,3)
# 結(jié)果輸出:
# a值大于b值!
# 空函數(shù):可以用來作為占位符
def nop():
    pass
# 參數(shù)檢查:Python解釋器可以幫我們檢查參數(shù)個(gè)數(shù)是否正確,但無法檢查參數(shù)類型是否正確
# 數(shù)據(jù)類型檢查實(shí)例
def myAbs(x):
    if not isinstance(x,(int,float)):
        raise  TypeError("Bad Operand Type.")
    if x >= 0:
        return x
    else:
        return -x

# 傳入"a"將拋出錯(cuò)誤
myAbs("A")
# 結(jié)果輸出:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-21934e00955a> in <module>
     15 
     16 # 傳入"a"將拋出錯(cuò)誤
---> 17 myAbs("A")
<ipython-input-8-21934e00955a> in myAbs(x)
      7 def myAbs(x):
      8     if not isinstance(x,(int,float)):
----> 9         raise  TypeError("Bad Operand Type.")
     10     if x >= 0:
     11         return x
TypeError: Bad Operand Type.
# 返回多個(gè)值
import math
def move(x,y,step,angle = 0):
    nx = x + step * math.cos(angle)
    ny = y - step * math.sin(angle)
    return nx,ny
# 獲取返回值
x,y = move(100,100,60,math.pi / 6)
print("x的值為%f,\ny的值為%f"%(x,y))
# 結(jié)果輸出:
# x的值為151.961524,
# y的值為70.000000
# 實(shí)例1:由歐拉角轉(zhuǎn)換成對(duì)應(yīng)的四元數(shù)
# 由角度計(jì)算四元數(shù)的值
import math
# yaw:繞z軸旋轉(zhuǎn)的角度;
# pitch:繞y軸旋轉(zhuǎn)的角度;
# roll:繞x軸旋轉(zhuǎn)的角度;
def eulerToQuaternion(yaw,pitch,roll):
    w = math.cos(roll/2.0)*math.cos(pitch/2.0)*math.cos(yaw/2.0)+math.sin(roll/2.0)*math.sin(pitch/2.0)*math.sin(yaw/2.0)
    x = math.sin(roll/2.0)*math.cos(pitch/2.0)*math.cos(yaw/2.0)-math.cos(roll/2.0)*math.sin(pitch/2.0)*math.sin(yaw/2.0)
    y = math.cos(roll/2.0)*math.sin(pitch/2.0)*math.cos(yaw/2.0)+math.sin(roll/2.0)*math.cos(pitch/2.0)*math.sin(yaw/2.0)
    z = math.cos(roll/2.0)*math.cos(pitch/2.0)*math.sin(yaw/2.0)-math.sin(roll/2.0)*math.sin(pitch/2.0)*math.cos(yaw/2.0)
    return x,y,z,w
# 繞z軸90度
print("繞z軸90度的四元數(shù)為:",(eulerToQuaternion(math.pi/2,0,0)))
# 繞y軸90度
print("繞y軸90度的四元數(shù)為:",(eulerToQuaternion(0,math.pi/2,0)))
# 繞x軸90度
print("繞x軸90度的四元數(shù)為:",(eulerToQuaternion(0,0,math.pi/2)))

# 結(jié)果輸出:
繞z軸90度的四元數(shù)為: (0.0, 0.0, 0.7071067811865476, 0.7071067811865476)
繞y軸90度的四元數(shù)為: (0.0, 0.7071067811865476, 0.0, 0.7071067811865476)
繞x軸90度的四元數(shù)為: (0.7071067811865476, 0.0, 0.0, 0.7071067811865476)
 

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評(píng)論