人生苦短_我用Python_def(函数)_004

# coding=utf-8

# function函数:内置函数
# 例如: len int extent list range str
# print insert append pop reverse sort
# upper strip  split lower

# 特点、作用:
# 1、可以直接调用
# 2、可以重复调用
# def ----->> definition function
# 函数  def  关键字  函数名/方法名 ->>有class、def就是方法;无class就是函数

# 定义函数

'''
def print_list():
    list_ = [1, 2, 3, 4, 5]
    print(list_)


# 调用函数  函数名()
print_list()
'''

'''
def demo_(list_):
    for items in list_:
        print('language very good: %s' % items)


list_ = ['python', 'demo', 'function', 'css', 'javascript']
demo_(list_)
'''

'''
def demo__(age, name):
    print(age + '的岁数', name + '是名字')


demo__('11', 'demo')

# 函数可以有多个参数,但是函数体里面,但不一定要全部都用
# 函数定义的时候,需要几个位置参数,那么调用函数的时候,也要传递几个参数
# 完成任意连续整数序列的累加计算
def dem_test(a, b):
    sum_ = 0
    for items in range(a, b):
        sum_ += items
    print(sum_)


dem_test(22, 99)
'''

# name为位置参数、age为默认参数
# 默认参数一定是在位置参数后面,不然会报错->age:默认值为十八,同类型字符用"+"号拼接,不同类型用","号拼接
# 也可以全部传默认参数
def str_demo(name, age='十八'):
    print(name+'', age, '最棒的......')


str_demo('demo')  # demo 18 最棒的......

'''
def 函数名(位置参数,默认参数):
    # 函数名  见名知意 按照规范去做
    # 位置参数  一定是默认参数之前
    # 可以有多个位置参数,也可以有多个默认参数
    # 函数调用:  函数名(位置参数吗,*****)
'''

猜你喜欢

转载自www.cnblogs.com/mrchenyushen/p/9094683.html