zoukankan      html  css  js  c++  java
  • python 基础 4.0 函数的一般形式及传参

    #/usr/bin/python
    #coding=utf-8
    #@Time   :2017/10/23 15:58
    #@Auther :liuzhenchuan
    #@File   :函数的一般形式.py
     
    ##函数的定义
      #x y 为形参 ,sum(6,4)叫实参
    def sum(x,y):
        print {'x = 0'.format(x)}
        print {'y = 0'.format(y)}
        return x + y
    m = sum(6,4)
    print m
     
    >>> set(['x = 0'])
        set(['y = 0'])
        10
     
     
    ###函数的参数
      #给形参b定义一个默认的值
    def funcA(a,b=0):
        print a
        print b
    funcA(1)
     
    >>> 1
        0
     
      #如果实参传入的时候,判定了b的值,那么b优先选择传入的实参,当b没有值时,才会选择默认的值
    def funcA(a,b=0):
        print a
        print b
    funcA(10,30)
     
    >>> 10
        30
     
     
    ##参数为tuple
      #a对应1,b对应2,*c对应剩下的元组列型
    def funcD(a,b,*c):
        print a
        print b
        print 'length of c is :%d' % len(c)
        print c
    funcD(1,2,3,4,5,6)
    >>> 10
    1
    2
    length of c is :4
    (3, 4, 5, 6)
     
    #通过解包的形式传入元组
    def funcD(a,b,*c):
        print a
        print b
        print 'length of c is :%d' % len(c)
        print c
    # funcD(1,2,3,4,5,6)
       #通过解包的形式传入元组
    test = ('hello','world')
    funcD(1,2,*test)
     
    >>> 1
        2
        length of c is :2
        ('hello', 'world')
     
     
     
     
     
    ##传入参数为字典,两个星号b代表的是字典,x是字典的键值,b[x]是字典的value.100是a的实参;x='hello',y='nihao' 是b的实参,是给a和b赋值
    所以不能写成 a='hello',b='nihao',也就是不能再用a和b当作实参。
    def funcF(a,**b):
        print a
        print b
        for x in b:
            print x  +  ":" + str(b[x])
    funcF(100,x='hello',y='nihao')
     
    >>> {'y': 'nihao', 'x': 'hello'}
    y:你好
    x:hello
     
    #还可以通过解包的形式,传入字典
    def funcF(a,**b):
        print a
        print b
        for x in b:
            print x  +  ":" + str(b[x])
    # funcF(100,x='hello',y='你好')
       #还可以通过解包的形式传入字典
    args = {'1':'a','2':'b'}
    funcF(100,**args)
     
    >>> {'1': 'a', '2': 'b'}
         1:a
         2:b
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    公司上线流程 pushonline_alpha
    关于统计数据
    实习生新手指南1
    菜鸟代码学习散点总结(四)
    菜鸟机器学习散点总结(三)
    菜鸟机器学习散点总结(二)
    springboot 中事件监听模型的一种实现
    java 代码获取视频时长
    spring cloud 服务链路追踪 skywalking 6.1
    windows 中如何定位恶意软件的藏身位置
  • 原文地址:https://www.cnblogs.com/lzcys8868/p/7768804.html
Copyright © 2011-2022 走看看