zoukankan      html  css  js  c++  java
  • Python 函数的一般形式及参数

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time   : 2017/11/01 21:46
    # @Author : lijunjiang
    # @File   : fanction.py
    
    """函数的一般形式"""
    
    """函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段
        可以提高应用的模块性,和代码的重复利用率
        分为内建函数和自定义函数
    """
    """
        函数以def关键字开头,后面跟函数标识符和圆括号()
        任何传入参数和自定义变量必须放在圆括号中间(定义形参)
        函数内容以冒号开始,并且缩进
        return[表达式]结束函数,可以选择性的返回一个值给调用方,不带表达式的return相当于返回None
    """
    
    # 定义一个函数
    # def functionname( parameters):
    #     代码段
    #     return [expression]
    
    
    # 示例:
    name = raw_input('please input your name: ')
    
    
    def fun1(input_name):
        print(input_name)
        return
    
    
    fun1(name)
    
    """
    执行结果:
    C:Python27python.exe D:/Python/Study/fanction.py
    please input your name: lijiang
    lijiang
    
    Process finished with exit code 0
    
    """
    
    """函数的调用"""
    """
        如上例所示:定义完成一个函数时,需要使用 函数名([参数]) 的形式调用函数(传入参数为实际参数)
    """
    
    """函数的参数-形参与实参"""
    """
    上例,  在函数 fun1(input_name) 中,input_name 为形参
           函数调用 fun1(name) 中, name 为实参
    """
    
    
    # 声明一个函数时可以不定义形参,但是声明函数时如果定义了形参,则在调用时必须传放实参或设置缺省值
    
    def fun2():
        print('hello {}'.format(name))
        return
    
    
    fun2()
    
    
    # 输出 hello lijunjiang
    
    # 正常情况下,在函数声明时,如果有一个或多个形参,则在调用该函数时,也需要传入一个或多个实参,
    # 且一般每一个形参和实参是相互对应的 (必备参数)
    def func3(x, y, z):
        print('x = {0}'.format(x))
        print('y = {0}'.format(y))
        print('z = {0}'.format(z))
    
    
    func3(10, 20, 30)
    """
    执行结果:
    x = 10
    y = 20
    z = 30
    """
    # 也可以在调用时指定   (关键字参数),   使函数调用时参数的顺序和声时不一致
    func3(z=30, x=10, y=20)
    """
    x = 10
    y = 20
    z = 30
    """
    
    
    # 在定函数时,也可设置形参的缺省值  (缺省参数)
    
    def func4(x, y=100):
        print('x = {0}'.format(x))
        print('y = {0}'.format(y))
    
    
    func4(10, 50)
    func4(10)
    """
    x = 10
    y = 50
    
    x = 10
    y = 100
    从结果可以看出.当调用时传入第二个实参时,以传入实参为准,不传入时,默认的缺省值生效
    """
    
    
    # *var_args_tuple 和 **var_args_dict (不定长参数)
    # 当一个函数需要处理比声明时更多的参数时,可使用*var_args_tuple 和 **var_args_dict
    
    # *var_args_tuple  传入的是一个tuple 类型
    def func5(x, y, *z):
        print('x = {0}'.format(x))
        print('y = {0}'.format(y))
        print('z = {0}'.format(z))
        print('lenth of z is: {0}'.format(len(z)))
        for i in z:
            print(i)
    func5(10, 20, 30, 40, 50, 60)
    
    #等价于
    # tuple_test = (30, 40, 50, 60)
    # func5(10, 20, tuple_test)
    
    """
    x = 10
    y = 20
    z = (30, 40, 50, 60)
    lenth of z is: 4
    30
    40
    50
    60
    从结果可以看出,x, y,分别对应前两个实参10 20,剩余的参数30 40 50 60 做为一个元组传递给形参 z
    """
    # **var_args_dict  传入的是一个dict 类型
    def func6(a, b, **c):
        print('a = {0}'.format(a))
        print('b = {0}'.format(b))
        print('c = {0}'.format(c))
        print(type(c))
        for key, valaue in c.iteritems():
            print('key = {0}, valaue = {1}'.format(key, valaue))
    
    func6(10, 50, x='hello', y='world')
    # 等价于
    # dict_test = {'x':'hello', 'y':'world'}
    # func6(10, 50, **dict_test)
    """
    a = 10
    b = 50
    c = {'y': 'world', 'x': 'hello'}
    <type 'dict'>
    key = y, valaue = world
    key = x, valaue = hello
    
    从结果可以看出,10 传给了形参a ,50 传给了形参 b , x='hello', y='world' 作为一个字典传给了 形参 c 
    """
    
  • 相关阅读:
    VS code 配置 PySide6的UI开发环境
    Python及PySide6学习网址
    NOIP2021模拟赛10.12 题解
    P2388 阶乘之乘 题解
    P3992 [BJOI2017]开车
    「NOIP2021模拟赛四 B」Polyline 题解
    P7115 [NOIP2020] 移球游戏 题解
    P7114 [NOIP2020] 字符串匹配 题解
    P3391 【模板】文艺平衡树 题解
    致夏天
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7769168.html
Copyright © 2011-2022 走看看