# python 重點整理 :::spoiler 章節目錄 [TOC] ::: # 英文名詞定義: ### attribute - > 屬性,物件中的命名值,存儲物件的狀態。可以使用「點(.)」來訪問。分為以下三種命名方式: 1. public:沒有底線作為前綴,可以全域訪問 2. protected:單底線前綴(_variable 或 _method),只能在def的class或subclass中訪問 3. private:雙底線前綴:(__variable 或 __method),只能在def的class中使用,無法被subclass或外部訪問,若發生名稱衝突,解譯器會自動重命名這些變量以確保其優先級最高。 ### element - > 元素,一般指還沒有歸屬(尚不屬於某個容器內的元素),或即將加入容器的元素 > 如:加入一個element到list # 常見函式整理 --- ### def - 用於定義函數。它後面跟著函數名稱和括號,括號中可以包含函數的參數。接下來是一個冒號和縮排的代碼塊,代表著函數的主體。 EX: :arrow_down: ```python def add(n1,n2): ans=n1+n2 print (ans) add(7,8) #呼叫上方名為add的函式 將(7,8)分別代入n1及n2 add(20,3) ``` 終端機: 15 23 ### class ```python class Test: x=3 def say(): print ("Hello World") ``` ### while 迴圈 - while 迴圈用於重複執行一個代碼塊,直到滿足指定的條件為止 EX: :arrow_down: ```python n=1 while n<=5 print("變數n的資料是:",n) n+=1 ``` ### for 迴圈 - for "變數" in [ ] 將列表中的項目或字串中的字元逐一取出,逐一處理 ```python for n in[1,3,5] print (n) ``` 終端機: 1 3 5 將list內的項目 逐一取出 逐一處理 ### range 製造一個連續數字的列表 :arrow_forward: [列表] ### import - 用於導入module , packages 以及library ```python import pygame ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up