» Python快速入门 » 1. 基础篇 » 1.7 运算符

运算符

算术运算符

x, y = 5, 8

print('x + y:', x + y)  # Addition
print('x - y:', x - y)  # Subtraction
print('x * y:', x * y)  # Multiplication
print('x / y:', x / y)  # Division
print('x % y:', x % y)  # Modular Division
print('x // y:', x // y) # Floor Division
print('x ** y:', x ** y) # Exponentiation

比较运算符

x, y = 5, 8

print('x == y:', x == y)  # Equal to
print('x != y:', x != y)  # Not Equal to
print('x > y:', x > y)   # Greater than
print('x < y:', x < y)   # Less than
print('x >= y:', x >= y)  # Greater than or Equal to
print('x <= y:', x <= y)  # Less than or Equal to

逻辑运算符

x, y = True, False

print('x and y:', x and y) # And
print('x or y:', x or y) # Or
print('not x:', not x) # Negate

位操作运算符

x, y = 5, 8

print('x & y:', x & y) # Bitwise AND
print('x | y:', x | y) # Bitwise OR
print('~y:', ~y) # Bitwise NOT
print('x ^ y:', x ^ y) # Bitwise XOR
print('x << y:', x << y) # Left Shift
print('x >> y:', x >> y) # Right Shift

标识运算符

x, y = [5, 8], [5, 8]
z = x

print('x is y:', x is y)
print('x is z:', x is z)

print('x is not y:', x is not y)
print('x is not z:', x is not z)

成员运算符

x = 'literank'
y = ['literank', 'tutorial', 'article']

print('x in y:', x in y)
print('x not in y:', x not in y)

Code Challenge

尝试修改编辑器中提供的代码以使其打印出 [12, 20, 28].

Loading...
> 此处输出代码运行结果
上页
下页