1. The functions and modules using
1) Defined Functions
You can use `def` keyword to define a function in Python
It can be passed after the function name is placed in parentheses to the parameters of the function
And after the function execution is complete we can return a value by `return` keyword
------ adjusted reconstruction of the code structure of the code in the code without affecting the result of execution
Function Structure
DEF defineName ([params]): ...........
# executable ........... return ..... call: defineName ([params])
Python function expansion
# In front of the parameter name is args * indicates a variable parameter
# that is 0 or more can pass parameters when calling the function add
Function uses the tips:
1. First will write the bare code and need to see where repeating
2. Next will need to reuse the code into parameter, brought into the function.
Function uses the classic example
Example 1: order form (required by the conditions, see if there are goods, then fill orders, shipments)
Enter the information required to verify the legal
DEF Address (): Print ( ' Please fill in the information ' ) Print ( ' Please enter your name: ' ) name = the INPUT ( ' the Name ' ) Print ( ' Please enter the phone: ' ) Phone = the INPUT ( ' Phone ' ) Print ( ' enter address: ' ) Addr = iNPUT ( ' Addr ' ) RES =check_Information (name, Phone, Addr) IF RES: Note () return False DEF Note (): Print ( ' submit orders, shipping immediately ' ) DEF Start (): Print ( ' Welcome ' ) G = the INPUT ( ' goods : ' ) Check (G) the Start ()
Example 2 Examples of user registration (verification code)
global_connt = 0 def User(): user_ = input('User:>>') Z ='QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm' N ='1234567890' T ='.*&^%$#@!~' is_Z = False is_N = False is_T = True for i in user_: if i in Z: is_Z =True IF I in N: is_N = true IF I in T: is_T = true IF is_Z and is_N and is_T: Password () the else : Print ( ' Account must also not be alphanumeric and there (# $% ^ & *. ! @ ~) ' ) DEF password (): the Passwd = iNPUT ( ' enter password: >> ' ) IF len (the Passwd) <. 6 : Print ( 'The password must be greater than 6 ' ) the else : Phone () DEF Phone (): Phone = INPUT ( ' Enter phone number ' ) IF len (Phone) =. 11! : Print ( ' Invalid phone number ' ) the else : Print ( ' code sent ' ) Verfily_number () DEF Verfily_number (): # declared as a global variable global global_connt Import Random Import Time NUMRandom.randrange = (1000,9999 ) global_connt + =. 1 START_TIME = the time.time () Print ( ' system codes to verification code is: D% ' % NUM) num_ = int (INPUT ( ' Enter Code: ' )) end_time = time.time () SUB_TIME = end_time- start_time IF SUB_TIME> 10 : IF global_connt> 2 : Print ( ' you might be a robot ' ) Exit () Print ( 'Code timeout, retransmission is about to ... ' ) the time.sleep ( 2 ) Verfily_number () the else : IF NUM == num_: Print ( ' verification success ' ) the else : Print ( ' Code error ' ) the time.sleep ( 2 ) Verfily_number () DEF Start (): Print ( ' Please register personal information ' ) the User () Start ()
2. Character and character structure
DEF main (): str1 = ' ! Hello, World ' # calculation function of the length len of the string by the Print (len (str1)) # 13 is # obtain capitalized string copy Print (str1.capitalize ()) # the Hello , World! # after obtaining copies of the string becomes uppercase Print (str1.upper ()) # HELLO, WORLD! # locate a substring from a string location Print (str1.find ( ' or ' )) # . 8 Print ( str1.find ( ' shit ' )) # -1 #And find similar, but throws an exception if can not find the substring # print (str1.index ( 'or')) # print (str1.index ( 'shit')) # check whether the string starts with the specified string of Print ( str1.startswith ( ' of He ' )) # False Print (str1.startswith ( ' HEL ' )) # True # checks whether the string ends with the specified string Print (str1.endswith ( ' ! ' )) # True # will centered string specified width on both sides and to fill the specified character Print (str1.center (50, ' * ' )) # string is placed in a specified width to the right side of the filler specified character Print (str1.rjust (50, ' ' )) str2 = ' abc123456 ' # character string extracted from the specified position (subscripting) Print (str2 [2]) # C # string sections (from a specified start to the specified index end index) Print (str2 [2:. 5]) # C12 Print (str2 [2:]) # c123456 Print (str2 [2 :: 2]) # C246 Print (str2 [:: 2]) # ac246 Print (str2 [:: -. 1]) # 654321cba Print (str2 [-3: -1]) # 45 # checks for string comprised of a digital Print (str2.isdigit ()) # False # Check whether the letter string constituting Print (str2.isalpha ()) # False # checks a string of numbers and letters to constitute Print (str2.isalnum ()) # True Str3 = ' [email protected] ' Print (Str3) # obtain a copy of the space around a string trimmer sides Print (str3.strip ()) IF the __name__ == ' __main__ ' : main ()