首頁 後端開發 Python教學 Guibs 的 Python學習_ 函數

Guibs 的 Python學習_ 函數

Jan 20, 2017 pm 04:12 PM
python 函數

 

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 person
print(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_hobbies

print(set_hobbies(name="Guibs", hobby_1='Swift', hobby_2='Python'))# 注意: 在 import 时, 若不使用系统中的解释器, 而是用自己创建的, 则报错
# 导入存储在模块中的函数
# 导入整个模块import pizza
pizza.make_pizza(12, 'mushrooms', 'extra cheese')# 使用 as 给模块指定别名import pizza as p
p.make_pizza(12, 'mushrooms', 'lots of cheese')# 导入特定的函数# from module_name import function_name_0, function_name_1, ...
# 这种语法可以无需使用 .from pizza import make_pizza
make_pizza(12, 'mushrooms', 'more cheese')# 使用 as 给函数指定别名from pizza import make_pizza as buy_pizza
buy_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")
登入後複製


網(www.php.cn)!


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

模板化的優點和缺點有哪些? 模板化的優點和缺點有哪些? May 08, 2024 pm 03:51 PM

模板化的優點和缺點有哪些?

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Jul 01, 2024 am 07:22 AM

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2

怎麼下載deepseek 小米 怎麼下載deepseek 小米 Feb 19, 2025 pm 05:27 PM

怎麼下載deepseek 小米

excel函數公式大全 excel函數公式大全 May 07, 2024 pm 12:04 PM

excel函數公式大全

deepseek怎麼問他 deepseek怎麼問他 Feb 19, 2025 pm 04:42 PM

deepseek怎麼問他

NET40是什麼軟體 NET40是什麼軟體 May 10, 2024 am 01:12 AM

NET40是什麼軟體

deepseek該怎麼搜索 deepseek該怎麼搜索 Feb 19, 2025 pm 05:18 PM

deepseek該怎麼搜索

瀏覽器插件是什麼語言寫的 瀏覽器插件是什麼語言寫的 May 08, 2024 pm 09:36 PM

瀏覽器插件是什麼語言寫的

See all articles