# filename:python2.26(2).py
# author:super
# date:2018-2-26
print('today to learn the function')
def hello(username):
print('hello, world '+username)
def pet(animal1, animal2='pig'):
print('first one is '+animal1)
print('second one is '+animal2)
def get_name(fisrt_name, last_name):
full_name = fisrt_name + ' ' + last_name
return full_name
def get_formatted_name(first_name, last_name, middle_name=''):
if middle_name:
return first_name + middle_name + last_name
else:
return first_name + last_name
def build_person(first_name, last_name):
person = {'first': first_name, 'last': last_name}
return person
hello('super')
# 位置实参, 顺序很重要
pet('cat', 'dog')
# 关键字实参, 就不需要考虑顺序
pet(animal2='cat', animal1='dog')
# 当函数有默认值的时候, 那个参数可以不用传进去
pet('cat', 'piggggg')
# 函数有返回值的时候, 要有个变量去接这个值
name = get_name('su', 'super')
print(name)
# 让这个参数可用可不用的时候,可以让[参数]=''
name2 = get_formatted_name('su', 'super', 'good')
print(name2)
name3 = get_formatted_name('su', 'super')
print(name3)
# 函数可以返回一个复杂的数据结构, 如字典列表等
name4 = build_person('su', 'super')
print(name4)
# practice
def make_ablum(song_name, songer, count=''):
song = {'song_name': song_name, 'songer': songer}
if count:
song['count'] = count
return song
song1 = make_ablum('song1', 'singer1')
print(song1)
song2 = make_ablum('song2', 'singer2', 5)
print(song2)