網(wǎng)站首頁 編程語言 正文
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ù)名賦給一個變量,相當(dāng)于給這個函數(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)換成對應(yīng)的四元數(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é)
原文鏈接:https://fuxi-willard.blog.csdn.net/article/details/122657365
相關(guān)推薦
- 2022-02-10 Error: Cannot find module ‘webpack/lib/RuleSet‘ 解決
- 2023-03-19 Python學(xué)習(xí)之configparser模塊的使用詳解_python
- 2022-07-14 React父子組件傳值(組件通信)的實現(xiàn)方法_React
- 2023-07-30 el-selete改變值后選中的內(nèi)容不變
- 2022-08-21 golang中select語句的簡單實例_Golang
- 2023-05-22 解決The?Network?Adapter?could?not?establish?the?conn
- 2022-11-29 ASP.NET?MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字_基礎(chǔ)應(yīng)用
- 2022-09-09 Oracle存儲過程與函數(shù)的詳細(xì)使用教程_oracle
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支