zoukankan      html  css  js  c++  java
  • python07_函数

    多个参数定义函数

    >>> def print_sth(s1,s2,s3):
    ...   print(s1,s2,s3)
    ...
    >>> print_sth(1,2,3)
    1 2 3

    >>> print_sth("a","b","c")
    a b c

    >>> print_sth(s3="c",s2="b",s1="a")
    a b c


    C:Usersliuwe>python
    Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def print_sth(s1,s2,s3):
    ... print(s1,s2,s3)
    ...
    >>> print_sth(s3="c",s2="b",s1="a")
    a b c

    命名参数必须在非命名参数的后面 
    >>> print_sth("c",s3="b",s2="a")
    c a b

    >>>定义多个参数没有使用

    >>> def print_sth(s1,s2,s3):
    ...    print("hello")
    ...
    >>> print_sth(1,2,3)
    hello

    >>> print_sth(1,2,3)
    hello
    >>> def print_sth(s1,s2,s3):
    ...    print(s1,s2,s3)
    ...
    >>> print_sth(1,2,3)
    1 2 3

    >>> print_sth("a","b","c")
    a b c

    >>> print(print_sth())
    None
    >>> def print_sth():
    ... return "hello"
    ...
    >>> print(print_sth())
    hello
    >>> def return_sth(s):
    ... return s
    ...
    >>> print(return_sth(1))
    1
    >>> print(return_sth("a"))
    a

    >>> def return_sth(s):
    ... return s
    ...
    >>> print(return_sth([1,2,3]))
    [1, 2, 3]

    函数无法返回多个数值,都是放在容器里返回

    >>> def return_sth(s):
    ... return s
    ...
    >>> print(return_sth([1,2,3]))
    [1, 2, 3]
    >>> def return_sth(s1,s2):
    ... return s1,s2
    ...
    >>> print(return_sth(1,2))
    (1, 2)
    >>>

    >>> a=100
    >>> def func():
    ... print(a)
    ...
    >>> func()
    100

    >>> func()
    100
    >>> del a
    >>> func()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 2, in func
    NameError: name 'a' is not defined
    >>>函数里面取值,里面找不到就去外面找值

    >>> def func():
    ... global a
    ... a=a+1
    ... print(a)
    ...
    >>> func()
    101
    >>>参数不可变

    >>> a=100
    >>> def func(s):
    ... s=s+1
    ... return s
    ...
    >>> print(a)
    100
    >>> print(func(a))
    101
    >>> print(a)
    100

    >>> a=100
    >>> def func(s):
    ... s=s+1
    ... return s
    ...
    >>> print(a)
    100
    >>> print(func(a))
    101
    >>>参数为不可变类型,函数内部计算生效,外部不变 

    >>> s="abc"
    >>> def func(s):
    ... s=s+"d"
    ... return s
    ...
    >>> print(func(s))
    abcd
    >>> print(s)
    abc
    >>>可变类型传参如下

    >>> l=[1,2,3]
    >>> def func(s):
    ... s.append(4)
    ... return s
    >>> print(func(l))
    [1, 2, 3, 4]
    >>> print(l)
    [1, 2, 3, 4]

    >>> l=[1,2,3]
    >>> new_l=l[:]
    >>> new_l
    [1, 2, 3]
    >>> func(new_l)
    [1, 2, 3, 4]
    >>> l
    [1, 2, 3]
    >>>传递可变参数的时候,不想影响到外部的变量,就如上方法

    》》函数默认值如下

    >>> def func(a=100):
    ... print(a)
    ...
    >>> func()
    100
    >>> func(200)
    200
    >>>

    >>> def func(a=100,b):
    ... print(a,b)
    ...
    File "<stdin>", line 1
    SyntaxError: non-default argument follows default argument

    默认值参数必须在非默认值后面

    >>> def func(b,a=100):
    ... print(b,a)
    ...
    >>> func(1)
    1 100
    >>> func(1,200)
    1 200
    >>>

    >>> def func(a,b,*c):
    ... print(a)
    ... print(b)
    ... print(type(c))
    ... print(c)
    ...
    >>> func(1,2,3,4,5,6,7,8,9)
    1
    2
    <class 'tuple'>
    (3, 4, 5, 6, 7, 8, 9)
    >>>*是元组,把不定长参数传入到元组里供函数使用

    >>> def func(a,b,*c):
    ... result=0
    ... result+=a
    ... result+=b
    ... for i in c:
    ... result+=i
    ... return result
    ...
    >>> func(1,2)
    3
    >>> func(1,2,3)
    6
    >>> func(1,2,3,4)
    10
    >>> func(1,2,3,4,5)
    15
    >>>可变:list  set,dict

    不可变:数字,字符串,元组

    >>> def func(a,b,**d):
    ... print(a,b)
    ... print(type(d))
    ... print(d)
    ...
    >>> func(1,2,m=3,n=4,x=5)
    1 2
    <class 'dict'>
    {'m': 3, 'n': 4, 'x': 5}
    >>>用**d,求一下所有参数中的数字的和

    def func(a,b,*c,**d):

      result=0

      result+=a

      result+=b

      for i in c:

        result+=i

      for i in d.values():

        result+=i

      return result

    funct(1,2,3,4,m=5,n=6)

     

    >>> y=lambda x:x+1
    >>> y
    <function <lambda> at 0x00000297D33998C8>
    >>> y(1)
    2
    >>>

  • 相关阅读:
    WIN8.1安装 .net framework 3.5
    WIN8.1侧边栏文件夹删除
    初次接触Spring cloud微服务框架
    activiti中实现TaskListener注入Spring的bean
    spring中事务的回滚
    BeanUtils.copyProperties
    Java8新特性
    结构型模式总结
    创建型模式总结
    二十三种设计模式
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/14091431.html
Copyright © 2011-2022 走看看