first, the function
1. It is straightforward to put together a set of code, you can implement a function, you need to use this function, call this function directly.
2. Function, method is one thing
3. The format for defining a function is: def+ function name + ()
4. The function must be called before it can be executed
eg
My_open
FW = open (' A.txt ', ' A + ')
Fw.seek (0)
All_users = {} # is used to store all user names and passwords
def read_users (file_name): #括号里面是形式参数 is actually a variable
#函数体
Print (file_name)
With open (file_name) as fr:
For line in FR:
up = Line.strip (). Split (', ') # segregated account password
# print (' account password after separation ', up)
S_username = up[0] # Account
S_pwd = up[1] # password
All_users[s_username] = S_pwd
Print (' Call function before: ', all_users)
Read_users (' a.txt ') #调用函数传进去的参数叫做实际参数, referred to as argument
#函数名加上括号就是调用, the function needs to be called before it can execute
Print (' After calling function: ', all_users)
Simple Write
def hello (name):
#必填参数, positional parameters
Print (' Hello%s '%name)
Hello (' Zhengshuang ')
EG1:
DEF reg (name,age,sex= ' male '):
Age, name required parameter, positional parameter
Sex is the default value parameter, not a mandatory
Print (' Hi%s,age is%s,sex%s '% (name,age,sex))
Reg (' Little Dee ', ' Nan ')
Ggp
5.*args# Variable parameters
eg
Def post (*args):
A variable parameter, also called a parameter group, is not required, and it receives a tuple
It puts every parameter that goes in when the function is called into a single tuple.
Print (args)
Post (' 001 ', ' Denglu ', ' http://www.baiudd.com ', ' post ', ' a=1 ')
Post (' 001 ', ' Denglu ')
Post (' 001 ', ' Denglu ', ' post ')
Post (' 001 ')
Post ()
def other (name,age,country= ' China ', *args):
Print (name)
Print (age)
Print (country)
Print (args)
Other (' wubing ', ' 999 ')
eg
def other (name,age,country= ' China ', *args):
Print (name)
Print (age)
Print (country)
Print (args)
Other (")
6.**kwargs, keyword parameter, receive a dictionary, call to use xx=11
Incoming dictionary calls must be written **{' age ': ' Name ': ' Xiaodi '}
D = {' Age ': *, ' name ': ' Xiaodi '}
def kw (**kwargs):
Print (Kwargs)
KW ()
eg
def other2 (name,country= ' China ', *args,**kwargs):
#如果必填参数, default, variable, and keyword parameters if you want to use them together, you must follow the order of required parameters, default values, variable parameters, and keyword parameters, or you will get an error.
Print (name)
Print (country)
Print (args)
Print (Kwargs)
Other2 (' wubing ')
Other2 (' Xiaodi ', ' Beijing ', ' python ', ' yin ', user = ' Ywq ')
7. keyword parameter invocation
eg
def write (Filename,model,ending,user,os,money,other):
Print (filename)
Print (model)
Print (ending)
Print (user)
Print (OS)
Print (Money)
Print (Other)
Write (os= ' windows ', user= ' wubing ', model= ' W ', filename= ' a.txt ', ending= ' utf-8 ', money=222,other= ' hahah ')
8. return value of function
Def plus (A, b):
return function
1. Call the return and end the function immediately.
2. After the function is called, the result of the calculation is returned
3. Function can have no return value, if no return value, default return None
4. If the result of this function needs to be used somewhere else, then you need to return the value to the function.
5. If the function return multiple worthy words, then he will put a number of values in a meta-ancestor
c = a+b
Return C
Print (c)
Plus (+)
eg
Score1 =
Score2 = ALL
def echo (sum):
Print (' Total score is%s '%sum) '
res = plus (score1,score2)
Print (res)
echo (res)
lis = [5,2,6,44,77]
Print (lis.sort)
9. Applet
#1, judging decimals
# 1.92
#-1.988
def is_float (s):
‘‘‘
This function is used to determine whether a decimal is passed in, including positive and negative decimals.
:p Aram S: passing in a string
: Return:true or False
‘‘‘
s = str (s)
If S.count ('. ') ==1: #判断小数点个数
SL = S.split ('. ') #按照小数点进行分割
left = Sl[0] #小数点前面的
right = Sl[1] #小数点后面的
If Left.startswith ('-') and Left.count ('-') ==1 and Right.isdigit ():
Lleft = Left.split ('-') [1] #按照-split, then take the number after the minus sign
If Lleft.isdigit ():
Return True
Else
Return False
Elif Left.isdigit () and Right.isdigit ():
#判断是否为正小数
Return True
Else
Return False
10. Global variables and local variables
# score3 = [1,2,3,4,5]
# score3 = {"id": 1}
Score3 = 100
Def my_open ():
#在函数里面定义变量叫局部变量, it can only be used inside a function
#出了该函数外, you can't use it.
#在函数外面定义的变量, is a global variable and can be used within a function
#如果想在函数里面修改全局变量的值, first declare it with the global keyword
#要修改全局变量是int, String, you have to write global.
#如果是字典和list的话, if you want to change it, you can't add global
FW = open (' A.txt ', ' A + ')
Fw.seek (0)
Print (' Score3 ', score3)
d={' ID ': 2}
d[' Price ']=99
def hh ():
Print (' Modify Item ... ')
def cc ():
Print (' Add Item ... ')
def query ():
Print (' Search products ... ‘)
menu= {
' 1 ': hh,
' 2 ': CC,
' 3 ': Query
}
Chioce = input (' Input number ')
MENU[CHIOCE] ()
Iv. functions of Python