ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1、设置默认值 ``` def print_welcome(first,last,middle=''): print "welcome,{},{},{}".format(first,last,middle) print "enjoy your stay!" print_welcome(first='james',last='Grinnell') print_welcome(first='katie',middle='alison',last='cunningham') ``` 2、返回值 ``` def get_total(items): total = 0 for item in items: total += item return total items = [1,2,3,4] item_total = get_total(items) print item_total ``` 3、可以返回多个值 ``` def get_square_and_cube(number): square = number ** 2 cube = number ** 3 return square,cube print get_square_and_cube(5) square,cube = get_square_and_cube(4) print square print cube ``` 4、验证程序健壮性,及时结束程序![] ``` def check_year(year): if len(year) != 4: print "{} is invalid as a year.".format(year) return print "Good that seems to work as a year!" check_year("80") ```