● Pythonic是一種形容詞,意旨可閱讀性高且簡單扼要的程式碼。

**# 值互換_一般的寫法**
a = 10
b = 5
temp = a 
a = b
b = temp
print (a, b)
# 5 10

**# Pythonic寫法**
a = 10
b = 5
a, b = b, a
print(a, b)
# 5 10

a = 10
b = 5
c = 20
a, b, c = b, c, a
print(a, b, c)
# 5 20 10

**# 連結資料庫取資料_一般寫法**
info = get_user_id(id)
print("The user name is " + info[0])
print("The user age is " + info[1])
# 較難看出info代表什麼

**# Pythonic寫法_Tuple**
name, age = get_user_id(id)
print("The user name is " + name)
print("The user age is " + age)
**# 一般寫法**
b = 50
if b > 10 and b < 100:
    print("b is in the rage of 10 and 100.")
# b is in the rage of 10 and 100.

b = 0
if b > 10 and b < 100:
    print("b is in the rage of 10 and 100.")
# 不會有東西出現。

**# Pythonic寫法**
b = 50
if 10 < b < 100:
    print("b is in the rage of 10 and 100.")
# b is in the rage of 10 and 100.
# 這種寫法只能在Python裡使用。
**# 檢查function command(指令)是否正確_一般寫法**
cmd = input("Give a command: ")
if cmd == "cd" or cmd == "dir" or cmd == "echo":
    print("valid command")
else:
    print("invalid command")

# 切換到Terminal
# PS C:\\Users\\flute\\Desktop\\Python Codes> python .\\66.Pythonic_2.py
# Give a command: dir
# valid command
# PS C:\\Users\\flute\\Desktop\\Python Codes> python .\\66.Pythonic_2.py
# Give a command: mkdir
# invalid command

# Pythonic寫法
cmd = input("Give a command: ")
# membership operator_boolin
if cmd in ('dir', 'cd', 'echo'):
    print("valid command")
else:
    print("invalid command")

# 切換到Terminal
# PS C:\\Users\\flute\\Desktop\\Python Codes> python .\\66.Pythonic_2.py
# Give a command: echo
# valid command
# Give a command: abc
# invalid command

● Pythonic: Readable, Clean, Easy, Clear, Python Features and Built-in functions.