Python operators help you work with values or operands. They also help with changing them and linking them together. The operators can be logical or arith­metic.

What are Python operators and how do they work?

An operator is a character for an operation. Operators are usually used to link several operands to form a new value. The ap­pli­ca­tion of an operator to a single operand modifies it.

The simplest example of Python operators would probably be to join two numbers using the addition operator, which is a plus sign between the numbers. Python evaluates the ex­pres­sion and returns the cor­re­spond­ing value:

1 + 1

Python is special because short words such as “and”, “or”, “is”, “not” or “in” are used as operators in addition to symbols. The com­bi­na­tion of operators and operands results in an ex­pres­sion:

1 + 1 == 2

What are the different types of Python operators?

Python knows several classes of operators. They operate on different types of operands and return a certain type of results. Below is an overview of the different types of Python operators:

Classes of Python operators Ex­pla­na­tion Operands Result Operators at a glance
Arith­metic operators Combine two numbers to form a new number Numbers Number +, -, *, /, //, %, **, @
Com­par­i­son operators Compare two ex­pres­sions with each other Ex­pres­sions Boolean <, >, ==, !=, <=, >=
Logical operators Combine ex­pres­sions in a Boolean context Ex­pres­sions Last evaluated ex­pres­sion / Boolean and, or, not
Bitwise operators Ma­nip­u­late integers as binary sequences Numbers Number <<, >>, &, |, ^, ~
As­sign­ment operators Assign value to a name Lvalue, Rvalue - / Evaluated ex­pres­sion =, :=, +=, -=, *=, etc.
Identity operators Determine whether two names refer to the same object Objects Boolean is, is not
Con­di­tion­al operator Returns one of two values depending on a condition Ex­pres­sion, condition, al­ter­na­tive Ex­pres­sion / Al­ter­na­tive ... if ... else ...
Set operators Link two sets / Compare sets Quan­ti­ties Quantity / Boolean &, |, ^, -, <, >, <=, >=
Mem­ber­ship operators Testing whether an iterable contains a specific object Object, Iterable Boolean in, not in
Con­cate­na­tion operator Chained sequences Strings / Lists / Tuples String / List / Tuple +
Index and slice operators Return one or more elements of an iterable Iterable, Index / Slice String / List / Tuple [], [::]

Operators are clas­si­fied according to their “arity”, along with the type of operands and return value. The arity of an operator has nothing to do with “aric”. The term explains how many operands an operator combines. “Binary” operators with two operands are used in most cases. There are also some “unary” operators with only one operand, as well as a “ternary” operator which links three operands:

Operator arity Number of operands Example
Unary One operand not single_value
Binary Two operands left_operand + right_operand
Ternary Three operands some_value if condition else other_value

What is note operator prece­dence?

An un­der­stand­ing of operator prece­dence is fun­da­men­tal when it comes to using Python operators. The concept comes from arith­metic and is known as “point before dash cal­cu­la­tion”. Just to remind you, the ex­pres­sion 3 * 8 + 2 is in­ter­pret­ed as (3 * 8) + 2, not 3 * (8 + 2). As for the plus sign and the mul­ti­pli­ca­tion sign, there are prece­dence rules for all Python operators. Below is an example of an ex­pres­sion with the logical operators “and”, “or” and “not”:

if is_user and is_user_logged_in or is_admin and not login_blocked:
    ...

It is im­pos­si­ble to know how the in­di­vid­ual terms should be put together without knowing the prece­dence rules for the Python operators involved. Things can get com­pli­cat­ed if multiple operators are used in one ex­pres­sion. It is generally better not to rely on a perfect un­der­stand­ing of implicit rules. You should use explicit braces to clarify how the ex­pres­sion’s terms belong together:

if (is_user and is_user_logged_in) or (is_admin and not login_blocked):
    ...

The same terms grouped dif­fer­ent­ly result in a different statement:

if (is_user and is_user_logged_in or is_admin) and not login_blocked:
    ...

What are over­loaded operators, Dunder methods and Python operator functions?

Some Python operators are used for more than one operation. A prominent example is the plus sign. The plus sign acts as both an addition operator for numbers and a con­cate­na­tion operator for con­cate­nat­ing sequences such as strings and lists. We add two numbers with the addition operator:

8 + 3 == 11

We con­cate­nate two strings using the same operator:

"Walter" + "White" == "WalterWhite"

We also con­cate­nate lists with the plus operator:

['Jack', 'Jim'] + ['John'] == ['Jack', 'Jim', 'John']

The plus sign’s mul­ti­func­tion­al­i­ty as an operator reflects a common concept in computer science. You may have heard of “over­loaded operators”, which happen when one and the same operator perform different op­er­a­tions depending on the operands’ data type.

How do over­loaded operators work in Python? An operator and its operands are in­ter­pret­ed as a cue to a cor­re­spond­ing function. The first operand’s Dunder method is enabled, which receives the other operands as arguments. “Dunder” stands for “double un­der­score”. Therefore, the plus operator cor­re­sponds to the __add__() Dunder method. Objects which implement an __add__() method can be linked with the plus operator. It is up to the object what con­sti­tutes the link exactly.

In addition to the Dunder methods, the operator module contains functions which enclose the Python operators’ func­tion­al­i­ty. For example, “operator.add(a, b)” enables the Dunder method a.__add__(b), which is equiv­a­lent to the ex­pres­sion a + b. We’ll list the operator function for each operator when possible through­out the rest of the article. The name of the operator function cor­re­sponds to the name of the ap­pro­pri­ate Dunder method. You can use this table as a reference for im­ple­ment­ing your own operator func­tion­al­i­ty:

Python operator Operator function Dunder method
a + b operator.add(a, b) a.__add__(b)

Operators use the infix notation which inserts the operator between the operands, while the func­tion­al style uses the prefix notation. Both notations are equiv­a­lent:

Notation Use Example
Infix Operators a + b
Prefix Functions + a b / add(a, b)

Let’s consider an example. We define two numbers and add them to the operator, the operator function and the cor­re­spond­ing Dunder method:

import operator
a = 42
b = 69
assert a + b == operator.add(a, b) == a.__add__(b)

More complex ex­pres­sions can also be written with operator functions. The com­bi­na­tion of the Python addition and equality operators in prefix notation can be found below:

import operator
assert 'Py' + 'thon' == 'Python'
assert operator.eq(operator.add('Py', 'thon'), 'Python')

An overview of Python operators

We will now look into the eleven different Python operator classes.

Arith­metic operators

Python’s arith­metic operators operate on numbers to create a new number. All of these are binary operators except for the unary plus or minus. An overview is outlined below:

Python operator Meaning Operator function Example
+ Addition / unary plus add(a, b) / pos(a) 5 + 3 == 8 / +8 == 4 + 4
- Sub­trac­tion / unary minus sub(a, b) / neg(a) 7 - 2 == 5 / -4 == 2 - 6
* Mul­ti­pli­ca­tion mul(a, b) 2 * 3 == 6
/ “Real” Division truediv(a, b) 8 / 2 == 4.0, 7 / 2 == 3.5
// Integer division to next lower integer floordiv(a, b) 8 // 2 == 4, 7 // 2 == 3
% Modulus: Integer division’s remaining amount mod(a, b) 8 % 2 == 0, 7 % 2 == 1
** Ex­po­nen­ti­a­tion pow(a, b) 2 ** 3 == 8, 10 ** -1 == 0.1
@ Matrix mul­ti­pli­ca­tion matmul(a, b) -

The modulus operation is used by default to determine whether a number is even. This is because an even number divided by two has a remainder of zero. We define a cor­re­spond­ing Python function with the modulus operator:

def is_even(number):
    return number % 2 == 0
# test
assert is_even(8) and not is_even(7)

Matrix mul­ti­pli­ca­tion requires a package such as NumPy.

Com­par­i­son operators

The Python com­par­i­son operators indicate how two elements can be ordered with respect to each other. They give a Boolean result and are used es­pe­cial­ly for sorting al­go­rithms:

Python operator Meaning Operator function Example
< Less than lt(a, b) 3 < 1, 'a' < 'z'
> Greater than gt(a, b) 4 > 2, 'z' > 'a'
== Equals eq(a, b) 'a' == 'a'
!= Not equals ne(a, b) 1 ! = 2, 'Jim' != 'Jack'
<= Smaller equals le(a, b) 9 <= 10, 10 <= 10
>= Greater equals ge(a, b) 11 >= 10, 10 >= 10

Logical operators

Python’s logical “and” and “or” operators link multiple operands following Boolean logic. As a result, both operators return the last evaluated object. Python’s logical “not” operator in­ter­prets an object in the Boolean context and negates its truth value:

Python operator Meaning Operator function Example
and Logical AND no direct cor­re­spon­dence True and False == False, 'name' and ... == ...
or Logical OR no direct cor­re­spon­dence False or True == True, a = '' or 'Default'; assert a == 'Default'.
not Denial not_(a) not True == False

It is useful to visualize the effect of logical op­er­a­tions at truth tables. Below is the logical “AND”:

and True False
True True False
False False False

And the logical “OR”:

or True False
True True True
False True False

The operands of Python Boolean operators are by no means limited to Boolean variables. Any Python object can be in­ter­pret­ed in a Boolean context. The following objects evaluate to false in the Boolean context and are referred to as “falsy”:

Object Ex­pla­na­tion
False, None Constants which are False according to the de­f­i­n­i­tion
0, 0.0, Decimal(0), Fraction(0, 1), etc. Number which rep­re­sents zero
'', (), [], {}, set(), range(0), etc. Empty sequence or col­lec­tion

Bitwise operators

Python’s bitwise operators operate on integers, which are in­ter­pret­ed as bit sequences. They are all binary operators except for the bitwise “NOT” operator:

Python operator Meaning Operator function Example
<< Shift bit sequence to the left lshift(a, b) 5 << 3 == 5 * 2 ** 3
>> Shift bit sequence to the right rshift(a, b) 1 >> 1 == 0, 8 >> 1 == 4
& Combine two bit sequences by “AND” and_(a, b) ``
| Con­cate­nate two bit sequences by “OR” or_(a, b) ``
^ Linking two bit sequences using “XOR” xor(a, b) ``
~ Invert bit sequence with “NOT” invert(a) ``

The bitwise operators are suitable for optimized math­e­mat­i­cal op­er­a­tions. The left shift cor­re­sponds to a mul­ti­pli­ca­tion by a power of two:

Ex­pres­sion 23 = 8 22 = 4 21 = 2 20 = 1 Decimal
b = 6 0 1 1 0 6
b << 1 1 1 0 0 12
b >> 1 0 0 1 1 3

We have con­struct­ed a table of the in­di­vid­ual bits to il­lus­trate the bitwise “AND”, “OR”, and “NOT” op­er­a­tions. The op­er­a­tions are applied to a number in binary rep­re­sen­ta­tion using a bit mask:

Ex­pres­sion 23 = 8 22 = 4 21 = 2 20 = 1 Decimal
bits = 6 0 1 1 0 6
mask = 5 0 1 0 1 5
bits & mask 0 1 0 0 4
bits | mask 0 1 1 1 7
bits ^ mask 0 0 1 1 3

Python’s bitwise “NOT” operator inverts a bit sequence. This turns every 1 into a 0 and vice versa. Fur­ther­more, the number’s sign is reversed:

Ex­pres­sion 23 = 8 22 = 4 21 = 2 20 = 1 Decimal
b = 6 0 1 1 0 6
~ b 1 0 0 1 -7

As­sign­ment Operators

As­sign­ments are one of the basic state­ments in most pro­gram­ming languages. The Python as­sign­ment operators bind a value to a variable name. There is the newer “walrus” operator in addition to the as­sign­ment statement, which allows as­sign­ment within an ex­pres­sion. There are also several extended as­sign­ment state­ments which combine an as­sign­ment with another operation:

Python operator Meaning Operator function Example
= As­sign­ment statement no direct cor­re­spon­dence name = 'Walter'
:= As­sign­ment ex­pres­sion (“walrus” operator) no direct cor­re­spon­dence [ half for x in range(10) if (half := x / 2) < 5 ]
+= Advanced addition as­sign­ment iadd(a, b) x = 1; x += 4; assert x == 5

Python knows extended as­sign­ment operators for the arith­metic and bitwise op­er­a­tions. We do not list these in­di­vid­u­al­ly here. We simply show the general pattern using the extended con­cate­na­tion as­sign­ment as an example. A code which appends another part to an existing string is shown first:

name = 'Walter'
name = name + 'White'
assert name == 'WalterWhite'

An equiv­a­lent example using Python’s extended con­cate­na­tion operator “+=” yields the same result is obtained, but the code is more concise and ex­pres­sive:

name = 'Walter'
name += 'White'
assert name == 'WalterWhite'

Identity Operators

The Python is operator tests whether two variables refer to the same object in storage. Object identity contrasts with object equality, which is tested by the Python com­par­i­son operator “==”. The “is” in Python is similar to JavaScript’s strict equality operator “===”. Python also has a negated identity test with the “is not” operator:

Python operator Meaning Operator function Example
is Identity Test is_(a, b) a = 42; b = a; assert a is b
is not Negated Identity Test is_not(a, b) assert [42] is not [42]

Let’s look at a few examples. We create a reference to an object in storage. We create another reference as an alias later. The is operator returns true when both variables point to the same object in storage:

# assign value to name
a = [42]
# reference existing object
b = a
# if this holds
assert a is b
# so will this
assert a == b

We create two ref­er­ences to in­de­pen­dent objects in memory in the example below. Although the objects are the same, they are distinct. The is operator will return false in this case:

# assign the same value to different names
a = [42]
b = [42]
# `a`, `b` are two different objects
assert a is not b
# that contain the same value
assert a == b

Con­di­tion­al operator

Python’s con­di­tion­al operator can be used in place of the “if-else” keywords. The con­di­tion­al operator is popularly used to dis­tin­guish between two possible values in as­sign­ments. It is also known as a ternary operator, as the con­di­tion­al operator combines a condition and two ex­pres­sions.

Python operator Meaning Operator function Example
... if ... else ... Con­di­tion­al ex­pres­sion no direct cor­re­spon­dence name = 'Jim' if age == 42 else 'Jack'

Let’s look at an example using the if-else statement in Python first. The following code sets Celsius or Fahren­heit as the unit for a tem­per­a­ture mea­sure­ment depending on the chosen mea­sure­ment system:

if system == 'metric':
    unit = 'C'
else:
    unit = 'F

The code can be sim­pli­fied to a single as­sign­ment using the con­di­tion­al operator:

unit = 'C' if system == 'metric' else 'F'.

Set operators

In addition to strings, tuples, lists, and dic­tio­nar­ies, Python supports sets as a composite data type by default. Over­loaded operators are defined for the usual set op­er­a­tions:

Python operator Meaning Operator function Example
& Union of two sets and_(a, b) {'a', 'b'} & {'a', 'c'} == {'a'}
In­ter­sec­tion of two sets or_(a, b) {'a', 'b'} {'a', 'c'} == {'a', 'c', 'b'}
^ Form sym­met­ri­cal dif­fer­ence of two sets xor(a, b) {'a', 'b'} ^ {'a', 'c'} == {'c', 'b'}
- Dif­fer­ence of two sets sub(a, b) {'a', 'b'} - {'a'} == {'b'}
> Tests if quantity is a true superset gt(a, b) assert {'a', 'b'} > {'a'}
>= Tests if a set is a superset ge(a, b) assert {'a'} >= {'a'}
< Tests if quantity is a true subset lt(a, b) assert {'a'} < {'a', 'b'}
<= Tests if quantity is a subset le(a, b) assert {'a'} <= {'a'}

Mem­ber­ship Operators

The Python mem­ber­ship operators “in” and “not in” provide an in­di­ca­tion of whether an object is included in a col­lec­tion.

Python operator Meaning Operator function Example
in Tests whether an object is contained in an iterable contains(a, b) 'y' in 'Python'
not in Negation of the in operator not contains(a, b) 'x' not in 'Python'

Mem­ber­ship operators work with iterables and use an equality check to determine if the target object is in the col­lec­tion:

'Py' in 'Python'
'Px' not in 'Python'
'Jack' in ['Jim', 'Jack']

Using the “in” operator saves writing code of the following form:

def my_in(target, collection):
    for element in collection:
        if element == target:
            return true
    return False
# test
word = 'Python'
letter = 'y'
assert (my_in(letter, word)) == (letter in word)

Con­cate­na­tion operator

The con­cate­na­tion operator is used to con­cate­nate sequences of the same type. The operator symbol is the plus sign.

Python operator Meaning Operator function Example
+ Links two sequences add(a, b) ['Jim'] + ['Jack', 'John']

Let’s look at a few examples. We con­cate­nate two strings, two lists and two tuples:

assert "Walter" + "White" == 'WalterWhite'
assert ['a', 'b'] + ['c'] == ['a', 'b', 'c']
assert ('q', 'r') + ('s', 't') == ('q', 'r', 's', 't')

Python is used as an internet pro­gram­ming language. The con­cate­na­tion operator can be used to assemble HTML tags:

site_title = 'Welcome'
print('<h1>' + site_title + '</h1>')

The con­cate­na­tion operators differ in Python and PHP. This is because PHP uses the dot “.” as a symbol. The same example is shown below in PHP:

$siteTitle = 'Welcome';
echo '<h1>' . $siteTitle . '</h1>';

Index and slice operators

The Python index operator is used to extract a specific element of a col­lec­tion. The slice operator is used to extract a sub­se­quence.

Python operator Meaning Operator function Example
iterable[index] Returns the element of an iterable located under the index. getitem(iterable, index) 'Python'[1] == 'y'
sequence[start:stop:step] Returns a slice of a sequence getitem(iterable, slice(start, stop, step)) 'Python'[0:1] == 'Py', 'Python'[0:-1:2] == 'Pto'

Python’s index and slice operators revert to Dunder’s __getitem__() method. This is enabled with either a numeric index or a slice object:

names = ['Jim', 'Jack', 'John']
names[0] == names.__getitem__(0)
names[0:2] == names.__getitem__(slice(0, 2))

The slice operator is handy because it allows a sub­se­quence to be extracted without using a Python for-loop or Python while-loop. This saves pro­gram­mers from writing code in the following form:

word = 'Python'
start, stop, step = 0, 5, 2
index, substring = start, ''
while index in range(start, stop, step):
    substring += word[index]
    index += step
# test
assert substring == word[start:stop:step]
Go to Main Menu