Skip to content

AI STUDIO Taiwan

台 灣 人 工 智 慧 與 資 料 科 學 研 究 室

  • About 關於本站
  • 專欄列表
  • Taiwan NLP.台灣自然語言處理與人工智慧交流社( 1.8萬位成員)
  • 關於 Taiwan NLP 社群

【Python 基礎教學】03 變數與指派敘述

Posted on 2020 年 3 月 28 日2023 年 3 月 28 日 By 楊 明翰 在〈【Python 基礎教學】03 變數與指派敘述〉中尚無留言
Python 入門
Print Friendly, PDF & Email

Assignment Statement 稱指派敘述,是幾乎任何一種程式語言都會出現的語句,是用來宣告或是給予變數具體的資料的。而「=」符號在多數程式語言中都是賦值運算子(Assignment Operators)。 例如: A = 1  ,不過實際上在Python語言中會讓變數A成為數值1的引用

在Python中當一個值(value)被建立之後,只要有變數被指派為它,變數就會用參考的方式指向該值,一旦該值不再被引用則會被回收。這裡舉個例子說明:

Python
# show how  varable(name) reference to a value (object) in python
# step 1
#   ┌───┐
#   │   │     x = 1
#   │ x ├───┐
#   │   │   │
#   └───┘ ┌─▼─┐
#         │ 1 │
#         └─▲─┘
#   ┌───┐   │
#   │   │   │
#   │ y ├───┘
#   │   │     y = 1
#   └───┘
x = 1
y = 1
print('x = 1')
print('y = 1')
print(f"x reference to  {id(x)}")
print(f"y reference to  {id(y)}")
print(f"1 reference to  {id(1)}")
print('--------------------------')
# x,y,1 have same reference
#
#
# step 2
#   ┌───┐
#   │   │   x = 2
#   │ x ├───────────┐
#   │   │           │
#   └───┘ ┌───┐   ┌─▼─┐
#         │ 1 │   │ 2 │
#         └───┘   └─▲─┘
#   ┌───┐           │
#   │   │           │
#   │ y ├───────────┘
#   │   │   y = 2
#   └───┘
x = 2
y = 2
print('x = 2')
print('y = 2')
print(f"x reference to  {id(x)}")
print(f"y reference to  {id(y)}")
print(f"1 reference to  {id(1)}")
print(f"2 reference to  {id(2)}")
print('--------------------------')
# x,y,2 have same new reference and 1 had the old reference
# step 3
#   ┌───┐
#   │   │
#   │ x ├───┐
#   │   │   │               ┌───┐
#   └───┘ ┌─▼─┐   ┌───┐     │   │
#         │ 2 │   │ 1 │◄────┤ z │
#         └─▲─┘   └───┘     │   │
#   ┌───┐   │               └───┘
#   │   │   │
#   │ y ├───┘
#   │   │
#   └───┘
z = 1
print('z = 1')
print(f"x reference to  {id(x)}")
print(f"y reference to  {id(y)}")
print(f"z reference to  {id(z)}")
print(f"1 reference to  {id(1)}")
print(f"2 reference to  {id(2)}")
# x,y,2 have same new reference and 1 , z had the old reference

為了說明需要,這裡使用Python的函數id(),它會返回物件的記憶體位址,這讓我們能觀察誤變數實際指向的位址。首先第一步, x = 1 以及 y = 1 分別讓變數x和y指向「1」這個數值,所以「x」、「y」和「1」三者指向的記憶體位址都相同(皆為數值1的位址)。第二步我們的指令 x = 2 以及 y = 2 ,實際上是新增加「2」這個數值,並讓變數x和y改為指向「2」,而原先的「1」由於還有被使用,所以保留著。這時候「x」、「y」和「2」三個的記憶體位址都相同(皆為數值2的位址),另外「1」的位置與第一步時相同。第三步,我們新增變數z,指令z = 1。這時會把z指向第一步驟時「1」的位置,所以最終結果「x」、「y」和「2」三個的記憶體位址都相同,而「z」和「1」這兩個記憶體位址相同,並與前組不同。

在前述的例子中,「1」和「2」都是不可變物件(Immutable objects),其值和記憶體位置一旦被設定就無法更改,反而是透過變數的重新指向來改變值所代表的意義。再舉另一個例子:

Python
# show how  varable(name) reference to a value (object) in python
# while changing value
x = 1
y = 1
# step 1
#   ┌───┐
#   │   │     x = 1
#   │ x ├───┐
#   │   │   │
#   └───┘ ┌─▼─┐
#         │ 1 │
#         └─▲─┘
#   ┌───┐   │
#   │   │   │
#   │ y ├───┘
#   │   │     y = 1
#   └───┘
print('x = 1')
print('y = 1')
print(f"x reference to  {id(x)}")
print(f"y reference to  {id(y)}")
print(f"1 reference to  {id(1)}")
# step 2
#   ┌───┐
#   │   │
#   │ x │
#   │   ├───┐
#   └───┘ ┌─▼─┐
#         │ 2 │
#         └───┘
#   ┌───┐
#   │   │ ┌───┐
#   │ y ├─► 1 │
#   │   │ └───┘
#   └───┘
x = y + 1
print('x = y + 1')
print(f"x = {x}")
print(f"x reference to  {id(x)}")
print(f"y reference to  {id(y)}")
print(f"1 reference to  {id(1)}")
print(f"2 reference to  {id(2)}")

在這個例子中,第一步先建立變數x和y,並同時賦予初始值=1,但其實是把它們都指向數值1的位址。第二步做了運算,x = y + 1 ,此時x的值被改成2,但實際上不是把數值1的位址的內容改寫,而是新增另一個記憶體位址並寫上數值2,然後把變數x指過去。而原先的數值1並沒有更動。

除了不可變物件之外,也有些是可變的物件(Mutable Objects),例如容器類的。這裡以list舉例:

Python
# show how  varable(name) reference to a value (object) in python
# while changing a list
x = [1,2]
print('x = [1,2]')
print("current x : ")
print(x)
print(f"x reference to  {id(x)}")
print(f"1 reference to  {id(1)}")
print(f"2 reference to  {id(2)}")
x.append(2)
print('x.append(2)')
print("current x : ")
print(x)
print(f"x reference to  {id(x)}")
print(f"1 reference to  {id(1)}")
print(f"2 reference to  {id(2)}")
x.append(3)
print('x.append(3)')
print("current x : ")
print(x)
print(f"x reference to  {id(x)}")
print(f"1 reference to  {id(1)}")
print(f"2 reference to  {id(2)}")
print(f"3 reference to  {id(3)}")

x 指向的記憶體位置是固定的,但內含的值有更動。

✔ 幫我們按個喜歡和分享,支持我們

平均分數 0 / 5. 給分人數 0

尚未有評分,請幫我評分歐

楊 明翰
楊 明翰

是一名八年級中段班的創業者與資料科學家

“With belief and action, we change the world.”

憑藉信念與行動,我們改變世界💪

更多關於站長

本文允許重製、散布、傳輸以及修改,但不得為商業目的之使用

使用時必須註明出處自:楊明翰 , 台灣人工智慧與資料科學研究室 https://aistudio.tw

標籤: 位址 參考 變數

文章導覽

❮ Previous Post: 【Python 基礎教學】02 進入程式語言的世界
Next Post: 【Python 基礎教學】04 迴圈 for loop ❯

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

更多AI相關資訊,歡迎加入站長的粉絲團

Facebook

閱讀視覺風格選擇


實驗袍白 / 適合資料分析師、科學家
終端機黑 / 適合黑客、電腦工程師

專欄列表

  • Artificial intelligence 人工智慧 (3)
    • HPC 高效能運算 (1)
    • PyTorch (1)
    • 從頭實作AI (1)
  • Backend 後端開發 (1)
  • C++ 應用 (2)
  • Community 社群介紹 (1)
  • Data Engineering 資料工程與訊號分析 (1)
  • Java 開發與教學 (2)
  • Linux 入門 (5)
  • NLP/NLU 自然語言處理與理解 (4)
  • Philosophy 站長哲學 (5)
  • Python 開發與教學 (7)
    • Python Flask 網站開發 (1)
    • Python 中階 (1)
    • Python 入門 (5)
  • Slides會議簡報 (12)
  • Start-up 創新創業 (7)
  • 機器學習數學基礎 (1)
  • 私人筆記手稿 (4)
  • 線上教學目錄 (4)

近期發布

  • 【Linux 應用】使用Zenity零成本開發的多開瀏覽器管理器
  • 【Start-up 創新創業】如何選擇創業題目
  • 【自然語言處理與理解】Llama-2大語言模型CPU版本使用
  • 【個人演講】AI EXPO 2023 ChatGPT以及LLM
  • 【自訓課程】NGS 次世代基因體資料科學 課程

討論

尚無留言可供顯示。

年份彙整

  • 2023 (9)
  • 2022 (4)
  • 2021 (6)
  • 2020 (14)
  • 2019 (8)
  • 2018 (10)
  • 2017 (2)
  • 2016 (4)
  • 2015 (2)

ChatGPT GRU HPC llama-cpp LLM Log4J LSTM Node.js SMO SVM Zenity 人工智慧 人格特質 出資 創新 創業 多語言 多開瀏覽器 大語言模型 感知機 戰鬥 技術 技術合夥 技術股 撰寫程式 新創 新手 新聞輿情 最佳化 機器學習 死亡 現代C++ 系統日誌 股東權益 能力 證明 變數 負債 資本 資產負債表 長短期記憶 霍普菲爾 類神經網 類神經網路 風險

調整字型大小

A 縮小字型大小。 A 重設字型大小。 A 放大字型大小。

Copyright © 2023 AI STUDIO Taiwan.
聯絡我們 : whuang022@gmail.com
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Theme: Oceanly by ScriptsTown