Python是一種強大的編程語言,擁有許多有用的表達式。
# 賦值表達式 x = 5 y = "Hello World" z = [1, 2, 3] # 算術表達式 a = 10 b = 5 c = a + b # c = 15 d = a - b # d = 5 e = a * b # e = 50 f = a / b # f = 2.0(Python3中) # 邏輯表達式 g = True h = False i = g and h # i = False j = g or h # j = True k = not g # k = False # 比較表達式 l = 10 m = 5 n = l >m # n = True o = l< m # o = False p = l == m # p = False q = l != m # q = True r = l >= m # r = True s = l<= m # s = False # 字符串表達式 t = "Hello" u = "World" v = t + u # v = "HelloWorld" w = t * 3 # w = "HelloHelloHello" x = len(t) # x = 5 y = str(123) # y = "123" # 列表表達式 z = [1, 2, 3] a = z[0] # a = 1 b = z[1:3] # b = [2, 3] c = len(z) # c = 3 d = z.count(2) # d = 1
上述表達式可以幫助開發人員快速準確地處理各種數據類型。Python的表達式豐富多彩,學會使用它們可以大大提高代碼的效率和可讀性。