● Restriction: variable命名的限制

  1. 開頭必須為英文字或是底線不能用數字當開頭

    _player_1 🙆‍♀️
    player_1 🙆‍♀️
    1_player 🙅‍♀️
    1player = "Grace"
    print(1player) # 命名字首不得為數字。
    # SyntaxError: invalid syntax
    
    
  2. 只能用字母數字,以及底線作為命名元素。

  3. 命名大小寫有分別的。

NAME = "Grace" 
print(name) # 命名的大小寫有區分。
# NameError: name 'name' is not defined
  1. 避開Reserved words用於命名,像是int, list。
  2. 特殊符號不得用於命名,如: ” ” / ? | \ ( ) ! @ ~ + #

● Convention: variable命名的習慣

  1. Module name: 使用時全部用小寫(如: sys, math, import module name)若真的需使用到,使用底線將兩個字分開。
formal_name = "Grace Zhan"
formalName = "Grace Zhan" # camelcase 像駱駝駝峰一樣突然凸起。
  1. Function name: 小寫,若需要用底線分開。
  2. Variable name: 小寫,若需要則用底線分開。
  3. Class name: 字首大寫,若需使用則用Camelacse分開。
  4. Constants常數: 全部大寫,若需要則用底線分開。
  5. Comparison: 不會使用 = =
haveBudget = True
if haveBudget:
    print("Buy a house")
else: 
    print("Don't buy a house.")
# Buy house

haveBudget = True
if haveBudget == True:
    print("Buy a house")
else: 
    print("Don't buy a house.")
# Buy a house

# 比較好的寫法
not_have_budget = False
if not not_have_budget:
    print("buy a house")
else:
    print ("Don't buy a house.")
# Buy a house