04 函數
来源:互联网 发布:欧美网络电视柠檬tv 编辑:程序博客网 时间:2024/06/05 18:06
內置函數
Python內置了很多有用的函數,可以直接調用。
要調用一個函數,需要知道函數的名稱和參數。
可以直接從Python的官方網站查看文檔:http://docs.python.org/2/library
>>> abs(-20)
>>> help(abs)
>>> cmp(x, y) #x<y, -1 x==y,0 x>y,1
>>> int('123')
>>> int(12.34)
>>> str(123)
>>> str(1.23)
編寫函數
定義一個函數要使用def語句,依次寫出函數名、括號、括號中的參數和冒號;
然後在縮進中編寫函數體;
函數的返回值最後用return語句返回。
return None可以簡寫爲return。
返回多值
import math
def move(x, y, step, angle)
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, y
其實,返回多值的本質是返回一個Tuple。
默認參數
只能定義在必須參數的後面
def f1(a, b=2)
... ...
可變參數
def fn(*args):
print args
可以傳入0個、1個或多個參數給可變參數。
Python解釋器會把傳入的一組參數組裝成一個tuple傳遞給可變參數,即在函數內部,直接把args看成一個tuple就可以了。
0 0
- 04 函數
- 函數
- TRUNC()函數
- GetTickCount函數
- VB 函數
- SQL 函數
- poll函數
- CopyFile函數
- sqlite函數
- ORACL instr 函數 substr函數
- 網絡類常用API函數
- 常用JavaScript函數
- Oracle函數1-10
- Oracle函數11-20
- Oracle函數31-40
- Oracle函數21-30
- Oracle函數41-67
- UniCode 轉 GB 函數
- This version of android studio is incompatible with the gradle version used.
- DNA Pairing
- Q&A——粒子系统
- c/c++中内存区域划分大总结
- openfalcon
- 04 函數
- Android Studio:布局查看神器-UIAutomatorViewer
- B1068. 万绿丛中一点红(20)。。。map的运用
- QT 多目录多工程 配置
- Qt之Scene Graph画图
- MySQL 预处理语句prepare、execute、deallocate的使用
- MySQL数据库复制概论
- React初学手记
- Android仿微信语音聊天界面