函数的学习

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

#想要打印结果
passd=check_password('Abd123')
print(passd)

如果定义一个写文件的函数,只需要调用函数就帮忙写文件
#需要几个参数,需不需要返回值 写时候要考虑这个是写文件参数
import string
def write_file(file_name,content):#考虑文件名字和内容
with open(file_name,'w',encoding='utf-8') as fw:
fw.write(content)
函数只支持这三种传参方式
# write_file(content='1234',file_name='a.txt')
# write_file('a.txt',content='absdfs')
# write_file('a.txt','12235236')
 
默认值传参形式
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
#默认值参数
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('info.json') )#没有传返回none 读
op_file('1.txt','sdfdssgsdgsdg') #有传写文件


判断小数的方法 使用函数学习
思路


 

def is_float(s):
s = str(s)
if s.count('.') == 1: #1.3
left,right = s.split('.')
if left.isdigit() and right.isdigit():#正小数
return True
#左边的-1
# if left.startswith('-') and left.lstrip('-').isdigit() and right.isdigit():
if left[0]=='-' and left[1:].isdigit() and right.isdigit():
return True
print(is_float('-1.21'))
print(is_float('1.2s'))
print(is_float('s.3'))
print(is_float('--1.3'))
print(is_float( .3 ))#0.3
print(is_float( '.3' ))#0.3
#函数里面遇到return,函数立即结束
函数的返回值 

def test():
print('hello')
def test2():
return 1, 2, 3
# 如果一个函数没有写返回值的话,返回的就是None
# 如果函数有多个返回值,那么返回的是一个元组
ces=test()
print(ces)
ces1=test2()
print(ces1)
既然是元组就可以拆包 a,b,c = test2() List,元组,字符串都可以拆包
可以判断是合法小数 return FALSE返回none也正常
局部变量和全局变量 
#全局变量:一般定义在代码的最上面,大家都可以用的

#局部变量:在函数里面定义的变量,都是局部变量
file_name = 'a.txt'
country = 'China'
#list、dict、set不需要用global来声明了
#str、int、float、tuple、bool#需要的

file_name = 'a.txt'
country = 'China'
#list、dict、set不需要用global来声明了
#str、int、float、tuple、bool#需要的

def say():
print(file_name)
word = 'Nihao'
print(country)
print(word)

def zhucheng():
country = 'Japan'
print(country)

def update_file_name(): #修改全局变量操作
global file_name
file_name = 'a.json'
print(file_name)
update_file_name()
print(file_name)

函数参数
上面学了必传参数和默认值参数
下面说#可选参数(参数组)、关键字参数
import json
def send(*args): #这种函数传参优点不限制参数个数 变量名字写啥都行习惯性写args
for p in args:
print('发短信给xxx%s'%p)

# send()
# send(110)
# send(110,120,119)

#关键字参数,它不是必传的,不限制参数个数,它是把参数放到了一个字典里面,
# 但是它传参的时候必须得用关键字的方式
def send_sms(**kwargs):
print(kwargs)
 
send_sms()
send_sms(xzh='晚上好')
send_sms(lhh='新年好',fd='生日快乐',lyh='新婚幸福')

 

def nb_func(name,age,country='China',sex='女',*args,**kwargs):   参数组合顺序 
#1、必填参数
#2、默认值参数
#3、参数组
#4、关键字参数
print(name)
print(age)
print(sex)
print(country)
print(args)
print(kwargs)
nb_func('xh',18,'abc','efg','hhh',name=1,b=2,c=3)
nb_func('xh',18,'japan','nan','abc','efg','hhh','2335','23532')

 

匿名函数
只能定义简单的功能
冒号前面入参 后面是方法
def write_file():#lambda是定义匿名函数的
pass
a = lambda x,y:x+y
result = a(1,2)
print(result)
filter(lambda x:str(x),[1,2,3,4,5])
filter(str,[1,2,3,4,5]) #两种效果一样
内置函数  就是python里面自带一些函数 # list dict tuple str int float set  这些都是
 
原文地址:https://www.cnblogs.com/weilemeizi/p/14505163.html