2.python的常量、變量、關鍵字

常量

  1. python中沒有定義常量的關鍵字,一般用 變量名全大寫來標明關鍵字 ,可是這個變量仍是能夠改變的
  2. 自定義類來實現常量。

變量

  • 變量命名和其餘語言相似,由英文字母、·數字下劃線組成,開頭不能是數字。
  • python 變量是區分大小寫的
# 聲明變量
x = "Therre are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)

關鍵字

KEYWORD DESCRIPTION EXAMPLE
and 邏輯與 True and False == False
as with-as語句的一部分 with X as Y: pass
assert 聲明 assert False, "Error!"
break 中止整個循環 while True: break
class 定義一個類 class Person(object)
continue 中止這一次循環,但繼續下一次循環 while True: continuev
def 定義一個函數 def X(): pass
del 從字典中刪除 del X[Y]
elif Else if 條件 if: X; elif: Y; else: J
else Else 條件 if: X; elif: Y; else: J
except 若是捕獲異常,執行該代碼塊 except ValueError, e: print e
exec 將字符串做爲Python代碼執行 exec 'print "hello"'
finally 無論是否有異常,finally代碼塊都執行 finally: pass
for for循環 for X in Y: pass
from 從某一模塊中引入特定部分 import X from Y
global 定義一個全局變量 global X
if If 條件 if: X; elif: Y; else: J
import 引入一個模塊到當前模塊 import os
in for循環的一部分/ 測試X in Y. for X in Y: pass / 1 in [1] == True
is 相似==,判斷相等 1 is 1 == True
lambda 建立一個無名函數 s = lambda y: y ** y; s(3)
not 邏輯非 not True == False
or 邏輯或 True or False == True
pass 該代碼塊爲空 def empty(): pass
print 打印一個字符串 print 'this string'
raise 代碼出錯時,拋出一個異常 raise ValueError("No")
return 退出函數並返回一個返回值 def X(): return Y
try 嘗試代簽代碼塊,有異常則進入except代碼塊 try: pass
while While循環 while X: pass
with 一個變量的別名 with X as Y: pass
yield 暫停, 返回給調用者 def X(): yield Y; X().next()

參考資料

相關文章
相關標籤/搜索