• Python记录5:函数1


    函数

    '''
    1.
    什么是函数
        在程序中具备某一功能的工具->函数
        函数的使用必须遵循原则:
            先定义
            后调用

        函数分为两大类:
            1 内置函数
            2 自定义函数

    2. 为何要用函数
        1. 代码的组织结构不清晰,可读性差
        2. 代码冗余
        3. 可扩展性差

    3. 如何用函数

    '''
    # 先定义,语法为:
    # def 函数名(参数1,参数2,参数3):
    #     """文档注释"""
    #     代码1
    #     代码2
    #     代码3
    #     ......
    #     return 返回值

    # 后调用,语法为:
    # 函数名(值1,值2,值3)

    # def func():
    #     print('from func1....')
    #     print('from func2....')
    #     print('from func3....')
    #     print('from func4....')
    #
    # func()
    # func()

    # 函数定义阶段:只检测语法不执行代码
    # def func(): # 会将函数体代码保存起来然后将内存地址赋值给函数名func
    #     print('from func1....')
    #     print('from func2....')
    #     print('from func3....')
    #     print('from func4....')
    #     # asdfsadfsadfsadfasdf
    #     if

    # print(func)
    # 函数调用阶段:触发函数体代码的运行
    # func()  #函数名后面加一个括号表示运行函数,不加括号就不运行


    # 小练习1
    # def bar():
    #     print('from bar')
    #
    # def foo():
    #     print('from foo')
    #     bar()
    #
    # foo()

    # 小练习2
    # def foo():
    #     print('from foo')
    #     bar()
    #
    # def bar():
    #     print('from bar')
    #
    # foo()

    # 小练习3
    # def foo():
    #     print('from foo')
    #     bar()
    #
    # foo()
    #
    # def bar():
    #     print('from bar')

    # 有参函数与无参函数
    #定义函数
    # def auth():
    #     user=input('username>>>: ').strip()
    #     pwd=input('password>>>: ').strip()
    #     if user == 'egon' and pwd == '123':
    #         print('login successfull')
    #     else:
    #         print('username or passsword error')
    # 调用函数
    # auth()

    #先定义一个函数,比较两个数的大小并返回较大的那个
    # def max2(x,y):
    #     # x=10
    #     # y=20
    #     if x > y:
    #         return (x)
    #     else:
    #         return(y)
    #运用求两个数的大小比较函数求三个数的较大者
    # x=input('please input a number')
    # y=input('please input a number')
    # z=input('please input a number')
    # print(max2(max2(x,y),z))


    # 函数返回值:
    # def max2(x,y):
    #     if x > y:
    #         return x
    #     else:
    #         return y
    #
    # res=max2(10,20)
    # print(res)
    #####定义一个登陆的函数,调用函数,输入名字 密码 年龄 然后打开一个文件,如果这个文件之前不存在就创建一个新的文件,
    #####然后再将这些内容写入这个文档记录下来
    # def register():
    #     name=input('名字: ').strip()
    #     pwd=input('密碼: ').strip()
    #     age=input('年齡: ').strip()
    #     with open('asd.txt','at',encoding='utf-8') as f:
    #         f.write('%s,%s,%s ' %(name,pwd,age))
    #
    # register()


    # with open('db.txt','rt',encoding='utf-8') as f:
    #     for line in f:
    #         # print(line)
    #         print(line.strip(' ').split(','))


    # return:
    # 1.return是函数结束的标志,函数内可以有多个return,但只要执行一次return整个函数就立刻结束,并且将return后的返回值
    # 当做本次调用结果返回
    # 2.return控制返回值
    # 没有return===>返回None
    # return 值====>返回该值本身
    # return多个用逗号分隔开的值====>返回一个元组
    #
    # def func():
    #     return 1,'aa',{'x':1}
    #
    # res=func()
    # print(res)
    # def func():
        # print('first')
        # return 1
        # print('second')
        # return 2
        # print('third')
        # return 3
        # while  True:
        #     while True:
        #         while True:
        #             return
    # func()

  • 相关阅读:
    获取iphone当前的语言设置
    UIscrollView通过Button来实现view的切换的方法
    Citrix 客户端登录出现wfshell.exe 应用程序错误的解决方法
    WCF 学习资源
    There is no Citrix MetaFrame server configured on the specified address错误的解决方法
    ASP.NET AJAX,WCF,ADO.NET Entity 开发实例
    SC命令配置服务
    Citrix 客户端登录出现wfshell.exe 应用程序错误的解决方法
    删除子窗体中的控件中的某些属性时出现"Selection contains a component introduced in an ancestor form which cannot be deleted."错误的解决方法
    There is no Citrix MetaFrame server configured on the specified address错误的解决方法
  • 原文地址:https://www.cnblogs.com/1832921tongjieducn/p/10069697.html
Copyright © 2020-2023  润新知