Guibs Python Learning _ function

Source: Internet
Author: User

Guibs Python Learning _ function

# function # function is a code block with a name, used to complete the specific work # define function Greet_userdef Greet_user (): # function Body print ("Hello") # Call Function Greet_usergreet_user () # Letter Number of message Def greet_user (username): # username is a formal parameter print ("Hello" + username) greet_user (username= ' guibs ') # guibs is an actual parameter GRE Et_user (' Guibs ') # with keyword pass-through argument # can be used without considering the order of arguments passed Def greet_user (username1, username2): Print ("Hello" + username1 + "and" + U sername2) Greet_user (username2= ' guibs ', username1= ' Guibs82 ') # parameter default # call default value if no arguments are passed # parameters with default values must be placed behind Def Greet_user_with _default_name (username1, username2= ' Guibs82 '): Print ("Hello" + username1 + "and" + username2) # Pass parameter passing by position if not enough parameters are passed G        Reet_user_with_default_name (' G ') # return value def get_formatted_name (first_name, last_name, middle_name = ""): If Middle_name: Full_name = first_name + "" + Middle_name + "" + last_name Else:full_name = first_name + "" + Last_nam e return full_name.title () My_name = Get_formatted_name ("Guibs", "G") print ("My name is" + my_name) My_name = Get_formatt Ed_name ("Guibs", ' g ', ' prin ')T ("My name is" + my_name) # returns the dictionary def build_person (first_name, last_name, age= "):" Returns a dictionary containing information about a person ' " ' First ': First_name.title (), ' last ': Last_name.title (), "If age:person[' age '] = age retur  N Personprint (Build_person (' G ', ' ghost ', 22) # delivery List names = [' Guibs ', ' Ghostg ', ' Rio_g ']def greet_users (names): for name In Names:print ("Hello" + name.title ()) greet_users (names=names) # Modify the list in a function # after passing the list to the function, the function can modify it permanently unprinted_desi    GNS = [' iphone case ', ' ipad case ', ' mac case ']printed_designs = []def print_designs (Unprinted_designs, printed_designs):        While unprinted_designs:current_design = Unprinted_designs.pop () print ("Ready to print:" + current_design) Printed_designs.append (current_design) print ("Print Complete:" + current_design) print ("All works Printed") Print_designs (unprinted _designs=unprinted_designs, printed_designs=printed_designs) def show_printed_designs (printed_designs): Print (" Printed work: ") for Printed_design In Printed_designs:print (printed_design) show_printed_designs (printed_designs) # Prohibit function modification list # in the form of slices, copy the list of incoming functions Unprin ted_designs = [' iphone case ', ' ipad case ', ' Mac case ', ' Apple Watch case ']printed_designs = []print_designs (Unprinted_desi gns[:], printed_designs) print (unprinted_designs) # At this point, the original list was not modified # Pass any number of parameters # [*param_name]# at this point, Python encapsulates the parameters to a tuple def print _username (*username): print (username) print_username ("Guibs") print_username ("Guibs", ' Ghostg ') # Use any number of key glyph parameters # [* * Param_names]def set_hobbies (Name, **hobbies): My_hobbies = {} my_hobbies[' name '] = name for key, value in hobbies . Items (): My_hobbies[key] = value return My_hobbiesprint (set_hobbies (name= "Guibs", hobby_1= ' Swift ', hobby_2= ' Pyt Hon ') # Note: In the import, if you do not use the interpreter in the system, but created with your own, then the error # import the function stored in the module # imports the entire module import pizzapizza.make_pizza ("mushrooms", ' extr A cheese ') # Use as to assign an alias to the module import pizza as Pp.make_pizza (' mushrooms ', ' lots of cheese ') # import a specific function # from Module_name impor T Function_name_0, Function_name_1, ... # This syntax can be used without the use of. From Pizza import Make_pizzamake_pizza (+, ' mushrooms ', ' more cheese ') # Use as to assign an alias to a function from pizza Import Make_pizza as Buy_pizzabuy_pizza (, ' mushrooms ', ' a lot of cheese ') # import all functions in module # [*]from Pizza Import *get_price ()

To run the above code, you need to create another pizza.py

def make_pizza (Size, *toppings): "    Overview of the pizza to be made"    print ("Do a size:" + str (size) + ", contains:") for topping in    Toppin GS:        Print ("-" + topping)    print ("pizza") def get_price ():    print ("The Price is 20")


The above is guibs Python learning _ function content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.