0% found this document useful (0 votes)
64 views3 pages

Literals: Literal

Python supports various literals that evaluate to immutable objects like strings, bytes, integers, floats, and complex numbers. Literals with the same value may or may not return the same object. Parenthesized forms in Python return whatever the expression list within yields, such as a tuple if it contains a comma. Comprehensions provide a way to construct lists, sets and dictionaries using a for loop and optional if clauses to filter elements in a separate implicit scope.

Uploaded by

Vikky Nangare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views3 pages

Literals: Literal

Python supports various literals that evaluate to immutable objects like strings, bytes, integers, floats, and complex numbers. Literals with the same value may or may not return the same object. Parenthesized forms in Python return whatever the expression list within yields, such as a tuple if it contains a comma. Comprehensions provide a way to construct lists, sets and dictionaries using a for loop and optional if clauses to filter elements in a separate implicit scope.

Uploaded by

Vikky Nangare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Literals

Python supports string and bytes literals and various numeric literals:

literal ::= stringliteral | bytesliteral


| integer | floatnumber | imagnumber

Evaluation of a literal yields an object of the given type (string, bytes, integer, floating
point number, complex number) with the given value. The value may be approximated
in the case of floating point and imaginary (complex) literals. See section Literals for
details.

All literals correspond to immutable data types, and hence the object’s identity is less
important than its value. Multiple evaluations of literals with the same value (either the
same occurrence in the program text or a different occurrence) may obtain the same
object or a different object with the same value.

6.2.3. Parenthesized forms


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; 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.
6.2.4. Displays for lists, sets and dictionaries
For constructing a list, a set or a dictionary Python provides special syntax called
“displays”, each of them in two flavors:

● either the container contents are listed explicitly, or


● they are computed via a set of looping and filtering instructions, called a
comprehension.

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 await expressions. 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.

New in version 3.6: Asynchronous comprehensions were introduced.

Changed in version 3.8: yield and yield from prohibited in the implicitly nested
scope.

You might also like