大家好~我是Willis,今天又要介紹Python的基本語法囉,一起加油吧 ! ٩(✿∂‿∂✿)۶
app.py
a = 4
b = 2
print("a + b = {}".format(a+b))
print("a - b = {}".format(a-b))
print("a * b = {}".format(a*b))
print("a / b = {}".format(a/b))
print("a % b = {}".format(a % b))
print("a ** b = {}".format(a**b))
a + b = 6
a - b = 2
a * b = 8
a / b = 2.0
a % b = 0
a ** b = 16
app.py
a = 4
b = 2
print("a == b = {}".format(a == b))
print("a != b = {}".format(a != b))
print("a > b = {}".format(a > b))
print("a < b = {}".format(a < b))
print("a >= b = {}".format(a >= b))
print("a <= b = {}".format(a <= b))
a == b = False
a != b = True
a > b = True
a < b = False
a >= b = True
a <= b = False
app.py
a = 4
b = 2
print("a == 4 and b == 2 is {}".format(a == 4 and b == 2))
print("a == 2 and b == 4 is {}".format(a == 2 and b == 4))
print("a == 4 and b == 4 is {}".format(a == 4 and b == 4))
print("a == 2 and b == 2 is {}".format(a == 2 and b == 2))
print("a == 4 or b == 2 is {}".format(a == 4 or b == 2))
print("a == 2 or b == 4 is {}".format(a == 2 or b == 4))
print("a == 4 or b == 4 is {}".format(a == 4 or b == 4))
print("a == 2 or b == 2 is {}".format(a == 2 or b == 2))
print("not a == b is {}".format(not a == b))
print("not a != b is {}".format(not a != b))
a == 4 and b == 2 is True
a == 2 and b == 4 is False
a == 4 and b == 4 is False
a == 2 and b == 2 is False
a == 4 or b == 2 is True
a == 2 or b == 4 is False
a == 4 or b == 4 is True
a == 2 or b == 2 is True
not a == b is True
not a != b is False
and 和 or 看起來是不是有點複雜 (•ิ_•ิ)? 其實很簡單,看一下我的解說就懂了~
if 條件:
app.py
a = (int)(input("請問1+1=? "))
if(a == 2):
print("恭喜答對")
請問1+1=? 2
恭喜答對
if 條件:
...
else:
...
app.py
a = (int)(input("請問1+1=? "))
if(a == 2):
print("恭喜答對")
else:
print("答錯囉~") # 沒有滿足if輸出條件就執行
請問1+1=? 2
恭喜答對
請問1+1=? 3
答錯囉~
if 條件:
...
elif 條件:
...
app.py
a = (int)(input("請問1+1=? "))
if(a == 2):
print("恭喜答對")
elif(a < 1):
print("你的加法會越加越少?")
elif(a > 100):
print("你的數學體育老師教的?")
請問1+1=? 2
恭喜答對
請問1+1=? 0
你的加法會越加越少?
請問1+1=? 101
你的數學體育老師教的?
if 條件:
...
elif 條件:
...
else:
...
app.py
a = (int)(input("請問1+1=? "))
if(a == 2):
print("恭喜答對")
elif(a < 1):
print("你的加法會越加越少?")
elif(a > 100):
print("你的數學體育老師教的?")
else:
print("答錯囉~")
請問1+1=? 2
恭喜答對
請問1+1=? 0
你的加法會越加越少?
請問1+1=? 101
你的數學體育老師教的?
請問1+1=? 3
答錯囉~
while 持續條件:
app.py
a = 0
while a < 5:
print("a = {}".format(a))
a += 1 # 與a=a+1意思是一樣的
a = 0
a = 1
a = 2
a = 3
a = 4
for 變數 in 提取資料:
app.py
for a in "Willis":
print(a)
W
i
l
l
i
s
for 變數 in range(初始值,結束值,運算值):
for a in range(0, 5, 2):
print(a)
0
2
4
初始值預設為0,運算值預設為+1唷~(≧д≦ヾ)
for 變數 in range(初始值,結束值,運算值):
for 變數 in range(初始值,結束值,運算值):
...
app.py
for a in "第一層":
for b in range(3):
print("{} {}".format(a, b))
第 0
第 1
第 2
一 0
一 1
一 2
層 0
層 1
層 2
在上一篇的資料型別中,我們介紹了list(列表),其實列表還可以使用二維、多維的方式來放置資料,詳細使用方法如下。
app.py
list_1 = [[1, 2, 3], [4, 5, 6]]
print(type(list_2D)) # 二維列表
<class 'list'>
你要增加幾維就在多包一層[ ]就好了。
app.py
list_1 = [[[1, 2], 3], [4, 5, 6]]
print(type(list_1)) # 多維列表
<class 'list'>
https://ithelp.ithome.com.tw/articles/10260108
https://medium.com/ccclub/ccclub-python-for-beginners-tutorial-4990a5757aa6
今天就差不多到這裡啦 ! 我們下一篇再見面囉~ (^・ω・^ )