本文主要是介绍python学习4---fiction,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# fiction函数
# def 定义def say_hi():print("hi!")
# 我们需要在函数体内用缩进,用tab键say_hi()
say_hi()def print_sum_two(a, b):c=a+bprint(c)
# 无返回值打印
# 传入具体参数
print_sum_two(3,6)def hello_some(str):print("Hello "+str+"!")hello_some("world")
hello_some("China")# 有返回值的例子
def repeat_str(str, times):repeated_str = str * timesreturn repeated_strrepeated_string =repeat_str("happy birthday! ",5)
print(repeated_string )#全局变量
x=60def foo(x):print("x is: ",str(x))x = 3 # 局部变量print("change local x to "+str(x))foo(x)
print("x is still "+str(x))y=60
# 在函数内部声明全局变量
def foo2(x):global yprint("y is: ",str(y))y = 3 # 局部变量print("change local x to "+str(y))foo2(y)
print("y is still "+str(y))
这篇关于python学习4---fiction的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!