Python_初识函数

函数

函数的好处:可以复用

 

如:

def f():pass

 

f

print(f)

 

def add(a,b):

    return a+b

 

add

add(1,2)

print(add(1,2))

 

 

练习:除法

def div(a,b):

    if not isinstance(a,(int,float)):

        return None

    

    if not isinstance(b,(int,float)):

        return None

    if b == 0:

        return None

    return a/b

 

print(div(100,10))

 

1)练习:写一个函数,统计一下一句话中的数字个数。

I am a 19 years old boy!666!

 

s = 'I am a 19 years old boy!666!'
def count_digits_in_a_sentence(s):
    if not isinstance(s,str):
        print ("the parameter is not a unicode string")
        return 0
    result=0
    for i in s:
        if i in "0123456789":
            result+=1
    return result

print(count_digits_in_a_sentence(s))

 

 

2)统计一下字母个数

s = 'I am a 19 years old boy!666!'

def count_letters_in_a_sentence(s):

    if not isinstance(s,str):

        print ("the parameter is not a unicode string")

        return 0

    result=0

    for i in s:

        #'a'<=i<='z' and 'A'<=i<='Z'

        if (ord(i) >=65 and ord(i) <=91) or\

        (ord(i) >=97 and ord(i) <=122):

            result+=1

    return result

 

print(count_letters_in_a_sentence(s))

3)统计一下字母和数字个数

 

print(count_letters_in_a_sentence(s)+count_digits_in_a_sentence(s))

4)统计一下非字母和非数字的个数

def count_non_alphabetnum_in_a_sentence(s):

    count = len(s)

return count-(count_letters_in_a_sentence(s)+count_digits_in_a_sentence(s))

 

 

>>> import re
>>> re.findall(r"\d+",s)
['19', '666']
>>> len(re.findall(r"\d+",s))
2

 

5正则

>>> import re
>>> re.findall(r"\d+",s)
['19', '666']
>>> len(re.findall(r"\d+",s))
2

 

2、缺省默认值

>>> def add(a,b=100):
...     return a+b
...
>>> print(add(10))
110
>>> print(add(10,200))
210

默认值在前面,否则会有语法报错

3、按值传递,按引用传递

函数传入的参数为不可变的,对外部的变量就没影响。a=100
函数传入的参数为可变的,对外部的变量就有影响。

 

函数传入的参数为不可变的,对外部的变量就没影响。a=100

按值传--传入的不是变量对应的内存地址。

函数传入的参数为可变的,对外部的变量就有影响。

按引用传--传入的是变量对应的内存地址。

>>> a = []
>>> def f(a):
...     a.append(1)
...     return a
...
>>> f(a)
[1]
>>>
>>> a
[1]

4global引用全局变量

>>> def f():

...     global a

...     a+=1

...     return a

...

>>> a

100

>>> f()

101

>>> a

101

 

实例:

>>> def f(a,b,**kw):

...     print(type(kw))

...     for k,v in kw.items():

...         print(k,v)

...

>>>

>>> f(1,2,a=3,b=4,c=5)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: f() got multiple values for argument 'a'

>>>

>>> f(1,2,c=3,d=4,e=5)

<class 'dict'>

c 3

d 4

e 5

5、可变函数参数(*args,**args)

实例:计算所有参数之和且函数参数是可变的 

def sum(a,b,*arg):

    result = a+b

    for i in arg:

        result+=i

    return result

 

print(sum(1,2,3,4,5))

print(sum(1,2,3,4,5,6,7,8,9,10)) 

可变参数要放在不可变参数之后

可变参数字典**args

>>> def f(a,b,**kw):

...     print(type(kw))

...     for k,v in kw.items():

...         print(k,v)

...

>>>

>>> f(1,2,c=3,d=4,e=5)

<class 'dict'>

c 3

d 4

e 5

 

>>> def func(a,b):

...     print(a,b)

...

>>> func(1,2)

1 2

>>> func(b=1,a=2)

2 1

 

*arg:表示把可变的多个非命名参数,转换为了元祖

**kw:表示把可变的多个命名参数,转换为了字典

 

传参不按顺序,传参时可以指定参数名和值

posted @ 2018-08-02 00:16  翻滚的小强  阅读(104)  评论(0)    收藏  举报