9,函数的初识

03,函数的初识
def关键字 空格 函数名(与变量设置相同): 英文的冒号
函数体
执行函数 :函数名+()
函数是以功能为导向的,

什么是None:他是一个独立的数据类型, ‘’ () {} set() [] 内存中都指向None

三元运算:def man(x,y): return x if x > y else y

return : 1函数中遇到return 结束函数,下面代码不执行
2将函数里面的值返回给函数的执行者(调用者)。
第一种情况:
只有return,后面什么都不写,则返回none
第二种情况:
return None
第三种情况:
return 单个值(该值,返回的是什么数据类型,就是什么数据类型。)
第四种情况:
return 返回多个值(以元组的形式,返回给函数调用者。)

count = 1

def func4():
    count = count + 1
    print(count)

func4()
# 局部只能引用全局的变量,不能修改,如果要修改,global。

def func4():

    count = 3
    def inner():
        nonlocal count
        count = count + 1
        print(count)
func4()
# 子函数只能引用父函数的变量,不能修改,如果要修改,nonlocal。


函数的传参


什么是实参
位置参数:一一对应对,缺一不可。
关键字传参:一一对应,形参和实参的数量必须一致。实参顺序可变。
混合传参:位置参数必须在前面,否则报错。(关键字参数必须在位置参数后面)


什么是形参
位置参数:一一对应,缺一不可
默认参数:1,必须放在形参的位置参数后面, 2默认参数不传值,则为默认值,传值则覆盖。

计算字符串的长度;

s1 = 'sdafasfasfsdfsadf'
count =  0
for i in s1:
    count += 1
print(count)

li = [1,23,34,5,56,6,7,8,32,42,23,45,12,323,23,234,2134]
count = 0
for i in li:
count += 1
print(count)

def my_len(a):
count = 0
for i in a:
count += 1
return count
li = [1, 23, 34, 5, 56, 6, 7, 8, 32, 42, 23, 45, 12, 323, 23, 234, 2134]
print(my_len(li))

return 的四种返回:

print(my_len(li))
# 没有意义
# def login():
#     print(111)
#     print(222)
#     return None
#     print(333)
# login()

# 只有return,后面什么都不写,则返回none # def login(): # return # print(login())

#return 单个值(该值,返回的是什么数据类型,就是什么数据类型。) # def login(): # a = 2 # b = 3 # return [1,2] # a1 = login() # a,b = a1 # print(type(a),b)

#return 返回多个值(以元组的形式,返回给函数调用者。) # def login(): # a = 2 # b = 3 # return 1,'alex',[1,2],{'name':'老男孩'} # print(login())

函数的传参

#内存中从上到下依次执行,执行my_len()加入到内存,从上往下走,li加入到内存,通过实参传参my_len()传参
# ,形参a接受参数从上往下依次执行,齿形count = 0 ,然后 for iin a: count += 1; return count

def my_len(a):
    count = 0
    for i in a:
        count += 1
    return count
li = [1,2,3,4,5,6,7,8,9]
print(my_len(li))

三元运算

#三元运算
def sds(a,b): return a if a > b else b
print(sds(3000,2000))

关键字传参

def func(y,x):
    return type(x)
print(func(x=333,y=222))

实参角度

什么是实参
位置参数:一一对应对,缺一不可。
关键字传参:一一对应,形参和实参的数量必须一致。实参顺序可变。
混合传参:位置参数必须在前面,否则报错。(关键字参数必须在位置参数后面)

什么是形参
位置参数:一一对应,缺一不可
默认参数:1,必须放在形参的位置参数后面, 2默认参数不传值,则为默认值,传值则覆盖。

# 默认传参,位置参数必须在前面,否则各种报错。
def func(x,y,z=100):
    print(y,x,z)
func(1,2)

增加员工信息内容

def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
def input_information(name, sex=''):
    with open('information',encoding='utf-8', mode='a') as f1:
        f1.write('{}	{}
'.format(name, sex))

while True:
    msg = input('请输入用户的姓名,性别Q或者q退出').strip()
    if msg.upper() == 'Q':break
    if ',' in msg:
        name1, sex1 = msg.split(',')
        input_information(name1, sex1)
    else:
        input_information(msg)
原文地址:https://www.cnblogs.com/ZJGG/p/9015678.html