zoukankan      html  css  js  c++  java
  • Python|函数

    '''
    函数化编程实现封装化编程
    默认参数一般放在最后面
    默认参数可以有多个,默认参数的值也是可以改变的
    默认参数需要指定赋值
    参数的顺序也可以不按照顺序
    一个*,会把所有的参数转换成一个元组
    2个*,会把所有的参数转换成一个字典
    一个*的放在前面,两个*的放在后面

    '''
    def show(a,b):
    print(a,b)
    show(b=123,a=456)
    def sho(a):
    print(a,type(a))
    n=[1,2,3,4]
    p={'k1':78,'k2':33}
    sho(n)
    def sh(*args):
    print(args,type(args))
    sh(n)
    def s(a,**kwargs):
    print("can",kwargs,type(kwargs))
    print(a)
    s(45,n1=78,uu=123,bb=99)
    s(45,**p)
    def m(*args,**kwargs):
    print(args,type(args))
    print(kwargs,type(kwargs))
    m(n,t=67,y=89)
    '''
    这种方式会把n,p全部放到args里面,改变
    '''
    m(n,p)
    """
    如下所示为解决方案
    """
    m(*n,**p)
    s="{0}{1}"
    ret=s.format('alex','b')
    l=['alex','b']
    print(ret)
    ret1=s.format(*l)
    print('chognxin',ret1)
    d={'k1':1,'ke':2}
    print(d)


    """
    lambda,简单方式定义一个函数
    第一个a为形式参数,可以为多个参数,参数后面为:,:后面为执行体。
    lambda会做两件事情,第一,创建形式参数,第二把函数内容返回,
    lambda只能够写一行,因此只适合于写简单函数
    """
    func=lambda a:a+1

    print(func(99))






    C:Python34python.exe C:/Users/Admin/PycharmProjects/untitled1/zidian/可命名元组/函数.py
    456 123
    [1, 2, 3, 4] <class 'list'>
    ([1, 2, 3, 4],) <class 'tuple'>
    can {'n1': 78, 'bb': 99, 'uu': 123} <class 'dict'>
    45
    can {'k2': 33, 'k1': 78} <class 'dict'>
    45
    ([1, 2, 3, 4],) <class 'tuple'>
    {'y': 89, 't': 67} <class 'dict'>
    ([1, 2, 3, 4], {'k2': 33, 'k1': 78}) <class 'tuple'>
    {} <class 'dict'>
    (1, 2, 3, 4) <class 'tuple'>
    {'k2': 33, 'k1': 78} <class 'dict'>
    alexb
    chognxin alexb
    {'ke': 2, 'k1': 1}
    100

    Process finished with exit code 0

  • 相关阅读:
    子序列自动机
    poj 暴力水题
    小白贪心题
    组合数+费马大/小定理
    随机数法
    vector的二维用法+前缀和
    巨思维题
    思维水题
    Codeforces Round #323 (Div. 2) D.Once Again... (nlogn LIS)
    Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
  • 原文地址:https://www.cnblogs.com/xwl65/p/5187113.html
Copyright © 2011-2022 走看看