最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

實(shí)例講解Python中函數(shù)的調(diào)用與定義

 更新時(shí)間:2016年03月14日 16:31:17   作者:YoferZhang  
這篇文章主要介紹了Python中函數(shù)的調(diào)用與定義,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

調(diào)用函數(shù):

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
 
# 函數(shù)調(diào)用 
>>> 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函數(shù),相當(dāng)于引用 
>>> a(-1) # 所以也可以通過(guò)a調(diào)用abs函數(shù) 
1 
 
>>> n1 = 255 
>>> n2 = 1000 
>>> print(hex(n1)) 
0xff 
>>> print(hex(n2)) 
0x3e8 

定義函數(shù):

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

pass語(yǔ)句什么都不做 。
實(shí)際上pass可以用來(lái)作為占位符,比如現(xiàn)在還沒(méi)想好怎么寫(xiě)函數(shù)代碼,就可以先寫(xiě)一個(gè)pass,讓代碼運(yùn)行起來(lái)。  
  

if age >= 18: 
 pass 
#缺少了pass,代碼就會(huì)有語(yǔ)法錯(cuò)誤 
>>> 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 

 
返回兩個(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, y) 
151.96152422706632 70.0 

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

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

實(shí)際上返回的是一個(gè)tuple! 
但是,語(yǔ)法上,返回一個(gè)tuple可以省略括號(hào),  而多個(gè)變量可以同時(shí)接受一個(gè)tuple,按位置賦給對(duì)應(yīng)的值。 
所以,Python的函數(shù)返回多值實(shí)際就是返回一個(gè)tuple,但是寫(xiě)起來(lái)更方便。  
  函數(shù)執(zhí)行完畢也沒(méi)有return語(yǔ)句時(shí),自動(dòng)return None。 
 
練習(xí)  :

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

相關(guān)文章

最新評(píng)論

兰坪| 肇州县| 萝北县| 芜湖县| 渑池县| 新平| 北安市| 贡山| 夏河县| 苏尼特左旗| 江陵县| 观塘区| 中阳县| 新绛县| 五河县| 通州市| 民县| 大足县| 汶川县| 叙永县| 鹤岗市| 新营市| 孟连| 伊川县| 长垣县| 高要市| 耒阳市| 吐鲁番市| 新宁县| 闵行区| 读书| 英吉沙县| 丹凤县| 莱阳市| 蓬溪县| 太仓市| 水富县| 亳州市| 葫芦岛市| 河东区| 郸城县|