There are two different classes of operators(or expressions): numeric operators and non-numeric operators. Since in Python even basic numeric types like integers and floats are implemented using classes the operators are implemented by operator overloading. Once we have studied classes we will learn how to implement operators ourselves or change the meaning of the existing ones. There are prerdefined methods for each operator to implement the associated functionality. Following text uses the word object a lot. Typical meaning of an object is an instance of a class. The grammar is expressed in form of extended BNF. Operators and expressions are classified in different categories which are explained one by one below(most of this theory follows from language reference of Python for accuracy; I have only tried to explain with my commentary and examples.):
When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type”, this means that the operator implementation for built-in types works as follows:
Atoms are the most basic elements of expressions. The simplest atoms are identifiers or literals. Forms enclosed in parentheses, brackets or braces are also categorized syntactically as atoms. The syntax for atoms is:
atom ::= identifier | literal | enclosure enclosure ::= parenth_form | list_display | dict_display | set_display | generator_expression | yield_atom
An identifier occurring as an atom is a name. See Section 2.6, “Identifiers or Names”.
When the name is bound to an object, evaluation of the atom yields that object. When a name is not bound, an attempt to
evaluate it raises a NameError
exception.
Private name mangling: When an identifier that textually occurs in a class definition
begins with two or more underscore characters and does not end in two or more underscores, it is considered a private name
of that class. Private names are transformed to a longer form before code is generated for them. The transformation inserts
the class name, with leading underscores removed and a single underscore inserted, in front of the name. For example, the
identifier __spam__spam
occurring in a class named Ham will be transformed to
_Ham__spam
. This transformation is independent of the syntactical context in which the identifier is
used. If the transformed name is extremely long (longer than 255 characters), implementation defined truncation may
happen. If the class name consists only of underscores, no transformation is done.
a = 1 b = 2 name = 'Shiv Dayal' l = [1, 2, 3] d = {"key1": "value1", 1: 2, (1, 2): "a tuple"} a = 100
Notice how a value is used to initialize a variable. Here a, b, name, l
and d
are
examples of an identifier. These are also called variables(simply because they can take up another value like
a
was 1
at the start but 100
at the end.)
A parenthesized form is an optional expression list enclosed in parentheses:
parenth_form ::= "(" [starred_expression] ")"
A parenthesized expression list yields whatever that expression list yields: if the list contains at least one comma, it yields a tuple(we will see these later); otherwise, it yields the single expression that makes up the expression list.
An empty pair of parentheses yields an empty tuple object. Since tuples are immutable, the same rules as for literals apply (i.e., two occurrences of the empty tuple may or may not yield the same object).
Note that tuples are not formed by the parentheses, but rather by use of the comma operator. The exception is the empty tuple, for which parentheses are required — allowing unparenthesized "nothing" in expressions would cause ambiguities and allow common typos to pass uncaught.
For constructing a list, a set or a dictionary Python provides special syntax called “displays”, each of them in two flavors(we will see these in their own chapters):
Common syntax elements for comprehensions are:
comprehension ::= assignment_expression comp_for comp_for ::= ["async"] "for" target_list "in" or_test [comp_iter] comp_iter ::= comp_for | comp_if comp_if ::= "if" or_test [comp_iter]
The comprehension consists of a single expression followed by at least one for clause and zero or more for or if
clauses. In this case, the elements of the new container are those that would be produced by considering each of the
for
or if
clauses a block, nesting from left to right, and evaluating the expression to
produce an element each time the innermost block is reached.
However, aside from the iterable expression in the leftmost for clause, the comprehension is executed in a separate implicitly nested scope. This ensures that names assigned to in the target list don't "leak" into the enclosing scope.
The iterable expression in the leftmost for
clause is evaluated directly in the enclosing scope and then
passed as an argument to the implicitly nested scope. Subsequent for clauses and any filter condition in the leftmost for
clause cannot be evaluated in the enclosing scope as they may depend on the values obtained from the leftmost iterable. For
example: [x*y for x in range(10) for y in range(x, x+10)]
.
To ensure the comprehension always results in a container of the appropriate type, yield
and yield
from
expressions are prohibited in the implicitly nested scope.
Since Python 3.6, in an async def
function, an async for
clause may be used to iterate
over a asynchronous iterator
. A comprehension in an async def
function may consist of
either a for
or async for
clause following the leading expression, may contain additional
for or async for
clauses, and may also use ???. If a comprehension
contains either async for
clauses or await
expressions it is called an asynchronous
comprehension. An asynchronous comprehension may suspend the execution of the coroutine function in which it appears. See
also PEP 530.
A list display is a possibly empty series of expressions enclosed in square brackets:
list_display ::= "[" [starred_list | comprehension] "]"
A list display yields a new list object, the contents being specified by either a list of expressions or a comprehension. When a comma-separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a comprehension is supplied, the list is constructed from the elements resulting from the comprehension.
For example, l = [1, 2, 3]
or l = [x for x in range(1, 4)]
, both will give same list. The
first is using a list of expressions and second is using a comprehension.
A set display is denoted by curly braces and distinguishable from dictionary displays by the lack of colons separating keys and values:
set_display ::= "{" (starred_list | comprehension) "}"
A set display yields a new mutable set object, the contents being specified by either a sequence of expressions or a comprehension. When a comma-separated list of expressions is supplied, its elements are evaluated from left to right and added to the set object. When a comprehension is supplied, the set is constructed from the elements resulting from the comprehension.
An empty set cannot be constructed with {}
; this literal constructs an empty dictionary. To create an
empty set you can call the constructor like so s = set()
A set can be created like s = {1, 2, 3}
or s = {x for x in range(1, 4)}
the first is done
using expressions and second is done using comprehension; both giving same set.
A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces:
dict_display ::= "{" [key_datum_list | dict_comprehension] "}" key_datum_list ::= key_datum ("," key_datum)* [","] key_datum ::= expression ":" expression | "**" or_expr dict_comprehension ::= expression ":" expression comp_for
If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given.
A double asterisk **
denotes dictionary unpacking. Its operand must
be a mapping
(A container object that supports arbitrary key lookups and
implements the methods specified in the or
MutableMapping
abstract base classes.). Each
mapping item is added to the new
dictionary. Later values replace values already set by earlier key/datum pairs and earlier dictionary unpackings.
A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual "for" and "if" clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced.
The key must be hashable(only immutable objects are hashable in Python, we will see what are immutable types later on). Clashes between duplicate keys are not detected; the last datum (textually rightmost in the display) stored for a given key value prevails.
A generator expression is a compact generator notation in parentheses(we will study these in more detail later on):
generator_expression ::= "(" expression comp_for ")"
A generator expression yields a new generator object. Its syntax is the same as for comprehensions, except that it is enclosed in parentheses instead of brackets or curly braces.
Variables used in the generator expression are evaluated lazily when the __next__()
method is called for
the generator object (in the same fashion as normal generators). However, the iterable expression in the leftmost for
clause is immediately evaluated, so that an error produced by it will be emitted at the point where the generator
expression is defined, rather than at the point where the first value is retrieved. Subsequent for clauses and any filter
condition in the leftmost for clause cannot be evaluated in the enclosing scope as they may depend on the values obtained
from the leftmost iterable. For example: (x*y for x in range(10) for y in range(x, x+10))
.
The parentheses can be omitted on calls with only one argument. See section ??? for details.
To avoid interfering with the expected operation of the generator expression itself, yield
and
yield from
expressions are prohibited in the implicitly defined generator.
If a generator expression contains either async for
clauses or await
expressions it is
called an asynchronous generator expression. An asynchronous generator expression
returns a new asynchronous generator object, which is an asynchronous iterator.
Primaries represent the most tightly bound operations of the language. Their syntax is:
primary ::= atom | attributeref | subscription | slicing | call
An attribute reference is a primary followed by a period and a name:
attributeref ::= primary "." identifier
The primary must evaluate to an object of a type that supports attribute references, which most objects do. This object is
then asked to produce the attribute whose name is the identifier. This production can be customized by overriding the
__getattr__()
method. If this attribute is not available, the exception AttributeError
is
raised. Otherwise, the type and value of the object produced is determined by the object. Multiple evaluations of the same
attribute reference may yield different objects.
We will see examples of this once we are finished with statements and move on to study strings. Once we study classes we
will see how to override __getattr__()
function for a class.
The subscription of an instance of a container class will generally select an element from the container. The subscription of a generic class will generally return a GenericAlias object.
subscription ::= primary "[" expression_list "]"
When an object is subscripted, the interpreter will evaluate the primary and the expression list.
The primary must evaluate to an object that supports subscription. An object may support subscription through defining one
or both of __getitem__()
and __class_getitem__()
. When the primary is subscripted, the
evaluated result of the expression list will be passed to one of these methods. For more details on when
__class_getitem__
is called instead of __getitem__
.
If the expression list contains at least one comma, it will evaluate to a tuple
containing the items of
the expression list. Otherwise, the expression list will evaluate to the value of the list's sole member.
For built-in objects, there are two types of objects that support subscription via __getitem__()
:
dict
class.
sequence
, the expression list must evaluate to an int
or
a slice
(as discussed in the following section). Examples of builtin sequence classes include the
str, list
and tuple
classes.
The formal syntax makes no special provision for negative indices in sequences. However, built-in sequences all provide a
__getitem__()
method that interprets negative indices by adding the length of the sequence to the index so
that, for example, x[-1]
selects the last item of x
. The resulting value must be a
nonnegative integer less than the number of items in the sequence, and the subscription selects the item whose index is
that value (counting from zero). Since the support for negative indices and slicing occurs in the object's
__getitem__()
method, subclasses overriding this method will need to explicitly add that support.
A string
is a special kind of sequence whose items are characters. A character is not a separate data type
but a string of exactly one character.
Suppose we have a list l = [1, 2, 3]
, then l[0] = l[-3] = 1, l[1] = [-2], = 2
and
l[2] = l[-1] = 3
. Also, consider an example dictionary d = {"k1": "v1", "k2": "v2"}
then
d["k1"] = "v1"
and so on.
A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as
expressions or as targets in assignment or del
statements. The syntax for a slicing:
slicing ::= primary "[" slice_list "]" slice_list ::= slice_item ("," slice_item)* [","] slice_item ::= expression | proper_slice proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" [stride] ] lower_bound ::= expression upper_bound ::= expression stride ::= expression
There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice).
The semantics for a slicing are as follows. The primary is indexed (using the same __getitem__()
method as normal
subscription) with a key that is constructed from the slice list, as follows. If the slice list contains at least one
comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is
the key. The conversion of a slice item that is an expression is that expression. The conversion of a proper slice is a
slice object (see ???) whose start, stop and step attributes are the values of the
expressions given as lower bound, upper bound and stride, respectively, substituting None for missing expressions.
Consider a string; s = "Hello world!"
then there are various slicings possible. s[0:]
will present whole string. Similarly s[:0]
will be empty string. s[0:5]
will give
"Hello"
because the range is closed on the left side and open on right side i.e. 0
is
included but 5
is excluded. Also, s[0:5:2]
will give "Hlo"
i.e. every
alternate character because step is of 2
characters. Note that s[:-1]
strips the last
character because the interval is open on right side of range so -1
index will not be included.
Given below is the grammar for these:
m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr | m_expr "//" u_expr | m_expr "/" u_expr | m_expr "%" u_expr a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr
u_expr
means unary expressions for unary operators. m_expr
is expression for multiplicative
operators and a_expr
is expression for arithmetic operators. These operators have conventional priority.
The operators in this section are +, -, *, @, /
and //
(floor division
operator). @
is an exceptional operator because no built-in class of Python implements this and it is meant
for matrix multiplication. Floor division means quotient will be floored i.e. mathematical floor operator will be applied
which means that the integral part of quotient will be the final result. Let us see these opperators in action in following
program:
a = 5 b = 10 c = 4 print(a + b) print(a - c) print(a * b) print(a * (b + c)) print(a * (b - c)) print(a / b) print(a // b) print((a + b)/c) print((a + b)//c) print(a * b + c) print(a/b + c) print(a%c)
The output is given below:
15
1
50
70
30
0.5
0
3.75
3
54
4.5
1
Thus, you can see how the conventional operator precedence works and how you can use parentheses()
to
override that precedence.
The +
(addition) operator produces sum of its arguments. The arguments must be both numbers or sequence of
same types(for example, a list
or a string of str
class). When both are numbers the result
is sum else it will be concatenation of sequences. The functions which allow this operator for a class are
__add__()
and __radd__()
i.e. if you implement these methods then that classes' objects
will be able to use this operator.
The -
(subtraction) operator produces the difference of its arguments. The numeric arguments are first
converted to a common type. The __sub__()
overloaded method of a class controls the implementation of
the operator.
The *
(multiplication) operator produces product of its arguments if both are numeric. The first argument is
always numeric. The second argumant can be numeric or a sequence. In second argument is a sequence then sequence repitition
is performed, a negative as first argument and a sequence as second argument produces an empty sequence. The
__mul__()
and __rmul()__
overloaded method of a class controls the implementation of the
operator.
The /
(division) and //
(floor division) operators produce the quotient of their
arguments. The numeric arguments are first converted to a common type. Division of integers produces float, while floor
division produces an integer as explained above. Division by zero will raise ZeroDivisionError
exception. These operation are controlled by __truediv__()
and __floordiv__()
overloaded
methods for the class of arguments.
The %
(modulo) operator yields the remmainder from the division of the first argument by the second. Like
other operators the numeric types are first converted to a common type. If the second argument is zero then like division and
floor division operators ZeroDivisionError
execption is raised. This operator always yields a result with
the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value
of the second operand.
The floor division and modulo operators are connected by the following identity: x == (x//y)*y +
(x%y)
. Floor division and modulo are also connected with the built-in function divmod(): divmod(x, y) ==
(x//y, x%y)
.
In addition to performing the modulo operation on numbers, the %
operator is also overloaded by string
objects to perform old-style string formatting (also known as interpolation). The syntax for string formatting is described
in the Python Library Reference, section printf-style String Formatting. Like other operators this operator is also
controlled by an overloaded function which is __mod__()
.
The floor division operator, the modulo operator, and the divmod()
function are not defined for complex numbers.
I am introducing list and strings to explain some of the operators discussed here. A list
is a
collection/sequence of heterogeneous objects i.e. these can contain objects of varying type. A list
is an
iterable i.e. it can be iterated or specifically it implements __next__()
function. It uses square
brackets in syntax and elements are separated by commas. An example is: l = [1, 2, 3.5, 'a', 'b']
. It
supports a lot of functions to operate with.
A string in simplest form is sequence of unicode characters. For example: s = 'My heart is ðŸ’”'
. Like a
list
string supports a lot of functions to operate with. A string is also an itearble. I will not go in
much detail about strings and lists here. This introduction is just for examples to work with.
An example is given below:
l1 = [1, 2, 3] l2 = [4, 5, 6] s1 = 'Hello ' s2 = 'world!!!' print(l1 + l2) print(s1 + s2) print(2*l1) print(2*s1) print(-2*s1) print(-2*l1)
The output is given below:
[1, 2, 3, 4, 5, 6]
Hello world!!!
[1, 2, 3, 1, 2, 3]
Hello Hello
[]
Note that -2*s1
will yield an empty string which is an empty line in output. Specifically, I would like to
draw your attention to sequence repitition.
The grammar is given below:
u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr
All unary arithmetic and bitwise operations have the same priority. The unary -
(minus) operator yields the
negation of its numeric argument; the __neg__()
method of the class can be used to overload this operator
and customize it.
The unary +
(plus) operator yields its numeric argument unchanged; the __pos__()
method of
the class can be used to overload this operator and customize it.
The unary ~
(invert) operator yields the bitwise inversion of its integer argument. The bitwise inversion of
x
is defined as -(x+1)
. It is applicable only to integral numbers or by those classes which
implement the __invert__()
overloaded method. This is also called bit negation or bit flipping and is best
understood by 2's complement.
An example is given below:
a = -5 b = 4 print(-a) print(+a) print(~b)
The output is:
5
-5
-5
The grammar is:
power ::= (await_expr | primary) ["**" u_expr]
This operator binds more tightly than unary operator on its left; and less tightly on its right. Thus, in an unparenthesized
sequence of power and unary operators, the operators are evaluated from right to left (this does not constrain the evaluation
order for the operands): -1**2
results in -1
. In this case first power calculation is done
and then unary negation operator is applied.
The power operator has the same semantics as the built-in pow()
function, when called with two arguments: it
yields its left argument raised to the power of its right argument. The numeric arguments are first converted to a common
type, and the result is of that type.
For int operands, the result has the same type as the operands unless the second argument is negative; in that case, all
arguments are converted to float and a float result is delivered. For example, 10**2
returns
100
, but 10**-2
returns 0.01
.
Raising 0.0
to a negative power results in a ZeroDivisionError
. Raising a negative number
to a fractional power results in a complex
number. (In earlier versions it raised a
ValueError
.)
If you want to implement this yourself for a class then you need to implement __pow__()
function for the
respective class.
Given below are some examples:
print(-1**2) print((-1)**2) print(2**2) print((-2)**2) print(-2**2) print(2**100)
The output is:
-1
1
4
4
-4
1267650600228229401496703205376
The grammar is:
shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr
These operators have lower priority than arithmetic operators. In CPU there are special shift registers which perform this operation. The case of shift registers is especially true for fixed type integers in languages like C/C++ where the variable fits exactly in one register.
These operators accept only integers as arguments. They shift the first argument to left and right by the number of bits
given by second argument. The second argument cannot negative and if negative will raise a ValueError
exception. Note that -1
cannot be right shifted and it does not matter what second argument
you supply its result will remain -1
. A right shift by n
bits is defined as floor division
by pow(2,n)
. A left shift by n bits is defined as multiplication with pow(2,n)
.
You can implement __lshift__()
and __rshift__()
to implement the functionality for these
functions.
An example is give below:
a = 2 print(a << 2) print(a >> 2) print(-1 << 10) print(-1 >> 10) # special case in 2's complement cannot be right shifted
The output is:
8
0
-1024
-1
The grammar is:
and_expr ::= shift_expr | and_expr "&" shift_expr xor_expr ::= and_expr | xor_expr "^" and_expr or_expr ::= xor_expr | or_expr "|" xor_expr
The three operators &, ^
and |
represent three logic gates, namely AND, XOR or
EX-OR
and OR
gate. NOT
gate is a comparison operator and can be used with
AND
and OR
operators to get NAND
and NOR
behavior.
The &
operator yields the bitwise AND of its arguments, which must be integers or one of them must be a
custom object overriding __and__()
or __rand__()
special methods.
The ^
operator yields the bitwise XOR (exclusive OR) of its arguments, which must be integers or one of them
must be a custom object overriding __xor__()
or __rxor__()
special methods.
The |
operator yields the bitwise (inclusive) OR of its arguments, which must be integers or one of them
must be a custom object overriding __or__()
or __ror__()
special methods.
An example is given below:
a = 0 b = 1 print(a & b) print(a | b) print(a ^ b) print(a & a) print(a | a) print(a ^ a) print(b & b) print(b | b) print(b ^ b)
The output is:
0
1
1
0
0
0
1
1
0
XOR or EXOR can be used to swap(exchange) values of two variables without using a third operator. Let us first see the program to change the value using a third variable.
a = 5 b = 7 tmp = a a = b b = tmp print(a, b)
The output is:
7 5
The above program is a very simple program which uses a temporary variable to hold the value of variable
a
. Now we will do this exchange without temporary variable.
And the output is:a = 5 b = 7 a = a^b b = a^b a = a^b print(a, b)
7 5
I will leave it to you to use pen and paper to figure out the output.
The grammar is:
comparison ::= or_expr (comp_operator or_expr)* comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!=" | "is" ["not"] | ["not"] "in"
All comparison operators in Python have same priority, which is lower than any arithmetic, shifting or bitwise
operators. Unlike C, expressions like a < b < c
have the interpretation that is conventional in
mathematics.
Like C, comparisons yield boolean values: True
or False
. Custom rich comparison methods may return non-boolean values. In this case Python will call
bool()
on such value in boolean contexts.
Comparisons can be chained arbitrarily, e.g., x < y <= z
is equivalent to x < y and y <=
z
, except that y
is evaluated only once (but in both cases z
is not evaluated at
all when x < y
is found to be false).
While it is possible to chain the operators it is best not to do that in favor of readability and maintainability. Also,
whenever possible you should use parentheses to explicitly mark the desired operational order. This will help other
programmers who read your code with ease of understanding. For exmaple if you want to evaluate the last example then you can
either write (x < y) < z
or x < (y <= z)
. This grouping makes it much easier to
understand the code.
Formally, if a, b, c, …, y, z
are expressions and op1, op2, …, opN
are comparison
operators, then a op1 b op2 c ... y opN z
is equivalent to a op1 b and b op2 c and ... y opN
z
, except that each expression is evaluated at most once.
Note that a op1 b op2 c
doesnt imply any kind of comparison between a
and
c
, so that, e.g., x < y > z
is perfectly legal (though perhaps not pretty).
An example program is given below:
a = 1 b = 2 print(a < b) print(a > b) print(a == b) print(a >= b) print(a <= b) print(a != b) print(a is a) print(a is not a) l = [1, 2, 3] print(1 in l) print(1 not in l)
The output is:
True
False
False
False
True
True
True
False
True
False
The operators <, >, ==, >=, <=
and !=
compare the values of two objects. The
objects do not need to have the same type.
In Python, objects have a value (in addition to type and identity). The value of an object is a rather abstract notion in Python: For example, there is no canonical access method for an object’s value. Also, there is no requirement that the value of an object should be constructed in a particular way, e.g. comprised of all its data attributes. Comparison operators implement a particular notion of what the value of an object is. You can think of them as defining the value of an object indirectly, by means of their comparison implementation.
Because all types are (direct or indirect) subtypes of object
, they inherit the default comparison
behavior from object
. Types can customize their comparison behavior by implementing rich comparison
methods like __lt__()
.
The default behavior for equality comparison (==
and !=
) is based on the identity of the
objects. Hence, equality comparison of instances with the same identity results in equality, and equality comparison of
instances with different identities results in inequality. A motivation for this default behavior is the desire that all
objects should be reflexive (i.e. x is y
implies x == y
).
A default order comparison (<, >, <=
and >=
) is not provided; an attempt
raises TypeError
. A motivation for this default behavior is the lack of a similar invariant as for
equality.
The behavior of the default equality comparison, that instances with different identities are always unequal, may be in contrast to what types will need that have a sensible definition of object value and value-based equality. Such types will need to customize their comparison behavior, and in fact, a number of built-in types have done that.
The following list describes the comparison behavior of the most important built-in types.
Numbers of built-in numeric types (Numeric Types — int, float, complex) and of the standard library types
fractions.Fraction
and decimal.Decimal
can be compared within and across their types,
with the restriction that complex numbers do not support order comparison. Within the limits of the types involved,
they compare mathematically (algorithmically) correct without loss of precision.
The not-a-number values float('NaN')
and decimal.Decimal('NaN')
are special. Any
ordered comparison of a number to a not-a-number value is false. A counter-intuitive implication is that not-a-number
values are not equal to themselves. For example, if x = float('NaN'), 3 < x, x < 3
and x
== x
are all false, while x != x
is true. This behavior is compliant with IEEE 754.
None
and NotImplemented
are singletons. PEP 8 advises that comparisons for
singletons should always be done with is or is not, never the equality operators.
bytes
or bytearray
) can be compared within and across
their types. They compare lexicographically using the numeric values of their elements.
str
) compare lexicographically using the numerical Unicode code points (the result
of the built-in function ord()
) of their characters.
Strings and binary sequences cannot be directly compared.
tuple, list
or range
) can be compared only within each of their
types, with the restriction that ranges do not support order comparison. Equality comparison across these types results
in inequality, and ordering comparison across these types raises TypeError
.
[1,2] == (1,2)
is false because the type is
not the same).
[1,2,x] <= [1,2,y]
has the same value as x <= y
). If a corresponding element
does not exist, the shorter collection is ordered first (for example, [1,2] < [1,2,3]
is true).
dict
) compare equal if and only if they have equal (key, value) pairs. Equality
comparison of the keys and values enforces reflexivity.
Order comparisons (<, >, <=
and >=
) raise TypeError
.
set
or frozenset
) can be compared within and across their types.
They define order comparison operators to mean subset and superset tests. Those relations do not define total orderings
(for example, the two sets {1,2}
and {2,3}
are not equal, nor subsets of one another,
nor supersets of one another). Accordingly, sets are not appropriate arguments for functions which depend on total
ordering (for example, min(), max()
and sorted()
produce undefined results given a
list of sets as inputs).
Comparison of sets enforces reflexivity of its elements.
User-defined classes that customize their comparison behavior should follow some consistency rules, if possible:
x is y
implies x == y
x == y
and y == x
x != y
and y != x
x < y
and y > x
x <= y
and y >= x
x > y and y > z
implies x > z
x < y and y <= z
implies x < z
x == y
and not x != y
x < y
and not x >= y
(for total ordering)
x > y
and not x <= y
(for total ordering)
The last two expressions apply to totally ordered collections (e.g. to sequences, but not to sets or mappings). See
also the total_ordering()
decorator.
hash()
result should be consistent with equality. Objects that are equal should either have the same
hash value, or be marked as unhashable.
Python does not enforce these consistency rules. In fact, the not-a-number values are an example for not following these rules.
The operators in
and not in
test for membership. x in s
evaluates to
True
if x
is a member of s
, and False
otherwise. x not in s
returns the negation of x
in s
. All built-in
sequences and set types support this as well as dictionary, for which in tests whether the dictionary has a given key. For
container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y
is
equivalent to any(x is e or x == e for e in y
).
For the string and bytes types, x in y
is True
if and only if x
is a
substring of y
. An equivalent test is y.find(x) != -1
. Empty strings are always
considered to be a substring of any other string, so ""
in "abc"
will return
True
.
For user-defined classes which define the __contains__()
method, x in y
returns
True
if y.__contains__(x)
returns a true value, and False
otherwise.
For user-defined classes which do not define __contains__()
but do define __iter__()
,
x in y
is True
if some value z
, for which the expression x is
z
or x == z
is true, is produced while iterating over y
. If an exception is
raised during the iteration, it is as if in
raised that exception.
Lastly, the old-style iteration protocol is tried: if a class defines __getitem__()
, x in
y
is True
if and only if there is a non-negative integer index i
such that
x is y[i]
or x == y[i]
, and no lower integer index raises the IndexError
exception. (If any other exception is raised, it is as if in raised that exception).
The operator not in
is defined to have the inverse truth value of in
.
The operators is
and is not
test for an objects identity: x is y
is true
if and only if x
and y
are the same object. Identity of an object is determined using the
id()
function. x is not y
yields the inverse truth value.
The output is:x = 1 print(id(x)) print(id(1)) print(x is 1) # even though both the ids are same you should not use is operator with a literal
9788960
9788960
True
Note that id
value can be different on your system.
The grammar is:
or_test ::= and_test | or_test "or" and_test and_test ::= not_test | and_test "and" not_test not_test ::= comparison | "not" not_test
In the context of Boolean operations(these are logical operators not bitwise), and also when expressions are used by
control flow statements, the following values
are interpreted as false: False, None,
numeric zero of all types, and empty strings and containers
(including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as
true. User-defined objects can customize their truth value by providing a __bool__()
method.
The operator not
yields True
if its argument is false, False
otherwise.
The expression x and y
first evaluates x
; if x
is false, its value is
returned; otherwise, y
is evaluated and the resulting value is returned. This is known as short-circuit evalation and is also applied for or
operator.
The expression x or y
first evaluates x
; if x
is true, its value is
returned; otherwise, y
is evaluated and the resulting value is returned.
Note that neither and
nor or
restrict the value and type they return to False and True, but
rather return the last evaluated argument. This is sometimes useful, e.g., if s
is a string that should be
replaced by a default value if it is empty, the expression s or 'foo'
yields the desired value. Because
not
has to create a new value, it returns a boolean value regardless of the type of its argument (for
example, not 'foo'
produces False
rather than ''
.)
An example is given below:
a = 1 b = 2 c = 0 * print(a and b) hprint(a and c) print(a or b) print(a or c) print(not a) print(not c) print('' or 'hello')
The output is:
2
0
1
1
False
True
hello
The grammar is:
assignment_expression ::= [identifier ":="] expression
An assignment expression (sometimes also called a “named expression” or “walrus”) assigns an expression to an identifier, while also returning the value of the expression.
One common use case is when handling matched regular expressions:
if matching := pattern.search(data): do_something(matching)
Or, when processing a file stream in chunks:
while chunk := file.read(9000): process(chunk)
New in version 3.8: See PEP 572 for more details about assignment expressions.(We will discuss this in further detail later on.)
The grammar is:
conditional_expression ::= or_test ["if" or_test "else" expression] expression ::= conditional_expression | lambda_expr
Conditional expressions (sometimes called a “ternary operator”) have the lowest priority of all Python operations.
The expression x if C else y
first evaluates the condition, C
rather than
x
. If C
is true, x
is evaluated and its value is returned; otherwise,
y
is evaluated and its value is returned.
See PEP 308 for more details about conditional expressions.
We will study this in more detail once we have covered if-else statements in next chapter.
The grammar is:
lambda_expr ::= "lambda" [parameter_list] ":" expression
Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda
parameters: expression
yields a function object. The unnamed object behaves like a function object defined with:
def <lambda>(parameters): return expression
Note that functions created with lambda expressions cannot contain statements or annotations. We will study lambdas after we have studied functions.
The grammar is:
expression_list ::= expression ("," expression)* [","] starred_list ::= starred_item ("," starred_item)* [","] starred_expression ::= expression | (starred_item ",")* [starred_item] starred_item ::= assignment_expression | "*" or_expr
Except when part of a list or set display, an expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
An asterisk *
denotes iterable unpacking. Its operand must be an
iterable
. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or
set, at the site of the unpacking.
The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A
single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create
an empty tuple, use an empty pair of parentheses: ()
.)
Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.
In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:
expr1, expr2, expr3, expr4 (expr1, expr2, expr3, expr4) {expr1: expr2, expr3: expr4} expr1 + expr2 * (expr3 - expr4) expr1(expr2, expr3, *expr4, **expr5) expr3, expr4 = expr1, expr2
The following table summarizes the operator precedence in Python, from highest precedence (most binding) to lowest precedence (least binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left).
Operator | Description |
---|---|
(expressions...), [expressions...], {key: value...}, {expressions...} |
Binding or parenthesized expression, list display, dictionary display, set display |
x[index], x[index:index], x(arguments...), x.attribute |
Subscription, slicing, call, attribute reference |
await x |
Await expression |
** |
Exponentiation |
+x, -x, ~x |
Positive, negative, bitwise NOT |
*, @, /, //, % |
Multiplication, matrix mutliplication, division, floor division, remainder |
+, - |
Addition, subtraction |
<<,>> |
Shift |
& |
Bitwise AND |
^ |
Bitwise XOR or EX-OR |
| |
Bitwise OR |
in, not in, is, is not, <, <=, >, >=, !=, == |
Comparisons, including membership tests and identity tests |
not x |
Boolean or logical NOT |
and |
Boolean or logical AND |
or |
Bllean or logical OR |
if-else |
Conditional expression |
lambda |
Lambda expression |
:= |
Assignment expression |
© 2022 Shiv S. Dayal. www.ashtavakra.org. GNU FDL license v1.3 or later is applicable where not stated.