Operators#

Introduction#

Operators in Python are special symbols and keywords that allow you to perform operations on values and variables. They encompass a wide range of functionalities—from arithmetic and comparison to logical, bitwise, and assignment operations. Mastering these operators is essential for constructing expressive and efficient Python code, as they form the foundation of nearly every computation or decision-making process in your programs. Below, you’ll find concise examples for each operator to help you understand their usage and expected outcomes.

Arithmetic#

Name

Symbol

Operator

Example

Addition

+

a + b

5 + 3 # 8

Division

/

a / b

10 / 4 # 2.5  (always returns a float)

Exponentiation

**

a ** b

2 ** 3 # 8

Floor Division

//

a // b

10 // 4 # 2    (always returns an integer)

Modulo

%

a % b

10 % 4 # 2

Multiplication

*

a * b

5 * 3 # 15

Negation

-

- a

- 5 # -5

Subtraction

-

a - b

10 - 3 # 7

Augmented Arithmetic#

Name

Symbol

Operator

Example

Addition

+=

a += b

a = 5
b = 3
a += b # a = 8

Division

/=

a /= b

a = 10
b = 4
a /= b # a = 2.5

Exponentiation

**=

a **= b

a = 2
b = 3
a **= b # a = 8

Floor Division

//=

a //= b

a = 10
b = 4
a //= b # a = 2

Modulus

%=

a %= b

a = 10
b = 3
a %= b # a = 1

Multiplication

*=

a *= b

a = 2
b = 3
a *= b # a = 6

Subtraction

-=

a -= b

a = 10
b = 4
a -= b # a = 6

Comparison#

Name

Symbol

Operator

Example

Equality

==

a == b

5 == 5 # True

Inequality

!=

a != b

5 != 10 # True

Greater Than

>

a > b

10 > 5 # True

Greater Than or Equal

>=

a >= b

10 >= 5 # True

Less Than

<

a < b

5 < 10 # True

Less Than or Equal

<=

a <= b

5 <= 5 # True

Chained Comparison

varies

a < b < c

1 < 2 < 3 # True (1 < 2) and (2 < 3)

Logical#

Name

Symbol

Example

False

False

Certain values are defined to be False, including:

  • None
  • False
  • Numeric zero of any type: 0, 0.0, 0j
  • Empty sequences and collections: '' (empty string), [] (empty list), () (empty tuple), {} (empty dictionary), set(), etc.

True

True

Any object that is not explicitly False is considered True. For instance, non-empty strings, non-zero numbers, non-empty lists, and even custom objects (unless they override the truth value methods to return False) are treated as True.


Name

Symbol

Operator

Example

Identity

is

a is b

[1] is [1] # False

Not Identity

is not

a is not b

5 is not 10 # True

Logical AND

and

a and b

True and False # False

Logical Negation

not

not a

not False # True

Logical OR

or

a or b

False or True # True

Conditional Expression

if else

x if condition else y

"Yes" if 5 > 3 else "No" # "Yes"

Sequences (Lists, Tuples, Strings)#

Name

Symbol

Operator

Example

Concatenation

+

seq1 + seq2

[1, 2] + [3, 4] # [1, 2, 3, 4]

Slice Assignment

seq[i:j] = values

lst = [1, 2, 3, 4]
lst[1:3] = [20, 30] # lst = [1, 20, 30, 4]

Slice Deletion

del seq[i:j]

lst = [1, 2, 3, 4]
del lst[1:3] # lst = [1, 4]

Slicing

seq[i:j]

lst = [1, 2, 3, 4]
lst[1:3] # [2, 3]

Dictionaries (Mappings)#

Name

Symbol

Operator

Example

Indexed Assignment

obj[k] = v

d = {}
d['a'] = 1 # d = {'a': 1}

Indexed Deletion

del obj[k]

d = {'a': 1}
del d['a'] # d =  {}

Indexing

obj[k]

d = {'a': 1}
d['a'] # 1

String Formatting#

Name

Symbol

Operator

Example

String Formatting

%

s % obj

str = "Hello, %(name)s" % {"name": "Alice"}    # str = 'Hello, Alice'

Membership#

Name

Symbol

Operator

Example

Containment Test

in

obj in seq

'a' in 'cat' # True

Membership Negation (Not In)

not in

a not in b

'x' not in 'hello' # True

Bitwise#

Name

Symbol

Operator

Example

Bitwise And

&

a & b

6 & 3 # 2 (0b110 & 0b011 = 0b010)

Bitwise Exclusive Or (XOR)

^

a ^ b

6 ^ 3 # 5 (0b110 ^ 0b011 = 0b101)

Bitwise Inversion (NOT)

~

~ a

~5 # -6 (~0b101 yields -6 in two's complement)

Bitwise Or

|

a | b

6 | 3 # 7 (0b110 | 0b011 = 0b111)

Left Shift

<<

a << b

2 << 3 # 16 (0b10 << 3 becomes 0b10000)

Right Shift

>>

a >> b

16 >> 2 # 4 (0b10000 >> 2 becomes 0b100)

Augmented Bitwise#

Name

Symbol

Operator

Example

Augmented Bitwise And

&=

a &= b

a = 6
b = 3
a &= b # a = 2 (0b110 & 0b011 = 0b010)

Augmented Bitwise Exclusive Or (XOR)

^=

a ^= b

a = 6
b = 3
a ^= b # a = 5 (0b110 ^ 0b011 = 0b101)

Augmented Bitwise Or

|=

a |= b

a = 6
b = 3
a |= b # a = 7 (0b110 | 0b011 = 0b111)

Augmented Left Shift

<<=

a <<= b

a = 5
b = 2
a <<= b # a = 20 (5 << 2 equals 5 * 2²)

Augmented Right Shift

>>=

a >>= b

a = 20
b = 2
a >>= b # a = 5 (20 >> 2 equals 20 // 2²)