▶ Try Python

Python Operators

The symbols that do the work — comparisons, logic, assignments.

Arithmetic

OpNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/True division7 / 23.5
//Floor division7 // 23
%Modulo7 % 21
**Exponent2 ** 8256

Comparison

Python
x = 10 x == 10 # True — equal x != 5 # True — not equal x > 5 # True — greater than x < 5 # False — less than x >= 10 # True — greater or equal x <= 5 # False — less or equal # Python allows chaining: 1 < x < 20 # True — same as (1 < x) and (x < 20)

Logical

Python
True and False # False — both must be True True or False # True — at least one must be True not True # False # Short-circuit: Python stops as soon as it knows the answer 0 and risky_function() # risky_function never called True or risky_function() # risky_function never called

Assignment operators

Python
x = 10 x += 5 # x = 15 x -= 3 # x = 12 x *= 2 # x = 24 x /= 4 # x = 6.0 x //= 2 # x = 3.0 x **= 3 # x = 27.0 x %= 5 # x = 2.0

Identity and membership

Python
a = [1, 2, 3] b = a c = [1, 2, 3] a is b # True — same object in memory a is c # False — equal contents, different objects a == c # True — equal contents 2 in a # True — membership check 5 not in a # True
== vs is Use == to compare values. Use is only to check identity (same object in memory). x is None is the Pythonic way to check for None — never x == None.