python 基礎筆記

help() 用法:

# 查看看所有的內建函式
dir(__builtins__)
#[…, 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

# 查看 print() 內建函式的使用語法,不須加函式的 () 括號
help(print)

# 查看字串類別的使用語法 (先使用 type() 查看字串的類別)
type('s')
#<class 'str'>

help('str')

# 查看數值類別的使用語法 (可直接填入數值就會自動查看 int 類別)
help(1)

# 查看 sys 模組的使用語法
help('sys')

# 也可直接進入 help() 的提示字元查看,就可省略再輸入 help()
help()

help> print
help> str

Output 輸出 print:

print() 內建函式來印 (輸出) 資料,完整語法如下:
print(value, ..., sep=' ', end='', file=sys.stdout, flush=False)

#print() 內建函式參數表:
  value:要輸出的資料:可使用 , 逗號分隔指定多個 value
  sep:' ':value 與 value 之間的分隔方式
  end: :資料最後的結束方式
  file:sys.stdout:將資料輸出至指定檔案
  flush:False:True 立即將資料輸出至檔案 False 先將資料存至記憶體,待檔案關閉時才把資料輸出

#範例程式:
  # 輸出三個 value
    print('A', 'B', 'C')
    #結果:
    A B C
 
  # value 與 value 使用 - 符號分隔
    print('A', 'B', 'C', sep = '-')
    #結果:
    A-B-C
 
  # 資料最後的結束方式
    print('A', 'B', 'C', end = '*****')
    #結果:
    A B C
    *****
 
  # 將資料輸出至 data.txt 檔案
    print('A', 'B', 'C', file = open('data.txt', 'w'))

type() 查詢資料型態:

print(type(100))
#結果:

 
print(type('name'))
#結果:


強制停止程式:

#程式強制關閉,不回傳任何值
os._exit()

#會產生例外SystemExit,等同發起SystemExit(),可用於處理後續的Process
sys.exit(0)#預設為0,表示正常退出,也可以為1,表示異常退出

if else if 判斷語句:

#if...elif...else 多重條件判斷:
if 條件式 1:
    # 語句...
elif 條件式 2:
    # 語句...
elif 條件式 3:
    # 語句...
.
.
# 上述條件式都不成立執行
else:
    # 語句..

 

參考及好文:

MIS 腳印-資料型態
MIS 腳印-運算子



分享所學知識,願能求得朋友共同討論與分享。
明天的筆記本logo