Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Python Programming_Operators - Wikibooks, Open Books for an Open World

Uploaded by

joejimchibuzor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Programming_Operators - Wikibooks, Open Books for an Open World

Uploaded by

joejimchibuzor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

Python Programming/Operators

Contents
Basics
Powers
Floor Division and True Division
Modulus
Negation
Comparison
Identity
Augmented Assignment
Logical Operators
or
and
not
Bitwise Operators
References
External Links

Basics
Python math works as expected:

>>> x = 2
>>> y = 3
>>> z = 5
>>> x * y
6
>>> x + y
5
>>> y - x
1
>>> x * y + z
11
>>> (x + y) * z
25
>>> 3.0 / 2.0 # True division
1.5
>>> 3 // 2 # Floor division
1
>>> 2 ** 3 # Exponentiation
8

Note that Python adheres to the PEMDAS order of operations.

https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 1/8


7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

Powers
There is a built in exponentiation operator **, which can take either integers, floating point or
complex numbers. This occupies its proper place in the order of operations.

>>> 2**8
256

Floor Division and True Division


In Python 3.x, slash operator ("/") does true division for all types including integers, and therefore,
e.g. 3/2==1.5[1]
[1][2]
[2]. The result is of a floating-point type even if both inputs are integers: 4 / 2
yields 2.0.

In Python 3.x and latest 2.x, floor division for both integer arguments and floating-point
arguments is achieved by using the double slash ("//") operator. For negative results, this is unlike
the integer division in the C language since -3 // 2 == -2 in Python while -3 / 2 == -1 in C: C
rounds the negative result toward zero while Python toward negative infinity.

Beware that due to the limitations of floating point arithmetic, rounding errors can cause
unexpected results. For example:

>>> print(0.6/0.2)
3.0
>>> print(0.6//0.2)
2.0

For Python 2.x, dividing two integers or longs using the slash operator ("/") uses floor division
(applying the floor function after division) and results in an integer or long. Thus, 5 / 2 == 2 and -3
/ 2 == -2. Using "/" to do division this way is deprecated; if you want floor division, use "//"
(available in Python 2.2 and later). Dividing by or into a floating point number will cause Python to
use true division. Thus, to ensure true division in Python 2.x: x=3; y=2; float(x)/y == 1.5.

Links:

6.7. Binary arithmetic operations (https://docs.python.org/3/reference/expressions.html#binary-


arithmetic-operations), in The Python Language Reference, docs.python.org
Python-style integer division & modulus in C (https://stackoverflow.com/questions/828092/pyth
on-style-integer-division-modulus-in-c?noredirect=1&lq=1), stackoverflow.com
Integer division rounding with negatives in C++ (https://stackoverflow.com/questions/319880/int
eger-division-rounding-with-negatives-in-c), stackoverflow.com
In Python, what is a good way to round towards zero in integer division? (https://stackoverflow.
com/questions/19919387/in-python-what-is-a-good-way-to-round-towards-zero-in-integer-divisi
on), stackoverflow.com
Why Python's Integer Division Floors (http://python-history.blogspot.com/2010/08/why-pythons-
integer-division-floors.html), python-history.blogspot.com

Modulus
The modulus (remainder of the division of the two operands, rather than the quotient) can be
found using the % operator, or by the divmod builtin function. The divmod function returns a
tuple containing the quotient and remainder.
https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 2/8
7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

>>> 10 % 7
3
>>> -10 % 7
4

Note that -10 % 7 is equal to 4 while it is equal to -3 in the C language. Python calculates the
remainder as one after floor division while the C language calculates the remainder after C integer
division, which differs from Python's floor division on integers: C integer division rounds negative
results toward zero.

Links:

6.7. Binary arithmetic operations (https://docs.python.org/3/reference/expressions.html#binary-


arithmetic-operations), in The Python Language Reference, docs.python.org

Negation
Unlike some other languages, variables can be negated directly:

>>> x = 5
>>> -x
-5

Comparison
Operation Means
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

Numbers, strings and other types can be compared for equality/inequality and ordering:

>>> 2 == 3
False
>>> 3 == 3
True
>>> 3 == '3'
False
>>> 2 < 3
True
>>> "a" < "aa"
True

Identity
The operators is and is not test for object identity and stand in contrast to == (equals): x is y
is true if and only if x and y are references to the same object in memory. x is not y yields the
inverse truth value. Note that an identity test is more stringent than an equality test since two
distinct objects may have the same value.

https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 3/8


7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

>>> [1,2,3] == [1,2,3]


True
>>> [1,2,3] is [1,2,3]
False

For the built-in immutable data types (like int, str and tuple) Python uses caching mechanisms to
improve performance, i.e., the interpreter may decide to reuse an existing immutable object
instead of generating a new one with the same value. The details of object caching are subject to
changes between different Python versions and are not guaranteed to be system-independent, so
identity checks on immutable objects like 'hello' is 'hello', (1,2,3) is (1,2,3), 4 is
2**2 may give different results on different machines.

In some Python implementations, the following results are applicable:

print(8 is 8) # True
print("str" is "str") # True
print((1, 2) is (1, 2)) # False - whyever, it is immutable
print([1, 2] is [1, 2]) # False
print(id(8) == id(8)) # True
int1 = 8
print(int1 is 8) # True
oldid = id(int1)
int1 += 2
print(id(int1) == oldid)# False

Links:

3. Data model (https://docs.python.org/2/reference/datamodel.html), python.org


2. Built-in Functions # id (https://docs.python.org/2/library/functions.html#id), python.org
5. Expressions # is (https://docs.python.org/2/reference/expressions.html#is), python.org

Augmented Assignment
There is shorthand for assigning the output of an operation to one of the inputs:

>>> x = 2
>>> x # 2
2
>>> x *= 3
>>> x # 2 * 3
6
>>> x += 4
>>> x # 2 * 3 + 4
10
>>> x /= 5
>>> x # (2 * 3 + 4) / 5
2
>>> x **= 2
>>> x # ((2 * 3 + 4) / 5) ** 2
4
>>> x %= 3
>>> x # ((2 * 3 + 4) / 5) ** 2 % 3
1

>>> x = 'repeat this '


>>> x # repeat this
repeat this
>>> x *= 3 # fill with x repeated three times
>>> x
repeat this repeat this repeat this

https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 4/8


7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

Logical Operators
Logical operators are operators that act on booleans.

or

The or operator returns true if any one of the booleans involved are true. If none of them are true
(in other words, they are all false), the or operator returns false.

if a or b:
do_this
else:
do_this

and

The and operator only returns true if all of the booleans are true. If any one of them is false, the
and operator returns false.

if a and b:
do_this
else:
do_this

not

The not operator only acts on one boolean and simply returns its opposite. So, true turns into false
and false into true.

if not a:
do_this
else:
do_this

The order of operations here is: not first, and second, or third. In particular, "True or True and
False or False" becomes "True or False or False" which is True.

Warning, logical operators can act on things other than booleans. For instance "1 and 6" will
return 6. Specifically, "and" returns either the first value considered to be false, or the last value if
all are considered true. "or" returns the first true value, or the last value if all are considered false.

Bitwise Operators
Python operators for bitwise arithmetic are like those in the C language. They include & (bitwise
and), | (bitwise or), ^ (exclusive or AKA xor), << (shift left), >> (shift right), and ~ (complement).
Augmented assignment operators (AKA compound assignment operators) for the bitwise
operations include &=, |=, ^=, <<=, and >>=. Bitwise operators apply to integers, even negative
ones and very large ones; for the shift operators, the second operand must be non-negative. In the
Python internal help, this is covered under the topics of EXPRESSIONS and BITWISE.

Examples:

https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 5/8


7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

0b1101 & 0b111 == 0b101


Note: 0b starts a binary literal, just like 0x starts a hexadecimal literal.
0b1 | 0b100 == 0b101
0b111 ^ 0b101 == 0b10
1 << 4 == 16
7 >> 1 == 3
1 << 100 == 0x10000000000000000000000000
Large results are supported.
1 << -1
An error: the 2nd operand must be non-negative.
-2 & 15 == 14
For bitwise operations, negative integers are treated as if represented using two's
complement with an infinite sequence of leading ones. Thus -2 is as if 0x...FFFFFFFFFE,
which ANDed with 15 (0xF) yields 0xE, which is 14.
format(-2 % (1 << 32), "032b")
Determines a string showing the last 32 bits of the implied two's complement
representation of -2.
~-2 == 1
The above note about treatment of negative integers applies. For the complement (~), this
treatment yields ~x == -1 * (x + 1). This can be verified: min((~x == -1 * (x + 1) for x in
range(-10 ** 6, 10 ** 6))) == True.
~1 == -2
The formula ~x == -1 * (x + 1) mentioned above applies. In the bitwise complement
interpretation, all the imaginary leading leading zeros of 1 are toggled to leading ones,
which is then interpreted as two's complement representation of -2, which is as if
0x...FFFFFFFFFE.
x = 0b11110101; x &= ~(0xF << 1); x == 0b11100001
A common idiom clears the least significant bits 5 to 2 using complement, showing the
usefulness of the infinite-leading-ones two's complement implied representation of negative
numbers for bitwise operations. Works for arbitrarily large x.
~2 & 0xFFFFFFFF == 0xFFFFFFFD
We can emulate bitwise complement on 32-bit integers by ANDing the complement with the
maximum unsigned 32-bit integer, 0xFFFFFFFF. We can proceed similarly for 8-bit
complement, 16-bit complement, etc., by ANDing the complement with 0xFF, 0xFFFF, etc.
2 ^ 0xFFFFFFFF == 0xFFFFFFFD
Another way to emulate fixed-size bitwise complement is by XORing with all Fs for the size.
v = 2.0 & 2
Yields an error: no automatic conversion of floats to ints, and no operation on the
underlying representation of the float.
int.bit_length(0x8000) == 16
Determines how many bits are needed to represent the integer. Therefore,
min((int.bit_length(1 << x) == x + 1 for x in range(100000))) == True.

Examples of augmented assignment operators:

https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 6/8


7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

a = 0b1101; a &= 0b111; a == 0b101


a = 0b1; a |= 0b100; a == 0b101
a = 0b111; a ^= 0b101; a == 0b10
a = 1; a <<= 4; a == 16
a = 7; a >>= 1; a == 3

Class definitions can overload the operators for the instances of the class; thus, for instance, sets
overload the pipe (|) operator to mean set union: {1,2} | {3,4} == {1,2,3,4}. The names of the
override methods are __and__ for &, __or__ for |, __xor__ for ^, __invert__ for ~, __lshift__
for <<, __rshift__ for >>, __iand__ for &=, __ior_ for |=, __ixor__ for ^=, __ilshift__ for <<=,
and __irshift__ for >>=.

Examples of use of bitwise operations include calculation of CRC and MD5. Admittedly, these
would usually be implemented in C rather than Python for maximum speed; indeed, Python has
libraries for these written in C. Nonetheless, implementations in Python are possible and are
shown in the links to Rosetta Code below.

Links:

BitwiseOperators (https://wiki.python.org/moin/BitwiseOperators), wiki.python.org


BitManipulation (https://wiki.python.org/moin/BitManipulation), wiki.python.org
Bitwise Operations on Integer Types (https://docs.python.org/3/library/stdtypes.html#bitwise-op
erations-on-integer-types) in Library Reference, docs.python.org
2.5. Operators (https://docs.python.org/3/reference/lexical_analysis.html#operators) in The
Python Language Reference, docs.python.org
6. Expressions (https://docs.python.org/3/reference/expressions.html) in The Python Language
Reference, docs.python.org
3. Data model (https://docs.python.org/3/reference/datamodel.html) in in The Python Language
Reference, docs.python.org

"Integers (int) [...] For the purpose of shift and mask operations, a binary representation is
assumed, and negative numbers are represented in a variant of 2’s complement which
gives the illusion of an infinite string of sign bits extending to the left."

Bitwise operation, wikipedia.org


Bitwise operations in C, wikipedia.org
PEP 203 – Augmented Assignments (https://peps.python.org/pep-0203/), python.org
Bitwise operation and usage (https://stackoverflow.com/questions/1746613/bitwise-operation-a
nd-usage), stackoverflow.com
CRC-32 # Python (https://rosettacode.org/wiki/CRC-32#Python), rosettacode.org
MD5/Implementation # Python (https://rosettacode.org/wiki/MD5/Implementation#Python),
rosettacode.org
longobject.c (https://github.com/python/cpython/blob/main/Objects/longobject.c) at
python/cpython, github.com - functions long_bitwise, long_rshift, long_lshift, and long_invert

References
1. What's New in Python 2.2 (https://www.python.org/doc/2.2.3/whatsnew/node7.html)
2. PEP 238 -- Changing the Division Operator (https://www.python.org/dev/peps/pep-0238/)

External Links
https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 7/8
7/7/22, 7:10 PM Python Programming/Operators - Wikibooks, open books for an open world

3.1.1. Numbers (https://docs.python.org/3/tutorial/introduction.html#numbers) in The Python


Tutorial, docs.python.org
6. Expressions (https://docs.python.org/3/reference/expressions.html) in The Python Language
Reference, docs.python.org

Retrieved from "https://en.wikibooks.org/w/index.php?title=Python_Programming/Operators&oldid=4073412"

This page was last edited on 14 June 2022, at 09:37.

Text is available under the Creative Commons Attribution-ShareAlike License.; additional terms may apply. By using
this site, you agree to the Terms of Use and Privacy Policy.

https://en.wikibooks.org/wiki/Python_Programming/Operators#:~:text=In Python 3.,%3A 4 %2F 2 yields 2.0. 8/8

You might also like