python based learning - a function of the job

1, include a Boolean value of False

  空、0、False、[]、{}、()、None

2, 3 and is acquired according to a range in which all the numbers 7 and divisible, and returned to the caller: Eligible Eligible digits and digital integrated, such as: def func (start, end)

def func(start,end):
    count=0
    sumn=0
    for i in range(start,end):
        if i%3==0 and i%7==0:
            count +=1
            sumn  +=i
    return count,sumn
t1=func(1,43)
print(t1)

3, the return value is the default None

4, the difference briefly break / continue / return of

  end of the cycle of the current layer break

  The end of this cycle continue into the next cycle

  return end of the function, and returns the result, the default is None

5, the ternary operator briefly illustrated format and writing

  A variable = value else if condition a value of two

6, brief writing format lambda expressions and examples

  Anonymous function: function name = lambda parameter: function, function names can not write, save memory space, memory completely random calls to be released

  eg:lambdax :x=x-1

7, with the set collection obtain two lists L1 = [11,22,33], the same set of elements L2 = [22,33,44] in

L1=[11,22,33]
L2=[22,33,55]
L3=set(L1)&set(L2)
print(L3)

8, location parameters outlined function, keyword parameters, default parameters, characteristics of the variable-length parameters and considerations

  Location parameters: parameter according to the position of incorporation, must be shaped one-argument participation

  Keyword arguments: without a fixed location

  Default parameters: Parameter Default parameter values ​​specified directly

  Variable-length parameters: * list of related, ** related dictionary

9, read the results

def func(*y,**z):
    print(y,z)
func(*[1,22,33],{"name":"cc","age":18})
#结果:(1, 22, 33, {'age': 18, 'name': 'cc'}) {}
#方式二:
def func(*y,**z):
    print(y,z)
func(*[1,22,33],**{"name":"cc","age":18})
#结果:(1, 22, 33) {'name': 'cc', 'age': 18}
DEF FUNC (X, * z, ** y): # a * z ** y to the front 
    Print (X, Y, Z) 
FUNC ( l, 2,3 ) 

# Results: 1} {(2, 3 )
def func(x,*y,**z):
    print(x,y,z)
func(1,name=2,age=3)
#结果:1 () {'name': 2, 'age': 3}
DEF func1 (X =. 1, y *, ** Z):
     Print (X, y, Z) # . 3:. 1, y null (), { "name": "Fuck] 
    return y     # . 4: Returns the empty list y 
    Print (X)     # this function has returned, return the following contents is not performed 
DEF func2 (Arg): 
    RET = func1 (name = Arg) # 2: execute func1 
    Print (RET)      # . 5, execution func1 returns after 4 empty list, so ret empty tuple 
result = func2 ( " Fuck " ) # Step: execute function 
Print (result)      # . 6 func2 return no return, the execution result is null fun2 None 

# results: 
1 () { ' name ': 'Fuck'}
()
None
DEF FUNC (Arg): 
    arg.append ( 55 ) 
li = [11,22,33,44 ] 
FUNC (li) 
Print (li) 
li = FUNC (li) # li has been re-assigned, so is None 
Print (li ) 

# results: [. 11, 22 is, 33 is, 44 is, 55] 
None
DEF func (A1):
     return A1 + 100 
func = the lambda A1: A1 + 200 is   # func (A1) and the same function func. A front cover after a func func (A1) 
RET = func (10 )
 Print (RET)
 # Results 210

 9, a nested function calls another function

= name " CC " 
DEF Outer (func): 
    name = " Nana " 
    func () # result is a show performed, show and func calls is the same memory 
DEF show ():
     Print (name) 
Outer (show)

10, 1-8 recursive factorial

# 1-8 factorial 
DEF F (n-):
     IF n-==. 1 :
         return . 1
     return n-* F (. 1-n-)   # . 8 * F (. 7). 7 * F (. 6) .... 2 * F ( 1) 1, and then recursion starts up from a 
RET = F (. 8 )
 Print (RET)

11, write program implemented using withs simultaneously open two files (a read-write, read and write the contents of the file to write mode)

# Written procedures implemented using withs simultaneously open two files (a read-write, read and write the contents of the file to write mode) 

with Open ( " A " , " R & lt " ) AS X, Open ( " B " , " W " ) AS Y: 
    y.write (x.read ())

 

Guess you like

Origin www.cnblogs.com/xucuiqing/p/11762033.html