Foundation
Operators in Python
Operators perform operations on values and variables.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | Division (float) | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Modulus | 7 % 3 | 1 |
** | Exponent | 2 ** 3 | 8 |
Comparison operators
==, !=, >, <, >=, <= return a bool.
comparison.py
print(5 > 3) # True
print(5 == 5.0) # True
Logical operators
logical.py
age = 20
print(age > 18 and age < 60) # True
print(not age > 18) # False
Assignment operators
assignment.py
x = 10
x += 5 # 15
x *= 2 # 30
Membership and identity
membership.py
print("a" in "banana") # True
print(3 not in [1, 2, 3]) # False
a = [1]; b = a
print(a is b) # True (same object)
Loading Comments...