function 要先定義,定義完要呼叫才會執行
def test():
print("123")
test()
因為python是直譯的關係,如果先呼叫 會找不到定義
test()
def test():
print("123")
用function的時機:同樣邏輯的事情做很多次
return 返回值:要拿變數去接、或者印出
def test():
return 555
print(test())
x = test()
print(x)
def test():
print("444")
return 555
print(test())
return 的意思就是結束、離開目前的function
def test():
return 555
print("444")
print(test())
return 可以一次回傳多個值
def test():
return 1, 3, 5
print(test())
a, b, c = test()
print(a)
print(b)
print(c)
如果只拿一個變數接收多個返回值,a會變成tuple元組
def test():
return 1, 3, 5
a = test()
print(a)
在Python裡,function要不要加上return都可以
如果沒有return,拿變數a去接的話會是None
def test():
print("Hi")
a = test()
print(a)
def test():
print("Hi")
return None
def test(t):
print(t)
test(5)
兩數相加
def add(a, b):
return a + b
x = add(5, 8)
print(x)
簡單的計算機
def cal(a, op, b):
if op == "+":
return a + b
if op == "-":
return a - b
if op == "*":
return a * b
if op == "/":
return a / b
x = cal(5, "+", 8)
print(x)
x = cal(5, "-", 8)
print(x)
x = cal(5, "*", 8)
print(x)
x = cal(5, "/", 8)
print(x)
原本電腦中的設定有多個python macOS內建python3、有透過brew、IDLE、PyCharm安裝的 https://developer.apple.com/forums/thread/680222 後來全都都刪除 /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 /usr/local/ bin/python3 /usr/bin/python3
Aug 24, 2022松崗 作者黃建庭 2-15 bit位移 A = 5 << 2 2-16 運算子優先順序 1 ()[]{}
Aug 7, 2021code = { 'A':"10", 'B':"11", 'C':"12", 'D':"13", 'E':"14", 'F':"15", 'G':"16", 'H':"17", 'I':"34",
Jul 12, 2021List 陣列、串列 list是有序的 string = "String" for s in string: print(s) # "String" => "S","t","r","i","n","g" Tuple 元組
Jul 12, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up