python学习-第四篇

"""
Version: 0.1
Author: freshbin
Date: 2019年8月20日
"""

print("=================================函数 start================================================")

# 局部变量
x = 50

def func_x(x):
    print('x is ', x)
    x = 2
    print('Changed local x to ', x)

func_x(x)
print('x is still ', x)

# 全局变量
y = 50
def func_y():
    global y

    print('y is ', y)
    y = 2
    print('Changed global y to ', y)

func_y()
print('Value of y is ', y)

# 默认参数值
def say(message, times=1):
    print(message * times)

say('Hello')
say('Hello', 5)

# 关键字参数
def func_3(a, b=5, c=10):
    print('a is', a, 'and b is', b, 'and c is', c)

func_3(3, 7)
func_3(25, c=24)
func_3(c=25, a=100)

# 可变参数
def total(a=5, *numbers, **phonebook):
    print('a', a)
    for single_item in numbers:
        print('single_item', single_item)

    for first_part, second_part in phonebook.items():
        print(first_part, second_part)

print(total(10, 1, 2, 3, Jack=1123, John=2231, Inge=1560))

# return语句
def maximum(x, y):
    if x > y:
        return x
    elif x == y:
        return 'The numbers are equal'
    else:
        return y

print(maximum(2, 3))

def some_function():
    pass

print(some_function())

# DocStrings
def print_max(x, y):
    '''Prints the maximum of two numbers.

    The tow values must be integers.
    :param x:
    :param y:
    :return:
    '''
    x = int(x)
    y = int(y)
    if x > y:
        return x
    elif x == y:
        return 'equal numbers'
    else:
        return y
print(print_max(3, 5))
print(print_max.__doc__)


print("=================================函数 end================================================")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值