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!
===Expressions=== Python's [[expression (computer science)|expressions]] include the following: * The <code>+</code>, <code>-</code>, and <code>*</code> operators for mathematical addition, subtraction, and multiplication are similar to other languages, but the behavior of division differs. There are two types of division in Python: [[floor division]] (or integer division) <code>//</code>, and floating-point division <code>/</code>.<ref>{{cite web|title=division|url=https://docs.python.org|website=python.org|access-date=30 July 2014|archive-date=20 July 2006|archive-url=https://web.archive.org/web/20060720033244/http://docs.python.org/|url-status=live}}</ref> Python uses the <code>**</code> operator for exponentiation. * Python uses the <code>+</code> operator for string concatenation. The language uses the <code>*</code> operator for duplicating a string a specified number of times. * The <code>@</code> infix operator<!-- was introduced in Python 3.5--> is intended to be used by libraries such as [[NumPy]] for [[matrix multiplication]].<ref name=PEP465>{{cite web |title=PEP 0465 – A dedicated infix operator for matrix multiplication |url=https://www.python.org/dev/peps/pep-0465/ |website=python.org |access-date=1 January 2016 |archive-date=4 June 2020 |archive-url=https://web.archive.org/web/20200604224255/https://www.python.org/dev/peps/pep-0465/ |url-status=live}}</ref><ref name=Python3.5Changelog>{{cite web |title=Python 3.5.1 Release and Changelog |url=https://www.python.org/downloads/release/python-351/ |website=python.org |access-date=1 January 2016 |archive-date=14 May 2020 |archive-url=https://web.archive.org/web/20200514034938/https://www.python.org/downloads/release/python-351/ |url-status=live}}</ref> * The syntax <code>:=</code>, called the "walrus operator", was introduced in Python 3.8. This operator assigns values to variables as part of a larger expression.<ref name=Python3.8Changelog>{{cite web |title=What's New in Python 3.8 |url=https://docs.python.org/3.8/whatsnew/3.8.html |access-date=14 October 2019 |archive-date=8 June 2020 |archive-url=https://web.archive.org/web/20200608124345/https://docs.python.org/3.8/whatsnew/3.8.html |url-status=live}}</ref> * In Python, <code>==</code> compares two objects by value. Python's <code>is</code> operator may be used to compare object identities (i.e., comparison by reference), and comparisons may be chained—for example, {{code|lang=python|code=a <= b <= c}}. * Python uses <code>and</code>, <code>or</code>, and <code>not</code> as Boolean operators. * Python has a type of expression called a ''[[List comprehension#Python|list comprehension]]'', and a more general expression called a ''generator expression''.<ref name="AutoNT-59"/> * [[Anonymous function]]s are implemented using [[Lambda (programming)|lambda expressions]]; however, there may be only one expression in each body. * Conditional expressions are written as {{code|lang=python|code=x if c else y}}.<ref name="AutoNT-60"/> (This is different in operand order from the <code>[[?:|c ? x : y]]</code> operator common to many other languages.) * Python makes a distinction between [[list (computer science)|lists]] and [[tuple]]s. Lists are written as {{code|lang=python|code=[1, 2, 3]}}, are mutable, and cannot be used as the keys of dictionaries (since dictionary keys must be [[immutable]] in Python). Tuples, written as {{code|lang=python|code=(1, 2, 3)}}, are immutable and thus can be used as the keys of dictionaries, provided that all of the tuple's elements are immutable. The <code>+</code> operator can be used to concatenate two tuples, which does not directly modify their contents, but produces a new tuple containing the elements of both. For example, given the variable <code>t</code> initially equal to {{code|lang=python|code=(1, 2, 3)}}, executing {{code|lang=python|code=t = t + (4, 5)}} first evaluates {{code|lang=python|code=t + (4, 5)}}, which yields {{code|lang=python|code=(1, 2, 3, 4, 5)}}; this result is then assigned back to <code>t</code>—thereby effectively "modifying the contents" of <code>t</code> while conforming to the immutable nature of tuple objects. Parentheses are optional for tuples in unambiguous contexts.<ref>{{cite web|title=4. Built-in Types – Python 3.6.3rc1 documentation|url=https://docs.python.org/3/library/stdtypes.html#tuple|website=python.org|access-date=1 October 2017|archive-date=14 June 2020|archive-url=https://web.archive.org/web/20200614194325/https://docs.python.org/3/library/stdtypes.html#tuple|url-status=live}}</ref> * Python features ''sequence unpacking'' where multiple expressions, each evaluating to something assignable (e.g., a variable or a writable property) are associated just as in forming tuple literal; as a whole, the results are then put on the left-hand side of the equal sign in an assignment statement. This statement expects an ''iterable'' object on the right-hand side of the equal sign to produce the same number of values as the writable expressions on the left-hand side; while iterating, the statement assigns each of the values produced on the right to the corresponding expression on the left.<ref>{{cite web|title=5.3. Tuples and Sequences – Python 3.7.1rc2 documentation|url=https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences|website=python.org|access-date=17 October 2018|archive-date=10 June 2020|archive-url=https://web.archive.org/web/20200610050047/https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences|url-status=live}}</ref> * Python has a "string format" operator <code>%</code> that functions analogously to <code>[[printf]]</code> format strings in the C language—e.g. {{code|2=python|1="spam=%s eggs=%d" % ("blah", 2)}} evaluates to <code>"spam=blah eggs=2"</code>. In Python 2.6+ and 3+, this operator was supplemented by the <code>format()</code> method of the <code>str</code> class, e.g., {{code|2=python|1="spam={0} eggs={1}".format("blah", 2)}}. Python 3.6 added "f-strings": {{code|2=python|1=spam = "blah"; eggs = 2; f'spam={spam} eggs={eggs}'}}.<ref name="pep-0498">{{cite web |title=PEP 498 – Literal String Interpolation |url=https://www.python.org/dev/peps/pep-0498/ |website=python.org |access-date=8 March 2017 |archive-date=15 June 2020 |archive-url=https://web.archive.org/web/20200615184141/https://www.python.org/dev/peps/pep-0498/ |url-status=live}}</ref> * Strings in Python can be [[concatenated]] by "adding" them (using the same operator as for adding integers and floats); e.g., {{code|2=python|1="spam" + "eggs"}} returns <code>"spameggs"</code>. If strings contain numbers, they are concatenated as strings rather than as integers, e.g. {{code|2=python|1="2" + "2"}} returns <code>"22"</code>. * Python supports [[string literal]]s in several ways: ** Delimited by single or double quotation marks; single and double quotation marks have equivalent functionality (unlike in [[Unix shell]]s, [[Perl]], and Perl-influenced languages). Both marks use the backslash (<code>\</code>) as an [[escape character]]. [[String interpolation]] became available in Python 3.6 as "formatted string literals".<ref name="pep-0498"/> ** Triple-quoted, i.e., starting and ending with three single or double quotation marks; this may span multiple lines and function like [[here document]]s in shells, Perl, and [[Ruby (programming language)|Ruby]]. ** [[Raw string]] varieties, denoted by prefixing the string literal with <code>r</code>. Escape sequences are not interpreted; hence raw strings are useful where literal backslashes are common, such as in [[regular expression]]s and [[Windows]]-style paths. (Compare "<code>@</code>-quoting" in [[C Sharp (programming language)|C#]].) * Python has [[array index]] and [[array slicing]] expressions in lists, which are written as <code>a[key]</code>, {{code|lang=python|code=a[start:stop]}} or {{code|lang=python|code=a[start:stop:step]}}. Indexes are [[zero-based numbering|zero-based]], and negative indexes are relative to the end. Slices take elements from the ''start'' index up to, but not including, the ''stop'' index. The (optional) third slice [[Parameter (computer programming)|parameter]], called ''step'' or ''stride'', allows elements to be skipped or reversed. Slice indexes may be omitted—for example, {{code|lang=python|code=a[:]}} returns a copy of the entire list. Each element of a slice is a [[shallow copy]]. In Python, a distinction between expressions and statements is rigidly enforced, in contrast to languages such as [[Common Lisp]], [[Scheme (programming language)|Scheme]], or [[Ruby (programming language)|Ruby]]. This distinction leads to duplicating some functionality, for example: * [[List comprehensions]] vs. <code>for</code>-loops * [[Conditional (programming)|Conditional]] expressions vs. <code>if</code> blocks * The <code>eval()</code> vs. <code>exec()</code> built-in functions (in Python 2, <code>exec</code> is a statement); the former function is for expressions, while the latter is for statements A statement cannot be part of an expression; because of this restriction, expressions such as list and <code>dict</code> comprehensions (and lambda expressions) cannot contain statements. As a particular case, an assignment statement such as {{code|lang=python|code=a = 1}} cannot be part of the conditional expression of a conditional statement.
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)