代碼詳解Python的函數(shù)基礎(1)
1.函數(shù)調(diào)用
# 1.調(diào)用函數(shù),需要知道函數(shù)的名稱和參數(shù)
# 2.調(diào)用函數(shù)傳入的參數(shù)需要和函數(shù)定義的參數(shù)數(shù)量和類型一致
# 如調(diào)用abs函數(shù)
print("-2的絕對值為:",abs(-2))
print("100的絕對值為:",abs(100))
# 3.函數(shù)名是指向一個函數(shù)對象的引用,可以把函數(shù)名賦給一個變量,相當于給這個函數(shù)起別名
abs1 = abs # 變量abs1指向abs函數(shù)
print("-1的絕對值為:",abs1(-1))
# 結(jié)果輸出:
-2的絕對值為: 2
100的絕對值為: 100
-1的絕對值為: 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ù)個數(shù)是否正確,但無法檢查參數(shù)類型是否正確
# 數(shù)據(jù)類型檢查實例
def myAbs(x):
if not isinstance(x,(int,float)):
raise TypeError("Bad Operand Type.")
if x >= 0:
return x
else:
return -x
# 傳入"a"將拋出錯誤
myAbs("A")
# 結(jié)果輸出:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-21934e00955a> in <module>
15
16 # 傳入"a"將拋出錯誤
---> 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.
# 返回多個值
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
# 實例1:由歐拉角轉(zhuǎn)換成對應的四元數(shù)
# 由角度計算四元數(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é)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
python使用xlrd實現(xiàn)檢索excel中某列含有指定字符串記錄的方法
這篇文章主要介紹了python使用xlrd實現(xiàn)檢索excel中某列含有指定字符串記錄的方法,涉及Python使用xlrd模塊檢索Excel的技巧,非常具有實用價值,需要的朋友可以參考下2015-05-05
Python pandas的describe函數(shù)參數(shù)示例詳解
describe()函數(shù)是pandas 中一個十分實用的工具,用于快速獲取數(shù)據(jù)集的描述性統(tǒng)計信息,本文詳細介紹了該函數(shù)的各種參數(shù)及其用法,包括控制輸出的百分位數(shù)、列類型以及是否將日期時間列視為數(shù)值型列等,感興趣的朋友一起看看吧2018-04-04
python結(jié)合API實現(xiàn)即時天氣信息
這篇文章主要介紹了python結(jié)合API實現(xiàn)即時天氣信息的代碼,非常的實用,有需要的小伙伴可以參考下。2016-01-01
Python Web框架Pylons中使用MongoDB的例子
這篇文章主要介紹了Python Web框架Pylons中使用MongoDB 的例子,大家參考使用2013-12-12

