Python学习第十篇——函数初步
1 def make_album(name,album_name,song_nums = 1): 2 dict_album = {name:[album_name]} 3 if int(song_nums) > 1: 4 dict_album[name].append(song_nums) 5 return dict_album 6 first = make_album('Avril','Fly') 7 print(str(first)+"\n") 8 second = make_album('Jay Zhou','Jay Chou\'s bedside story',10) 9 print(str(second)+"\n") 10 third = make_album('Tall swift','welcome to New York',12) 11 print(str(third)+"\n") 12 while True: 13 print("\nplease enter the information of album:") 14 name = input("singer's name:") 15 album_name = input("album's name:") 16 song_nums = input("song's number:") 17 album = make_album(name,album_name,song_nums) 18 print(album)
上段代码描述了函数的定义和使用:
1.函数定义,用def关键字,注意函数定义的格式
2 函数的返回值可以是列表和字典,总之,是很丰富;这也从侧面反应了:函数的形参也可以是列表和字典
3 函数的形参可以有默认形参,但默认形参通常只能放在()的最后面
stay foolish,stay hungry