Guibs 的 Python學習_ 函數

來源:互聯網
上載者:User

Guibs 的 Python學習_ 函數

# 函數# 函數是帶有名字的代碼塊, 用於完成具體的工作# 定義函數 greet_userdef greet_user():    # 函數體    print("Hello")# 調用函數 greet_usergreet_user()# 向函數傳遞資訊def greet_user(username): # username 是一個形參    print("Hello " + username)greet_user(username='Guibs') # Guibs 是一個實參greet_user('Guibs')# 帶關鍵字傳遞實參# 可以不用考慮實參傳遞的順序def greet_user(username1, username2):    print("Hello " + username1 + " and " + username2)greet_user(username2='Guibs', username1='Guibs82')# 參數預設值# 若不傳遞參數則調用預設值# 含有預設值的參數必須放在後面def greet_user_with_default_name(username1, username2='Guibs82'):    print("Hello " + username1 + " and " + username2)# 若未傳遞足夠參數, 則按位置進行參數傳遞greet_user_with_default_name('G')# 傳回值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()my_name = get_formatted_name("guibs", "g")print("My name is " + my_name)my_name = get_formatted_name("guibs", 'g', '82')print("My name is " + my_name)# 返回字典def build_person(first_name, last_name, age=''):    '''返回一個字典, 包含有關一個人的資訊'''    person = {        'first': first_name.title(),        'last': last_name.title(),    }    if age:        person['age'] = age    return personprint(build_person('g', 'ghost', 22))# 傳遞列表names = ['guibs', 'ghostg', 'rio_G']def greet_users(names):    for name in names:        print("Hello " + name.title())greet_users(names=names)# 在函數中修改列表# 將列表傳遞給函數後, 函數可以對其進行永久性修改unprinted_designs = ['iphone case', 'ipad case', 'mac case']printed_designs = []def print_designs(unprinted_designs, printed_designs):    while unprinted_designs:        current_design = unprinted_designs.pop()        print("準備列印: " + current_design)        printed_designs.append(current_design)        print("列印完畢: " + current_design)    print("全部作品列印完畢")print_designs(unprinted_designs=unprinted_designs, printed_designs=printed_designs)def show_printed_designs(printed_designs):    print("列印完畢的作品: ")    for printed_design in printed_designs:        print(printed_design)show_printed_designs(printed_designs)# 禁止函數修改列表# 採用切片的形式, 複製傳入函數的列表unprinted_designs = ['iphone case', 'ipad case', 'mac case', 'apple watch case']printed_designs = []print_designs(unprinted_designs[:], printed_designs)print(unprinted_designs) # 此時, 原列表未被修改# 傳遞任意數量的參數# [*param_name]# 此時, Python 將參數封裝至一個元組def print_username(*username):    print(username)print_username("Guibs")print_username("Guibs", 'GhostG')# 使用任意數量的關鍵字形參# [**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='Python'))# 注意: 在 import 時, 若不使用系統中的解譯器, 而是用自己建立的, 則報錯# 匯入儲存在模組中的函數# 匯入整個模組import pizzapizza.make_pizza(12, 'mushrooms', 'extra cheese')# 使用 as 給模組指定別名import pizza as pp.make_pizza(12, 'mushrooms', 'lots of cheese')# 匯入特定的函數# from module_name import function_name_0, function_name_1, ...# 這種文法可以無需使用 .from pizza import make_pizzamake_pizza(12, 'mushrooms', 'more cheese')# 使用 as 給函數指定別名from pizza import make_pizza as buy_pizzabuy_pizza(12, 'mushrooms', 'a lot of cheese')# 匯入模組中的所有函數# [*]from pizza import *get_price()

運行上方代碼另需建立 pizza.py

def make_pizza(size, *toppings):    '''概述要製作的披薩'''    print("做一個尺寸為: " + str(size) + ", 包含: ")    for topping in toppings:        print("- " + topping)    print("的披薩")def get_price():    print("The price is 20")


以上就是Guibs 的 Python學習_ 函數的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.