Python( 八)函数

作者: Tester_Jingel | 来源:发表于2017-11-28 17:33 被阅读3次

    1、定义函数
    def text():
    Print “this is simple demo”
    text()

    2、实参与形参
    函数的定义的是形参
    调用函数传的值就是实参
    例:def fav_book(title): #此处title是形参
    Print title
    fav_book(“Python”) #此处传的值是实参

    3、位置实参、关键字实参和默认值参数
    (1)位置实参:按照声明的形参的顺序填写传的实参值,严格按照顺序来
    def fav_book(title,pages,author):
    Print “The” +title+”is all”+pages+”pages,” +author +”is it author”
    fav_book(“python”,”205”,”Mark”)

    (2)关键字实参:按照变量名赋值实参,无所谓位置
    def fav_book(title,pages,author):
    Print “The” +title+”is all”+pages+”pages,” +author +”is it author”
    fav_book(title =“python”, author = ”Mark”,pages = “205”)

    (3)默认值实参:使用默认值时注意一定要将没有默认值的形参放在有默认值形参的前面,不然肯定会报错
    def fav_book(pages,author,title=”python”):
    print "The" + title +"is all"+str(pages)+"pages," +author +"is it author"
    fav_book(author = "Mark",pages = 205)
    fav_book("Mark",205)
    fav_book("Mark",205,”python3”)#重新赋值默认形参的值也是可以的

    4、返回值 return
    (1)简单返回值
    def fav_book(title,pages,author):
    all = “The” +title+”is all”+pages+”pages,” +author +”is it author”
    return all
    demo = fav_book(title =“python”, author = ”Mark”,pages = “205”)
    Print demo

    (2)实参可选择
    给形参赋值一个空的值即可
    def fav_book(title,pages,author=””):
    (3)返回字典
    def build_person(first_name,last_name,age=''):
    person = {"first":first_name,"last":last_name}
    if age:
    person["age"]=age
    return person
    persons = build_person("jack","burs",20)
    print persons

    (4)While循环结合函数

    def make_album(singer,music,songs_num=''):
        message = {"sinage":singer,"music":music}
        if songs_num:
            message["songs_num"]=songs_num
        return message
    
    while True:
        singer = raw_input("singer name, if quit input quit \n")
        if singer =="quit" :
            break
        music = raw_input("music name ,if quit input quit \n")
        if music =="quit":
            break
        songs_num = raw_input("songs_num ,if quit input quit \n")
        if songs_num =="quit":
            break    
        messages = make_album(singer,music,songs_num)
        print messages
    

    (5)传递任意数量的实参
    def pizza(*demo)
    Print demo
    Pizza(‘aa’ ,’bb’ ,’cc’)

    输出结果:
    ('aa', 'bb', 'cc')

    传递任意数量的关键字实参

    (6)传递列表、禁止修改列表

    unprinted = ['iphone case','dodechecorn','robot']
    completed = []
    
    def unprinted_design(unprinted,completed):
        while unprinted:
            current_design = unprinted.pop()
            print "unprinted"+" :"+current_design 
            completed.append(current_design)
    
    def completed_design(completed):
        for completed_model in completed:
            print "complete" + " :"+completed_model
    
    #change data
    unprinted_design(unprinted,completed)
    #don't change data
    #unprinted_design(unprinted[:],completed)
    completed_design(completed)
    print unprinted
    print completed
    

    5、将函数存入模块中

    moudel.py
    def login_in(username,password):
    Print username
    Print password


    demo.py
    Import moudel
    moudel.login_in(‘ Tester Jingel ‘,123456)


    或者 from moudel.py import login_in --------login_in(‘Tester Jingel’ , 123456)
    或者 from moudel.py import login_in as login-----login.login_in(‘Tester Jingel’ , 123456)
    或者 import moudel.py as login--------login.login_in(‘Tester Jingel’ , 123456)
    导入所有的模块函数 from moudel.py import *

    相关文章

      网友评论

        本文标题:Python( 八)函数

        本文链接:https://www.haomeiwen.com/subject/kyuebxtx.html