The next day the python learning Notes

9,015
characters
# .format () and unknown parameters acceptable keyword parameters, e.g. 'Love {{0}}. 1'. The format ( 'the I', 'by You')
'by You Love the I'
'Love {A} { } B '. the format (a =' the I ', B =' by You ')
' the I by You Love '
10,016
sequences
#list () can be converted into a string or a list of tuples, e.g. a =' I Love You '
A = List (A)
A
[' the I ',' ',' L ',' O ',' V ',' E ',' ',' the Y ',' O ',' U ']
B = ( 1,2,3,4)
B = List (B)
B
[l, 2,3,. 4]
#tuple () can be converted into a string or a list of tuples, supra
#str () can be converted to a string
#len () can check the length
#max () can return a sequence parameter set or a maximum value
# min () can return a sequence parameter set or a minimum
#sum () or may be calculated and a sequence parameter set of all values, only the same type may be added before, for example SUM (B)
10
#sorted () sequence can be the set of values or parameters of all sorts
#enumerate () may be the position of a sequence parameter or all of the elements and print elements, for example: list (enumerate (b))
[(0,. 1), (. 1, 2), (2,. 3), (. 3,. 4)]
#zip () packaged two pairlist

11,017
function (the params)
function is defined, for example: DEF MyFirstFunction ():
Print ( 'the I by You Love')
calling a function, for example: MyFirstFunction ()
the I by You Love
arguments to the call, for example: DEF MySecondFunction (name):
Print (name + 'fool')
MySecondFunction ( 'millet')
millet is a fool
12,018
# ._ doc_ View function function documentation, for example: DEF MyFirstFunction ():
'this does not display the'
Print ( 'the I Love by You')
_ _doc_ MyFirstFunction._
'does not show this'
key parameter allows the input arguments to find a more accurate parameter, for example: DEF Music (name, time):
Print (name + "when the length' + time)
Music (time = '3: 12', name = ' that good cry')
said length cry time 3:12 good
collection parameters
plus a * represents a plurality of parameters before the parameter, for example: DEF Test (* the params):
Print ( 'the parameter the maximum value is: ', max (the params));
Print ('The minimum value is: ', min (params))
test (10,1,2,7,9)
the maximum value of this parameter is: 10
Min: 1
13,019
local function can only be accessed in function of its use in any global variables in the whole code location can access, but global variables can not be modified in function.

14,020
#global keywords, you can make use of global variables become local global variables.
Inline functions, internal python allow nested functions, for example: DEF fun1 ():
Print ( 'fun1 function is called a')
DEF fun2 ():
Print ( 'fun2 function is called a')
fun2 ()
fun1 ()
function is called fun1 the
function is invoked fun2
closure, there is a variable in the function calling an external function Lane, this function is called closure function, for example: DEF funX (X):
DEF funy (Y ):
return X * Y
return funy
funX (. 8) (. 5)
40
#nonlocal closures may function in the global variables into local variables, for example: DEF FUN3 ():
X =. 5
DEF FUN4 ():
nonlocal X
X = X *
return X
return FUN4 ()
FUN3 ()
25
15,021
anonymous function (the lambda)
the lambda is defined: the lambda X: X + 2. 1 *
G = the lambda X: 2. 1 * X +
G (. 3)
. 7
#filter () filter elements can be, for example: List (filter (the lambda X: X 2%, Range (10)))
[. 1,. 3,. 5,. 7,. 9]
# map () print shot, may be later elements into a former iteration function, for example: List (Map (the lambda X: X * 2, Range (10)))
[0, 2,. 4,. 6,. 8, 10, 12 is, 14, 16, 18 is]
16,022
recursive
DEF factorial (n-):
IF n-==. 1:
return. 1
the else:
return n-* (factorial (n--. 1))
Number = int (iNPUT ( 'enter a positive integer:'))
Result = factorial (Number)
Print ( "% D is hierarchical:% d"% (number, result))
run results: 120 class 5

17,023
recursive Examples:
DEF Fab (n-):
IF n-<. 1:
Print ( 'input error')
return -1

if n==1 or n==2:
return 1
else:
return fab(n - 1) + fab(n - 2)
resut = fab (20)
if result !=-1
print('总共有%d只!'% result)

Guess you like

Origin www.cnblogs.com/dcpb/p/11544576.html