python Lesson Eleven

# 2 write function to check an overview of all odd bit prime primer corresponding elements into lists or tuples Wei object, and returns it to the caller as a new list. 
# DEF FUNC (L):
# return L [2. 1 :: ] slice #
# Print (FUNC ([1,2,3,4,5]))
#. 3. write function, the user determines the value passed (strings, lists, tuples) is greater than the length. 5
# DEF FUNC (X ):
# return len (X)> 5
# IF FUNC ( 'ABCD'):
# Print ( 'length is strictly greater than 5')
# 4, the write function, checks the length of the incoming list, if more than 2, then the first retaining only two lengths content, the new content and returns to the caller.
# DEF FUNC (L):
# IF len (L)> 2:
# return L [: 2]
# P = FUNC ([l, 2,3, . 4])
# Print (P)
#. 5, the write function, calculate the incoming string [numbers], [letter], [space] and [other] number, and return the result
# def func (s): # 'skghfoiw
# # # NUM = 0 calculates the digital number
# # alpha = 0 # letter
# # space = 0 # spaces
# # other = 0 # other
# dic = {' num ': 0,' alpha ': 0, 'space': 0, 'other ':0}
For I in S #:
# i.isdigit IF ():
# DIC [ 'NUM'] +. 1 =
# elif i.isalpha ():
# DIC [ 'Alpha'] +. 1 =
# elif i.isspace ():
DIC # [ 'Space'] +. 1 =
# the else:
# DIC [ 'OTHER'] +. 1 =
# return DIC
# Print (FUNC ( '+ 0-0sfk3j5l8s; f7j; aslfks'))

# write function. 6, check. users incoming objects (strings, lists, tuples) of each element if it contains empty content, and returns the result
# DEF FUNC (the X-):
# iF of the type (the X-) iS str and the X-:
# for i in the X-:
# == I IF '':
# return Ture
# elif and X type (X) IS List or type (X) IS tuple:
# for I in X:
# Not I IF:
# return True
Elif not the X-#:
# return True
# Print (FUNC ([]))

# 7, write function that checks each incoming value of the length of the dictionary, if greater than 2, then retain only the first two content length, and The new content is returned to the caller
# DEF FUNC (dic):
# for k in dic:
# IF len (dic [k])> 2:
# dic [k] = dic [k] [: 2]
# return dic
# dic {= "K1": "v1v1", "K2": [11,22,33,44]}
# Print (FUNC (DIC))

# write function. 8, two digital reception parameters, that return large numbers.
DEF FUNC # (A, B):
# IF A> B:
# A return
# the else:
# return B
# Print (FUNC (l, 5))
# DEF FUNC (A, B):
# A return IF A> B B the else
# Print (FUNC (5,1))
# ternary operator
#. 1 = A
# = B. 5
# IF A = C A> B the else ternary operator B #
# print (c)
# = Variable condition returns True if the result of the conditions else the condition returns Falese results
# must have the results
# must have if and else
# can only be a simple case of

# 9. Write function, the user of incoming modify the file name, and to modify the contents of the batch function is executed to complete the operation of modifying the file
DEF FUNC (filename, Old, new new):
with Open (filename, encoding = 'UTF-. 8') AS F, Open ( 's.bak%'% filename, 'W', encoding = 'UTF-. 8') AS F2:
for in Line F:
IF in Old Line:
Line = line.replace (Old, new new)
f2.write (Line)
Import OS
The os.remove (filename )
os.rename ( '% s.bak'% filename, filename)

Process # Decorative formed of: simple decoration has a parameter that returns a value universal parameter
# decorator role
# principles: Principle closed prescribing
fixed pattern # decorator
Import Time
# Print (the time.time ()) # get the current time
# time.sleep (10) # make the program execution to stop for a while in this position when the
# print ( 'Gong Qili')
#
# DEF FUNC (): # was decorated function
# the time.sleep (0.01)
# print ( 'Kai Li I love you')
#
# DEF Timmer (f): # decorator function
# DEF Inner ():
# = time.time Start ()
# f () function decorator #
# end = time.time ()
# Print (End - Start)
# return Inner
# @timmer # syntactic sugar
# FUNC = Timmer (FUNC)
# FUNC ()
# decorator role - do not want to modify the function is called, but would like to in the original function before and after adding functionality
#timer is a decorator function, just a function, some decorative

# principle: open closed principle
# open: open to extension
# Closed: modification closed
# sealing plate

decorator function parameters decorative tape #
# DEF Timmer (F):
# Inner DEF (* args, ** kwargs):
# # (1,2) / (. 1)
# = the time.time Start ()
# RET = F (* args, ** kwargs) #f (1,2)
# = the time.time End ()
# Print (Start-End)
# return RET
# return Inner
# @timmer # syntactic sugar @ decorator function name
# def func (a, b) : # decorator function of
# the time.sleep (0.01)
# Print ( 'Kai Li I love you', a, b)
# return 'Kai Li'
# @timmer # syntactic sugar @ decorator function name
# def func1 (a): # was decorated function
# the time.sleep (0.01)
# Print ( 'Kai Li I love you', a, b)
# return 'Kai Lai '
# # = FUNC Timmer (FUNC)
# RET = FUNC (1,2) #inner ()
RET = FUNC # (. 1, B = 2) #inner ()
# Print (SET)


# warpper DEF (F): # decorator function: wrapper decorative name, f is a function to be decorated
# def inner (* args, kwargs **):
# 'before being decorative function to do'
# RET = f (* args, ** kwargs) # garnished with function
## after being decorative function that thing
# return RET
# return Inner
# @wrapper # syntactic sugar @ decorator function name = wrarpper FUNC (FUNC)
# DEF FUNC (a, b): # decorator function of
# the time.sleep (0.01)
# Print ( 'Kai Li I love you' A, B)
# return 'Kai Li'

# DEF warpper ():
# Inner DEF ():
# Pass
# Inner return
#
# DEF warpper (FUNC): #qqxing
# Inner DEF (* args, ** kwargs):
# ret = func (* args, ** kwargs) # function to be decorated
# return ret
# return inner
# @wrapper #qqxing=wrappr(qqxing)
# def qqxing():
# print(123)
#
# ret=qqxing() #inner

Guess you like

Origin www.cnblogs.com/huangjianfeng/p/11297239.html