美文网首页
023函数def

023函数def

作者: Nzkalhbxx | 来源:发表于2017-10-29 21:50 被阅读0次
# __author__:Nzkalhbxx
# __date__:17-10-29
import time
# 函数的定义:
"""
def functionName(形参1, 形参2...):
    函数体...
"""

def eatApple():
    time_format = "%Y-%m-%d  %X"
    time_current = time.strftime(time_format)
    print("you should eat %s %s at %s"%("apple", 3, time_current))
def eatBanana():
    time_format = "%Y-%m-%d  %X"
    time_current = time.strftime(time_format)
    print("you should eat %s %s at %s"%("Banana", 7, time_current))
def eat_passion_fruit():
    time_format = "%Y-%m-%d  %X"
    time_current = time.strftime(time_format)
    print("you should eat %s %s at %s"%("passion_fruit", 11, time_current))

eatApple()
eatBanana()
eat_passion_fruit()

print("使用参数".center(33, "-"))
def eat(fruit, num, name=""):
    time_format = "%Y-%m-%d  %X"
    time_current = time.strftime(time_format)
    if name != "":
        print(name, end=", ")
    print("you should eat %s %d at %s" % (fruit, num, time_current))

# 只有当形参有默认值时可以缺省, 否则eat() missing 1 required positional argument: 'name'
eat("apple", 7)
# 当一个实参使用key = value传递参数时顺序可以随意, 但未使用k-v的参数必须写在前面
eat("banana", name="", num=3)
# 当不使用key = value时, 参数顺序必须一一对应
eat("passion_fruit", 7, "psj")
# eat("passion_fruit", "psj", 19)     # TypeError: %d format: a number is required, not str

运行结果

相关文章

  • 023函数def

  • 【python基础】3-函数

    def:定义函数 print 函数 range 函数 type 函数 变量作用域 def:定义函数 def关键字用...

  • def 函数

    定义 调用 默认参数 关键子参数 不定参数 多返回值 拆包/解构

  • 2018-10-14

    C基础入门Python(四)——函数 一、创建函数 1、def语句创建函数 def 函数名(参数表)def hel...

  • 函数

    创建函数def , define 缩写,用来定义函数。 ①无参函数 def function() print(...

  • Python 3 学习笔记 -- def 函数

    一、def 函数基础知识 def 函数的定义 在 python 中,def 函数是用来定义一个 function(...

  • python3学习笔记--函数

    函数(function) 定义函数:def 函数名 + ":" 参数 多个参数(使用*arg) def p...

  • Python学习(五)

    函数 def 关键字创建函数; def FirstFunction(): -----方法体 调用函数,通过方法名...

  • Python-三、函数

    3.1 def语句和参数 def ...(...)语句用于定义一个函数,以便后面调用函数,如: def hello...

  • 函数 def 等

    '''def myfun(str): '我是解释的文字' print(str)#调用 定义和调用不能出...

网友评论

      本文标题:023函数def

      本文链接:https://www.haomeiwen.com/subject/vxdupxtx.html