zoukankan      html  css  js  c++  java
  • 第三篇:python基础_3

     

    本篇内容

    1. 文件处理补充

    2. 函数基本语法及特性

    3. 参数

    4. 返回值

    5. 嵌套函数

     

    一、文件处理补充

    1、文件操作的内置方法

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    with open("test.txt","r+",encoding="utf-8") as file:
        file.write("
    aaaaaa")        #换行写入aaaaaa   
        print(file.readline())        #以字符串的方式读一行test.txt的内容
        print(file.read())            #以字符串的方式读光标以后的test.txt的所有内容
        file.seek(0)                  #将光标回到开头处  
        file.flush()                  #刷新内存中的内容到test.txt中
        print(file.readlines())       #以列表的方式读光标以后的test.txt的所有内容
        print(file.tell())            #打印光标在第一个字符
        file.close()                  #关闭test.txt在内存的打开
    


    2、文本操作a模式

    文件以a模式打开,光标会默认在文本的末尾。a模式只可以追加,不可读。而a+模式既可以追加,又可以读。当文件不存在的时候,会创建文件,并追加写入。

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    with open("test.txt","a",encoding="utf-8") as file:
        file.write("test")
    


    3、文件操作b模式

    文件以b模式打开,默认字符是以bytes的字符集打开的,无需自己定义以什么字符集打开。

    以下是模拟linux的tail -f命令的代码:

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    import time, sys
    def tail_file(parameter,f_file):
        with open(f_file,"rb") as f:
            f.seek(0,2)
            while True:
                line = f.readline()
                if line:
                    print(line.decode("utf-8"),end="")
                else:
                    time.sleep(1)
    
    if __name__ == "__main__":
        tail_file(sys.argv[1], sys.argv[2])
    

    b模式还可以完成图片或者视频的复制功能。

    以下是模拟linux的copy命令的代码:

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    import sys
    
    def copy_file(old_file, new_file):
        with open(old_file,"rb") as f_read, open(new_file,"wb") as f_write:
            for line in f_read:
                f_write.write(line)
    
    if __name__ == "__main__":
        copy_file(sys.argv[1], sys.argv[2])
    


    二、函数基本语法及特性

    函数是什么?

    函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法。在BASIC中叫做subroutine(子过程或子程序),在Pascal中叫做procedure(过程)和function,在C中只有function,在Java里面叫做method。

    定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可

    特性:

    1. 减少重复代码

    2. 使程序变的可扩展

    3. 使程序变得易维护

    1.语法定义

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def test():
        print("Hello,World")
    
    test()
    

    2.函数即变量

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def foo():
        print("foo")
        bar()
    
    def bar():
        print("bar")
    
    foo()        #函数可以正常运行,说明函数是先定义,再调用。这一点与变量相同。
    

    3.函数还可以传递参数

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    a = 1
    b = 10
    c = a + b
    print(c)
    
    
    
    def test(x, y):
        z = x + y
        return z
    
    res = test(x=1,y=10)
    print(res)
    

    三、参数

    1.形参与实参

    形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量。

    实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值。

    2.位置参数数与关键字参数

    注意:关键字参数不能在位置参数的前面

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def user_info(name, age, sex):
        print(name)
        print(age)
        print(sex)
    
    user_info("yanglei", 18, "male")                      #位置实参
    user_info(name="yanglei", age=18, sex="male")         #关键字实参
    user_info("yanglei", 18, sex="male")                  #位置实参与关键字实参混合
    user_info("yanglei", age=18, sex="male")              #位置实参与关键字实参混合
    

     3.非固定参数

    若你的函数在定义时不确定用户想传入多少个参数,就可以使用非固定参数。

    args

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def test(x,y,*args):
        print(x)
        print(y)
        print(args)
    
    test(1,2,3,4,5,6,7)
    

    kwargs

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def test(x,y,**kwargs):
        print(x)
        print(y)
        print(kwargs)
    
    test(x=1,y=2,z=3,a=4,j=5,g=6,l=7)
    


    四、返回值

    要想获取函数的执行结果,就可以用return语句把结果返回。

    注意:

    1. 函数在执行过程中只要遇到return语句,就会停止执行并返回结果,也可以理解为 return 语句代表着函数的结束

    2. 如果未在函数中指定return,那这个函数的返回值为None

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def test():
        print("test")
        return
        print("test")
        print("test")
        print("test")
    
    test()
    
    #return后面的三个打印test不会被执行
    
    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def test():
        print("test")
        return
    
    res = test()
    print(res)
    
    #return会默认返回None
    
    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def test():
        print("test")
        return test1
    
    def test1():
        print("test1")
    
    res = test()
    res()
    
    #return可以返回你定义的值,包括函数也可以被返回
    

    五、嵌套函数

    嵌套函数,顾名思义就是函数里面套函数。

    #!/usr/bin/env pyhon
    #encoding: utf-8
    #auth: yanglei
    
    def test():
        print("test")
        def test1():
            print("test1")
        test1()
    
    test()
    

     

  • 相关阅读:
    免费的视频、音频转文本
    Errors are values
    Codebase Refactoring (with help from Go)
    Golang中的坑二
    Cleaner, more elegant, and wrong(msdn blog)
    Cleaner, more elegant, and wrong(翻译)
    Cleaner, more elegant, and harder to recognize(翻译)
    vue控制父子组件渲染顺序
    computed 和 watch 组合使用,监听数据全局数据状态
    webstorm破解方法
  • 原文地址:https://www.cnblogs.com/00doudou00/p/7202822.html
Copyright © 2011-2022 走看看