日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python函数调用的例子_实例讲解Python中函数的调用与定义

發布時間:2025/4/16 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python函数调用的例子_实例讲解Python中函数的调用与定义 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

調用函數:

#!/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 "", line 1, in

TypeError: abs() takes exactly one argument (2 given)

>>> abs('a')

Traceback (most recent call last):

File "", line 1, in

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 "", line 2

^

IndentationError: expected an indented block

>>> myAbs(1, 2)

Traceback (most recent call last):

File "", line 1, in

TypeError: myAbs() takes 1 positional argument but 2 were given

>>> myAbs('A')

Traceback (most recent call last):

File "", line 1, in

File "", line 2, in myAbs

TypeError: unorderable types: str() >= int()

>>> abs('A')

Traceback (most recent call last):

File "", line 1, in

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 "", line 1, in

File "", 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

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的python函数调用的例子_实例讲解Python中函数的调用与定义的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。