国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

實例講解Python中函數的調用與定義

2019-11-25 16:52:57
字體:
來源:轉載
供稿:網友

調用函數:

#!/usr/bin/env python3 # -*- coding: utf-8 -*-  # 函數調用 >>> abs(100) 100 >>> abs(-110) 110 >>> abs(12.34) 12.34 >>> abs(1, 2) Traceback (most recent call last):  File "<stdin>", line 1, in <module> TypeError: abs() takes exactly one argument (2 given) >>> abs('a') Traceback (most recent call last):  File "<stdin>", line 1, in <module> TypeError: bad operand type for abs(): 'str' >>> max(1, 2) 2 >>> max(2, 3, 1, -5) 3 >>> int('123') 123 >>> int(12.34) 12 >>> str(1.23) '1.23' >>> str(100) '100' >>> bool(1) True >>> bool('') False >>> a = abs # 變量a指向abs函數,相當于引用 >>> a(-1) # 所以也可以通過a調用abs函數 1  >>> n1 = 255 >>> n2 = 1000 >>> print(hex(n1)) 0xff >>> print(hex(n2)) 0x3e8 

定義函數:

#!/usr/bin/env python3 # -*- coding: utf-8 -*-  #函數定義 def myAbs(x):  if x >= 0:   return x  else:   return -x  a = 10 myAbs(a)  def nop(): # 空函數  pass 

pass語句什么都不做 。
實際上pass可以用來作為占位符,比如現在還沒想好怎么寫函數代碼,就可以先寫一個pass,讓代碼運行起來。  
  

if age >= 18:  pass #缺少了pass,代碼就會有語法錯誤 >>> if age >= 18: ...  File "<stdin>", line 2   ^ IndentationError: expected an indented block  >>> myAbs(1, 2) Traceback (most recent call last):  File "<stdin>", line 1, in <module> TypeError: myAbs() takes 1 positional argument but 2 were given >>> myAbs('A') Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "<stdin>", line 2, in myAbs TypeError: unorderable types: str() >= int() >>> abs('A') Traceback (most recent call last):  File "<stdin>", line 1, in <module> TypeError: bad operand type for abs(): 'str'  def myAbs(x):  if not isinstance(x, (int, float)):   raise TypeError('bad operand type')  if x >= 0:   return x  else:   return -x  >>> myAbs('A') Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "<stdin>", line 3, in myAbs 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, y) 151.96152422706632 70.0 

 
其實上面只是一種假象,Python函數返回的仍然是單一值 。

>>> r = move(100, 100, 60, math.pi / 6) >>> print(r) (151.96152422706632, 70.0) 

實際上返回的是一個tuple! 
但是,語法上,返回一個tuple可以省略括號,  而多個變量可以同時接受一個tuple,按位置賦給對應的值。 
所以,Python的函數返回多值實際就是返回一個tuple,但是寫起來更方便。  
  函數執行完畢也沒有return語句時,自動return None。 
 
練習  :

import math def quadratic(a, b, c):  x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)  x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)  return x1, x2  x1, x2 = quadratic(2, 5, 1) print(x1, x2)  >>> import math >>> def quadratic(a, b, c): ...  x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a) ...  x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a) ...  return x1, x2 ... >>> x1, x2 = quadratic(2, 5, 1) >>> print(x1, x2) -0.21922359359558485 -2.2807764064044154

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁陵县| 双桥区| 安宁市| 泰来县| 敦化市| 宝应县| 商都县| 吉水县| 和政县| 泗洪县| 北票市| 鄂尔多斯市| 蓬安县| 达州市| 嵩明县| 苍山县| 黔西| 丰宁| 新巴尔虎左旗| 宿迁市| 壤塘县| 武乡县| 临湘市| 河东区| 恩施市| 南澳县| 西宁市| 若尔盖县| 南投县| 西畴县| 文化| 新民市| 玛纳斯县| 达拉特旗| 黔西| 临沂市| 青浦区| 托克逊县| 宜春市| 南阳市| 南阳市|