Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Python (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Arithmetic operations=== Python includes conventional symbols for arithmetic operators (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>), the floor-division operator <code>//</code>, and the [[modulo operation|modulo operator]] <code>%</code>. (With the module operator, a remainder can be negative,<!--unlike in C language depending on compiler,<ref>{{Cite web|url=https://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbers/42131603|title=c – Modulo operation with negative numbers|quote=Note that, in C89, whether the result round upward or downward is implementation-defined.|website=Stack Overflow|access-date=25 September 2019}}</ref>--> e.g., <code>4 % -3 == -2</code>.) Python also offers the <code>**</code> symbol for [[exponentiation]], e.g. <code>5**3 == 125</code> and <code>9**0.5 == 3.0</code>; it also offers the matrix‑multiplication operator <code>@</code> .<ref>{{cite web |url=https://legacy.python.org/dev/peps/pep-0465/ |title=PEP 465 – A dedicated infix operator for matrix multiplication |work=python.org |access-date=3 July 2018 |archive-date=29 May 2020 |archive-url=https://web.archive.org/web/20200529200310/https://legacy.python.org/dev/peps/pep-0465/ |url-status=live}}</ref> These operators work as in traditional mathematics; with the same [[order of operations|precedence rules]], the [[infix notation|infix]] operators <code>+</code> and <code>-</code> can also be [[unary operation|unary]], to represent positive and negative numbers respectively. Division between integers produces floating-point results. The behavior of division has changed significantly over time:<ref name="pep0238"/> * The current version of Python (i.e., since 3.0) changed <code>the /</code> operator to always represent floating-point division, e.g., {{code|class=nowrap|2=python|1=5/2 == 2.5}}. * The floor division <code>//</code> operator was introduced. Thus <code>7//3 == 2</code>, <code>-7//3 == -3</code>, <code>7.5//3 == 2.0</code>, and <code>-7.5//3 == -3.0</code>. For outdated Python 2.7 adding the {{code|class=nowrap|2=python2|1=from __future__ import division}} statement causes a module in Python 2.7 to use Python 3.0 rules for division instead (see above). In Python terms, the <code>/</code> operator represents ''true division'' (or simply ''division''), while the <code>//</code> operator represents ''floor division.'' Before version 3.0, the <code>/</code> operator represents ''classic division''.<ref name="pep0238"/> [[Rounding]] towards negative infinity, though a different method than in most languages, adds consistency to Python. For instance, this rounding implies that the equation {{code|class=nowrap|2=python|1=(a + b)//b == a//b + 1}} is always true. The rounding also implies that the equation {{code|class=nowrap|2=python|1=b*(a//b) + a%b == a}} is valid for both positive and negative values of <code>a</code>. As expected, the result of <code>a%b</code> lies in the [[half-open interval]] [0, ''b''), where <code>b</code> is a positive integer; however, maintaining the validity of the equation requires that the result must lie in the interval (''b'', 0] when <code>b</code> is negative.<ref name="AutoNT-62"/> Python provides a <code>round</code> function for rounding a float to the nearest integer. For [[Rounding#Tie-breaking|tie-breaking]], Python 3 uses the ''round to even'' method: <code>round(1.5)</code> and <code>round(2.5)</code> both produce <code>2</code>.<ref name="AutoNT-64"/> Python versions before 3 used the [[Rounding#Rounding away from zero|round-away-from-zero]] method: <code>round(0.5)</code> is <code>1.0</code>, and <code>round(-0.5)</code> is <code>−1.0</code>.<ref name="AutoNT-63"/> Python allows Boolean expressions that contain multiple equality relations to be consistent with general usage in mathematics. For example, the expression <code>a < b < c</code> tests whether <code>a</code> is less than <code>b</code> and <code>b</code> is less than <code>c</code>.<ref name="AutoNT-65"/> C-derived languages interpret this expression differently: in C, the expression would first evaluate <code>a < b</code>, resulting in 0 or 1, and that result would then be compared with <code>c</code>.<ref name="CPL"/> Python uses [[arbitrary-precision arithmetic]] for all integer operations. The <code>Decimal</code> type/class in the <code>decimal</code> module provides [[decimal floating point|decimal floating-point numbers]] to a pre-defined arbitrary precision with several rounding modes.<ref name="AutoNT-88"/> The <code>Fraction</code> class in the <code>fractions</code> module provides arbitrary precision for [[rational number]]s.<ref>{{cite web|title=What's New in Python 2.6 |url=https://docs.python.org/2.6/whatsnew/2.6.html|website=Python v2.6.9 documentation |date=Oct 29, 2013 |access-date=26 September 2015|archive-date=23 December 2019|archive-url=https://web.archive.org/web/20191223213856/https://docs.python.org/2.6/whatsnew/2.6.html|url-status=live}}</ref> Due to Python's extensive mathematics library and the third-party library [[NumPy]], the language is frequently used for scientific scripting in tasks such as numerical data processing and manipulation.<ref>{{Cite web|url=https://www.stat.washington.edu/~hoytak/blog/whypython.html|title=10 Reasons Python Rocks for Research (And a Few Reasons it Doesn't) – Hoyt Koepke|website=University of Washington Department of Statistics |access-date=3 February 2019|archive-date=31 May 2020|archive-url=https://web.archive.org/web/20200531211840/https://www.stat.washington.edu/~hoytak/blog/whypython.html|url-status=dead}}</ref><ref>{{Cite web|url=https://engineering.ucsb.edu/~shell/che210d/python.pdf|title=An introduction to Python for scientific computing|last=Shell|first=Scott|date=17 June 2014|access-date=3 February 2019|archive-date=4 February 2019|archive-url=https://web.archive.org/web/20190204014642/https://engineering.ucsb.edu/~shell/che210d/python.pdf|url-status=live}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)