python 函数基础

来源:互联网 发布:27周胎儿发育标准数据 编辑:程序博客网 时间:2024/04/29 04:36
#-*- coding:utf-8 -*-'''Created on 2016年12月16日@author: Administrator''''''定义函数 '''def sey_hi():    print("hi~");sey_hi()#调用函数"""传参数函数"""def sey_a_b(a,b):    return a+bprint(sey_a_b(2, 4))def say_hell_hi(str):    print("hello   "+str+"!")say_hell_hi("jack")##实现对hello 打印四次def repest_str(str,tiems):    repeated_str=str * tiems    return repeated_strrepest_strings=repest_str("hello   ", 4)print(repest_strings)print("hello   "*4)##全局变量def foo():    global x    print(str(x))    print(x)##默认参数的函数def fooo(s,t=1):    repeated=s*t;    return repeatedprint(fooo("jack",))##关键字参数  选择传入def func(a,b=10,c=8):    print("a is ",a ,"b is ",b,"c is ",c)func(123,c=23)   ##传入相应的关键字  如果指明关键字 那么顺序不太要#varargs 参数def print_paras(fpara,*nums,**words):  ##nums 后台当做元组来对待    print("fpara "+str(fpara))    print("nums"+str(nums))# 后台当做字典来存储    print("words"+str(words))# 后台当做字典来存储print_paras("hello",1,3,5,7,words="python",anohter_words="java")#
0 0
原创粉丝点击