大家好,我是yma16,本文分享关于python函数入门使用。
该系列文章:
python爬虫_基本数据类型
python函数:
def say_hello():
return "I am yma16 a csdn player,hello world!"
if __name__ == '__main__':
print(say_hello())
运行结果
I am yma16 a csdn player,hello world!
传递number、string、tuple时为值传递
示例传递:number
def say(word):
word=word+1
return word
word=0
if __name__ == '__main__':
print(word)
print(say(word))
print(word)
运行结果
0
1
0
示例传递:string
def say(word):
word=word+' yma16'
return word
word='name'
if __name__ == '__main__':
print(word)
print(say(word))
print(word)
运行结果
name
name yma16
name
传递数组,在函数改变数组会改变初始值
def say(word):
word.append('hello')
return word
word=['yma16','csdn']
if __name__ == '__main__':
print(word)
print(say(word))
print(word)
运行结果
['yma16', 'csdn']
['yma16', 'csdn', 'hello']
['yma16', 'csdn', 'hello']
*变量接受多参数,对别c++的指针学习很容易理解
示例:传入多参数并入数组
def say(word,*vartuple):
for i in vartuple:
print(i)
word.append(i)
return word
word=['yma16','csdn']
if __name__ == '__main__':
print(word)
print(say(word,1,2,3))
print(word)
运行结果
['yma16', 'csdn']
1
2
3
['yma16', 'csdn', 1, 2, 3]
['yma16', 'csdn', 1, 2, 3]
lambda 创建匿名函数
示例:
join_str = lambda arg1, arg2: arg1 + arg2
word=['yma16','csdn']
if __name__ == '__main__':
print(join_str)
print(join_str('csdn',' yma16'))
运行结果:
<function <lambda> at 0x000001B6870AC1E0>
csdn yma16
本文分享python函数使用到这结束!
点赞,是我创作的动力!
⭐️ 收藏,是我努力的方向!
✏️ 评论,是我进步的财富!
感谢你的阅读!