The initial function
Get any element of a string before the number are so we
S1 = 'fdskjlgfdgfdjkslgdfjkjafdsajk'
COUNT = 0
for I in S1:
COUNT = +. 1
Print (COUNT)
Get the number of elements in the list until we are so
L1 = [. 1, 2,. 3]
COUNT = 0
for I in L1:
COUNT = +. 1
Print (COUNT)
Such surface during programming:
1. Repeat Code
2. readability is not high
Initial function
L1 = [. 1, 2,. 3]
DEF new_len ():
COUNT = 0
for I in L1:
COUNT = +. 1
Print (COUNT)
new_len ()
Function: function-oriented function is a function of a function package. Change login, registration, file operations .....
function to reduce repetitive code to enhance the readability of the code.
Structure Function
l1 = [1, 2, 3]
new_len DEF ():
COUNT = 0
for I in L1:
COUNT = +. 1
Print (COUNT)
new_len ()
'' '
DEF keyword: definition of a function. Followed by a space.
new_len function name: variable naming consistent. Necessarily have be descriptive.
(): Structure requires, the use of parameter passing.
: Delimiter.
tab key: four spaces. indentation. Function body
'''
Call functions
l1 = [1, 2, 3]
def new_len():
count = 0
for i in l1:
count += 1
print(count)
Execution of the function to write how many times, how many times to perform.
new_len () # function name performers () function. The caller.
new_len () # function name performers () function.
new_len () # function name performers () function.
new_len () # function name performers () function.
new_len () # function name performers () function.
Function's return value
A function is a function package, this function will generally have a final result, for example, you write a login function, the ultimate success is not login you need to return a result? There we are not all used len this function, he is to obtain the total number of elements of an object, will eventually return to a number of elements in this result:
s1 = 'abfdas'
Print (len (s1)) # 6
The return value of the function needs expressed by return:
he has a few features that we expressed in the following codes:
End Function
= L1 [. 1, 2,. 3]
DEF new_len ():
for I in Li:
Print (I)
return
new_len ()
Note: He is a function of not ending the cycle
Function does not return or write only a return, caller function's return value is None
= L1 [. 1, 2,. 3]
DEF new_len ():
COUNT = 0
for I in L1:
COUNT = +. 1
return
Print (new_len ()) # new_len () Returns the value of his None, need to print to appear. Do not print, then just call, I learned in confusion.
Functions return a single value followed by the caller, the function of the resulting value is his own
def func():
print(111)
# return 100
# return [1, 2, 3]
return {'name': '普润特业'}
ret = func()
print(ret, type(ret))
Followed by a plurality of function return values, the caller is a function of tuples.
def func ():
print (111)
return 1, [22, 33]
K = func ()
print (right type (right)) # (1, [22, 33])
Return result of calculation may be returned
def func():
print(111)
# return 1+1+2 -- 4
# return 2 > 1 --Ture
ret = func()
print(ret)
Function parameter passing
We have studied the above, the implementation of the structure, function function, and the function's return value. Have a preliminary understanding of the function, then the next parameter is a very important knowledge point function. Function is a function-oriented, we write the above function inside the code is written dead, that is, the function of which changes a lot of trouble, Imagine, we used to test the waters, unfamiliar street and other software, can you screening, such as selecting gender, age, etc., to export the results? Before we had a chance to learn len len This is not to obtain the total number of string? It is not able to obtain the total number of list? You change the internal len function code? no? You see the following example:
Before:
S1 = 'sfdas'
L1 = [. 1,. 3,. 7]
Print (len (S1)). 5 #
Print (len (L1)). 3 #
Function parameter passing: the expansion of the function
def new_len (a): # When defining the function: Parameters: parameter.
= 0 COUNT
for I in A:
COUNT = +. 1
return COUNT
L1 = [. 1, 2,. 3]
S1 = 'fdsjaklsfjgfds'
Print (new_len (L1)) # caller of the function: The parameter argument.
caller print (new_len (s1)) # Function: parameter argument.
print (len (s1))
Arguments angle
- Location parameters.
- Keyword arguments.
- Mixing parameters.
Positional parameters
Location parameters. : From left to right, in order, one-
DEF Meet (Sex, Age, the Job,):
Print ( 'left draw it')
Print ( 'the right plan it')
Print (f 'Gender {sex}, the age {age} years old, {the Job}')
Print ( 'chat')
Print ( 'about it')
Print ( 'about ....')
Meet ( 'F', '18 ~ 25 ',' lecturer ')
A write function, the two numbers received parameters, the larger the number returned.
CoMP DEF (A, B):
IF A> B:
return A
the else:
return B
RET = CoMP (1,2000)
Print (RET)
Ternary operator: simple if else.
. 1 = A1
B2 = 2
RET IF = A1 A1> B2 B2 the else
Print (RET)
def comp(a,b):
ret = a if a > b else b
return ret
return a if a > b else b
ret = comp(1,2000)
print(ret)
Keyword arguments
Keyword parameter correspondence
DEF Meet (Sex, Age, the Job, Hight, weight,):
Print ( 'left draw it')
Print ( 'the right plan it')
Print (f 'Gender {sex}, the age {age} years old, height {hight }, weight {weight}, working Job} { ')
Print (' chat ')
Print (' about it ')
Print (' about .... ')
meet (sex = 'F', job = 'students', weight = 120, hight = 170, age = '18 ~ 25')
Mixing parameters
Keyword arguments must be in position behind the parameters, - one correspondence
DEF Meet (Sex, Age, the Job, Hight, weight,):
Print ( 'left draw it')
Print ( 'the right plan it')
Print (f 'Gender {sex}, the age {age} years old, height {hight }, weight {weight}, working Job} { ')
Print (' chat ')
Print (' about it ')
Print (' about .... ')
Meet (' M ', 27,' ITC language ', weight = 120, hight = 175,)
Katachisan angle
Positional parameters
As with the angular position parameter argument, from left to right, correspond
DEF Meet (Sex, Age, the Job,):
Print ( 'left draw it')
Print ( 'the right plan it')
Print (f 'Gender {sex}, the age {age} years old, {the Job}')
Print ( 'chat')
Print ( 'about it')
Print ( 'about ....')
Meet ( 'F', '18 ~ 25 ',' lecturer ')
The default parameters
Some parameters in the rear position, i.e. not in use default parameter passing parameters
open ( 'change files', encoding =' UTF-8 ')
DEF Meet (Age, the Job, Sex =' female '):
Print (' left draw it ')
Print (' the right plan it ')
Print (f' gender {sex}, {age} years of age, Job {} ')
Print (' chat ')
Print (' about it ')
Print (' about .... ')
Meet ('18 ~ 25', 'Young division ')
to change the default parameters
meet ('18 ~ 25', 'teachers', Sex =' laddy_boy ')
meet ('18 ~ 25', 'teachers',' laddy_boy ')
Dynamic positional parameters * arg
EAT DEF (food1, FOOD2, food3):
Print (f 'I invite you to eat: food1 {}, {} FOOD2, food3 {}')
EAT ( 'steamed lamb', 'steamed bear's paw', 'steamed Luk Mei')
When the number of arguments passed to the function from time to time, prior to transmission parameters manner not solve the problem.
Universal parameters, dynamic parameters. args *
DEF EAT (food1, FOOD2, food3):
Print (f 'I invite you to eat: food1 {}, {} FOOD2, food3 {}')
EAT ( 'steamed lamb', 'steamed bear's paw', 'steamed Luk Mei '' burning flower duck ',' burn Penguin)
EAT DEF ( args): the angle of the argument: a function is defined when all the position parameters of polymerization to a tuple.
Print (args)
Print (f 'I invite you to eat: {args}')
EAT ( 'steamed lamb', 'steamed bear's paw', 'steamed Luk Mei', 'burning flower duck', 'burn Penguin)
Note: emphasis * rather than args, args are programmers convention
Dynamic keyword arguments ** kwargs
FUNC DEF ( kwargs): defining a function of: the angles of all the arguments into a dictionary keyword polymerization parameters, gave kwargs.
print(kwargs)
func (name = 'alex', age = 84, hobby = 'dancing and singing rap basketball')
* The use of magic
In the function
In the function parameter * or ** (such as * args, ** kwargs) representative of the polymerization
def func (* args, ** kwargs ):
print (args) # (1, 2, 3, 'silly', 'two silly', 'three silly')
print(kwargs)
The final sequence of parameter angle: position parameter, * args, default parameters, keyword parameters only, ** kwargs
In argument function representing break.
L1 = [. 1, 2,. 3]
l2 = [ 'silly', 'two silly', 'three silly']
function (L1, L2)
FUNC ( L1, L2) when performing the function: * iterable behalf break.
FUNC ( [. 1, 2,. 3], (11,22), 'fdsakl') when the execution of the function: Iterable Representative broken.
Outside the function:
Processing remaining elements
a,b,*c = [1,2,3,4,5]
a,*c,b, = [1,2,3,4,5]
a,*c = range(5)
a,*c,b = (1,2,3,4,5,6)
print(a,c,b)