zoukankan      html  css  js  c++  java
  • 函数简介

    一、函数

    函数就是一个功能,一个方法,简化代码。函数是用来提高代码的复用性的。
    #一个函数只做一件事
    #重复的代码就是低级的
    #实现同样的功能,代码越少越好

    函数必须得调用才能执行。

    1、函数的简单介绍,如下

    1 def say(name):# 函数,形参,形式参数,变量
    2     print(name)# 函数体
    3 x = 'yhx'
    4 say(x)#实参,实际参数

    2、函数之必填参数、位置参数

    位置参数的形参与实参不一致时,以实参为准

    1 def say(name,sex=''):# 函数,形参,形式参数,变量
    2     # 必填参数   位置参数
    3     # 默认值参数  非必填参数
    4     print('%s哈哈哈%s'%(name,sex)) # 哈哈哈
    5     # 函数体
    6 x = 'nn'
    7 say(x)
    8 say(x,'') #实参,实际参数
    9 # 函数必须得调用才会执行

    3、函数的特点

    #在函数里面的变量全都是局部变量,它只能在函数里面使用,函数执行结束那么没有这个变量
    #返回值
    # 如果需要用到函数的处理结果的话,那么就写return,不需要的话,那就不用写
    #函数里面如果碰到return,函数立即结束

    1 # 计算两个数字相乘
    2 def calc(a,b):
    3     res = (a*b) # res是局部变量,只在函数体内有效
    4     print(res)   # 所以输入结果要写在函数体内,可输出执行结果
    5 cf = calc(3,9)

    使用return:

    1 # 计算两个数字相乘
    2 def calc(a,b):
    3     res = (a*b) # res是局部变量,只在函数体内有效
    4     return res# 函数体外要调用这个结果,则需要return
    5 cf = calc(3,9)
    6 print(cf)   # res这个变量是在函数体内的,无法在函数体外直接调用,故此处输出应写cf

    4、默认值参数

    content参数有默认值,默认就为None

    如下代码,如果传给了我参数的内容,那我就认为是要写的,如果没传,那就是要读的

    #默认值参数
    
    def op_file(file_name,content=None):
        print(content)
        if content:
            write_file(file_name,content)
        else:
            result = read_file(file_name)
            return result
    
    print( op_file('1.txt') )
    op_file('1.txt','sdfdssgsdgsdg')

    5、函数返回值

    如果一个函数没有写返回值的话,返回的就是None
    如果函数有多个返回值,那么返回的是一个元组
    def test():
        print('hello')
    
    def test2():
        return 1,2,3
    
    
    #如果一个函数没有写返回值的话,返回的就是None
    #如果函数有多个返回值,那么返回的是一个元组
    
    a,b,c = test2()

    6、练习

    (1)

     1 def my():
     2     for i in range(100):
     3         print(i)
     4         if i==2:
     5             return
     6 print(my())
     7 # 运行结果:
     8 # 0
     9 # 1
    10 # 2
    11 # None

    (2)写一个校验输入的字符串是否为小数的程序

     1 #    0.12  -12.9
     2 # (1) 只有一个小数点,判断小数点个数
     3 # (2) 正小数的情况下,小数点左边和右边都是整数的话,才合法
     4 # (3) 负小数的情况下,小数点右边是整数,左边必须以‘-’开头,且只有一个负号才合法
     5 #   -5.4
     6 #   ['-5','4']
     7 #   [1:]
     8 
     9 def check_float(s):
    10     s = str(s)
    11     if s.count('.') == 1:
    12         s_list = s.split('.')
    13         # 5.3  [5,3]
    14         # -98.37  [-98,37]
    15         left = s_list[0]# 小数点左边‘-98’
    16         right = s_list[1]# 小数点右边
    17         if left.isdigit() and right.isdigit():# 这里是正小数的
    18             return True
    19         elif left.startswith('-') and left.count('-') == 1 and right.isdigit():
    20             return True
    21     return False
    22 print(check_float(1.8))
    23 print(check_float(-1.8))
    24 print(check_float('1.8'))
    25 print(check_float('-1.8s'))

     (3)

     1 def myfile(name,content = None):
     2     with open(name,'a+') as f:
     3         f.seek(0)
     4         if content:# 如果有content参数,则进行写
     5             f.write(content)
     6         else:# 如果没有content参数,则进行读
     7             return f.read()
     8 myfile('hh','dhjksdefe')# 会将dhjksdefe写入文件
     9 a = myfile('hh')# 实参  hh指的是一个文件名
    10 print(a)# 会读取文件内容,并输出显示

     (4)

     1 # import json
     2 #
     3 # def json_file(name,d=None):
     4 #     pass
     5 #
     6 # print(json_file('tt'))
     7 # 运行结果:None
     8 
     9 def say():
    10     pass
    11 print(say())
    12 # 运行结果:None

     (5)将校验密码的代码写成函数,如下

    def check_password(password):#校验密码是否合格,必传参数,位置参数
        password_set = set(password)
        if password_set & set(string.digits) and password_set & set(string.ascii_lowercase) 
                and password_set & set(string.ascii_uppercase):
            print('合法')
            return True
        else:
            print('不合法')
            return False

     (6)把文件操作,用函数实现

    # with open('f','w',encoding='utf-8') as fw:
    #     fw.write(xxx)
    
    def write_file(file_name,content):
        with open(file_name,'w',encoding='utf-8') as fw:
            fw.write(content)
    
    def read_file(file_name):
        with open(file_name,encoding='utf-8') as fr:
            result = fr.read()
            return result
    # write_file(content='1234',file_name='a.txt')
    # write_file('a.txt',content='absdfs')
    # write_file('a.txt','12235236')
  • 相关阅读:
    paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之output encoded style with registered outputs(Good style)
    软测(一)
    package.json
    邬江兴:网络安全“再平衡战略”抓手--拟态防御
    什么是DDOS攻击?怎么防御?
    什么是gitlab CI ?CI代表什么?
    结构体字节对齐(转)
    MySQL 及 SQL 注入与防范方法
    HDU 4704 Sum (费马小定理)
    HDU 4704 Sum (费马小定理)
  • 原文地址:https://www.cnblogs.com/Noul/p/9175432.html
Copyright © 2011-2022 走看看