★专题0:py语法精讲:函数

了解函数的定义方法

Python内置函数(Built-in Functions)
自定义函数

>>> def add(x,y):
...     '''
...     This is my first function.
...     '''
...     r = x + y
...     return r
...
>>> help(abs)
Help on built-in function abs in module builtins:

abs(x, /)
    Return the absolute value of the argument.

>>> help(add)
Help on function add in module __main__:

add(x, y)
    This is my first function.

>>> add.__doc__
'\n    This is my first function.\n    '
>>> abs.__doc__
'Return the absolute value of the argument.'
>>> add(3,4)
7
>>> r = add(3,4)
>>> r
7
>>> def my_book():
...     return 'Learn Python'
...
>>> my_book()
'Learn Python'
>>> my_book
<function my_book at 0x000001ECCBC42510>
>>> def book():
...     print('Learn Python')
...
>>> book()
Learn Python
>>> b = book()\
...
Learn Python
>>> b = book()
Learn Python
>>> print(b)
None
>>> def foo():
...     print('before return')
...     return
...     print('after return')
...
>>> foo()
before return

例题1:编写函数,查找某个范围内自然数中的素数

#方法一
import math
def is_prime1(n):
    if n <= 1:
        return False
    for i in range(2,int(math.sqrt(n)) +1):
        if n % i == 0:
            return False
    return True


#方法二
def is_prime2(n):
    if n <= 1:
        return False
    i = 2
    while i*i <= n:
        if n % i == 0:
            return False
        i += 1
    return True

#方法三
from itertools import count
def is_prime3(n):
    if n <= 1:
        return False
    for i in count(2):
        if i * i > n:
            return True
        if n % i ==0:
            return False
        
#方法四
def is_prime4(n):
    if n <=1:
        return False
    if n == 2:
        return True
    if n % 2 ==0:
        return False
    i = 3
    while i * i <= n:
        if n % i ==0:
            return False
        i +=2
    return True


#方法五
def find_prime(nlst):
    primes = []
    for n in nlst:
        for x in range(2,n):
            if n % x == 0:
                break
        else:
            primes.append(n)
    return primes

ns = range(2,20)
print(find_prime(ns))

primes_lst = [i for i in ns if is_prime4(i)]
print(primes_lst)
primes_lst = [i for i in ns if is_prime3(i)]
print(primes_lst)
primes_lst = [i for i in ns if is_prime2(i)]
print(primes_lst)
primes_lst = [i for i in ns if is_prime1(i)]
print(primes_lst)

调用函数

调用方式
“传参数”模式
-按照位置
-根据名称
-设置默认值

>>> def foo(x,y):
...     print('x=',x)
...     print('y=',y)
...     return x+y
...
>>> foo(3,4)
x= 3
y= 4
7
>>> foo(4,3)
x= 4
y= 3
7
>>> foo(x=3,y=4)
x= 3
y= 4
7
>>> foo(x=4,y=3)
x= 4
y= 3
7
>>> def bar(x,y=3):
...     print('x=',x)
...     print('y=',y)
...     return x+y
...
>>> bar(2)
x= 2
y= 3
5
>>> bar(2,6)
x= 2
y= 6
8
>>> bar(y=1,x=9)
x= 9
y= 1
10

参数与对象关系
python中参数“传递对象引用”

>>> def bar(a):
...     print(id(a))
...     a.append(33)
...     return a
...
>>> lst = [1,2,3]
>>> blst = bar(lst)
2605216035912
>>> id(lst)
2605216035912
>>> id(blst)
2605216035912
>>> lst
[1, 2, 3, 33]
>>> blst
[1, 2, 3, 33]
>>>

注意:
通常,不对函数的参数所引用对象类型进行检查
可以用pass关键词定义函数
函数名称代表函数对象

>>> foo(3,'a')
x= 3
y= a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in foo
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> def bar(a):pass
...
>>> bar
<function bar at 0x0000025E92E40730>
>>> type(bar)
<class 'function'>

在程序中使用函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值