Python 函数的定义

函数的定义
函数的定义
- 将一件事情的步骤
封装在一起并得到的结果 - 函数名代表了这个函数要做的事情
- 函数体是实现函数功能的流程
- 方法或功能
- 函数可以帮助我们重复使用,通过函数名我们可以知道函数的作用
函数的分类
- 内置函数
- 自定义函数
函数的创建方法
关键词def的功能
- 实现python中函数的创建
通过def定义函数
def name(args...):
todo something..
返回值
#函数名 + 小括号执行函数
函数的返回值return
- 将函数结果返回的关键字
return只能在函数体内使用return支持所有返回的python类型有返回值的函数可以直接赋值给一个变量#参数按顺序传递
- 代表函数执行的结束
(break)
return与print的区别
print只是单纯的将对象打印 , 不支持赋值语句return是对函数执行结果的返回,也支持赋值语句
代码
# coding:utf-8
def capitalize(data):
index = 0
temp = ''
for item in data:
if index == 0:
temp = item.upper() # 此处是直接赋值给temp变量 故也可写成 +=
else:
temp += item # 此处是给temp变量添加元素
index += 1
print('for结束了')
return temp
# print('finishi')
result = capitalize('hello 小慕')
print(result)
def message(message, message_type):
new_message = '[%s] %s' % (message_type, message)
print(new_message)
result = message(message='今天天气真好', message_type='info')
print('message result:', result)
def test():
for i in range(10):
if i == 5:
return i
print('test的结果是:', test())
Comments
Join the discussionNo comments yet. Be the first to comment.




