Day 6 python入門から実践へ-関数

32353 ワード

  • 定義関数
  • #    
    def greet_user():
        """       """
        print("Hello")
    greet_user()
    
    #       
    def greet_user(user_name):
        """       """
        print("Hello "+user_name.title()+" !")
    greet_user("Li lei")
    
  • 形参加実パラメータ-上記の関数定義で使用されるuser_nameはパラメータであり、関数呼び出しで使用する「Li lei」は実パラメータ
  • である.
    #    ——                 
    def describe_pet(animal_type,pet_name):
        """      """
        print("
    I have a "
    +animal_type.title()) print("My "+animal_type.title()+"'s name is "+pet_name.title()+".") describe_pet("hamster","harry") # —— def describe_pet(animal_type,pet_name): """ """ print("
    I have a "
    +animal_type.title()) print("My "+animal_type.title()+"'s name is "+pet_name.title()+".") describe_pet(animal_type="hamster",pet_name="harry") describe_pet(pet_name="harry",animal_type="hamster") # —— , def describe_pet(pet_name,animal_type = "dog"): """ """ print("
    I have a "
    +animal_type.title()) print("My "+animal_type.title()+"'s name is "+pet_name.title()+".") describe_pet(pet_name="willie") describe_pet("willie") describe_pet(pet_name = "lily", animal_type = "cat") describe_pet("lily","cat") # —— #practice def make_shirt(size,word): print("
    The shirt's size is "
    +str(size)+".") print("And the word is "+word.title()+".") make_shirt(75,"love") make_shirt(size=75,word="love") # def make_shirt(size="medium",word="I love Python"): print("
    The shirt's size is "
    +size.title()+".") print("And the word is "+word.title()+".") make_shirt() # —— # def get_formatted_name(first_name,last_name): """ """ full_name = first_name+" "+last_name return full_name.title() musician = get_formatted_name("jimi","hendrix") print(musician) # 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_name return full_name.title() musician = get_formatted_name("john","hoker","lee") print(musician) # def build_person(first_name,last_name,age=""): """ , """ person = {"first": first_name,"last": last_name} if age: person["age"] = age return person musician = build_person("jimi","hendrix",age = 27) print(musician) # while def get_formatted_name(first_name,last_name): """ """ full_name = first_name+" "+last_name return full_name.title() while True: print("
    Please tell me your name: "
    ) print("(enter 'q' at any time to quit !)") f_name = input("First name : ") if f_name == "q": break l_name = input("Last name : ") if l_name == "q": break formatted_name = get_formatted_name(f_name,l_name) print(formatted_name) #practice def city_country(city_name,country_name): print(city_name+", "+country_name) city_country("A","B") # def greet_users(names): """ """ for name in names: print("Hello "+name.title()+" !") usernames = ["hannah","ty","margot"] greet_users(usernames) # def print_models(unprinted_designs, completed_modles): """ , , completed_modles """ while unprinted_designs : current_design = unprinted_designs.pop() print("Print models: "+current_design.title()) completed_models.append(current_design) def show_completed_models(completed_models): """ """ print("
    The following models has been finished:"
    ) for completed_model in completed_models: print(completed_model.title()) unprinted_designs = ["iphone case","robot pendant", "dodecahedron"] completed_models = [] print_models(unprinted_designs,completed_models) show_completed_models(completed_models)# # print_models(unprinted_designs[:],completed_models) #practice def show_magicians(magicians): for magician in magicians: print("
    "
    +magician.title()) # def make_great(magicians): # for magician in magicians: # change_magician = magician+" the Great" # magicians[magicians.index(magician)] = change_magician # magicians = ["eric","bruce","tom"] # make_great(magicians) # print(magicians) # -*toppings toppings def make_pizza(*toppings): """ """ print("
    The following toppings :"
    ) for topping in toppings: print("- "+topping) make_pizza("mushrooms","green pepers","extra cheese") # , def make_pizza(size,*toppings): """ """ print("
    The following toppings :"
    ) for topping in toppings: print("- "+topping+"- size: "+str(size)) make_pizza(16,"mushrooms","green pepers","extra cheese") # def build_profile(first,last,**user_info): """ , """ profile = {} profile["first_name"] = first profile["last_name"] = last for key,value in user_info.items(): profile[key] = value return profile user_profile = build_profile("albert","einstein", location = "princeton", filed = "physics") print(user_profile)
  • インポートモジュールと関数
  • # coding : utf-8
    #      
    #import module_name
    import pizza
    pizza.make_pizza(16,"pepperoni")
    
    #      
    #from module_name import fuction_0, function_1, function_2
    from pizza import make_pizza
    make_pizza(16,"pepperoni")
    
    #  as       
    #from module_name import function_name as fn
    from pizza import make_pizza as mp  
    
    #  as       
    #import module_name as mn
    import pizza as p
    
    #         
    #from pizza import *