码迷,mamicode.com
首页 > 编程语言 > 详细

python函数01

时间:2017-12-22 20:45:12      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:div   hang   全局   str   print   返回值   sum   body   log   

# 没有参数和返回的函数
def say_hi():
    print(" hi!")
say_hi()
say_hi()

# 有参数,无返回值
def print_sum_two(a, b):
    c = a + b
    print(c)

print_sum_two(3, 6)

def hello_some(str):
    print("hello " + str + "!")

hello_some("China")
hello_some("Python")


# 有参数,有返回值
def repeat_str(str, times):
    repeated_strs = str * times
    return repeated_strs


repeated_strings = repeat_str("Happy Birthday!", 4)
print(repeated_strings)


# 全局变量与局部 变量
x = 60

def foo(x):
    print("x is: " + str(x))
    x = 3
    print("change local x to " + str(x))

foo(x)
print(‘x is still‘, str(x))

  输出:

 hi!
 hi!
9
hello China!
hello Python!
Happy Birthday!Happy Birthday!Happy Birthday!Happy Birthday!
x is: 60
change local x to 3
x is still 60

  

python函数01

标签:div   hang   全局   str   print   返回值   sum   body   log   

原文地址:http://www.cnblogs.com/zlz-leidaxia/p/8087431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!