Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand expression lists in Python

Tags:

python

list

When I read the Python document today, I found Expression lists on Python Documents, the description on the site like this:

expression_list ::= expression ( "," expression )* [","]

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.

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: ().)

Because the examples are not given on the site, So I just wondering anyone can give a brief description about this, and give a example about its usage. Thank a lots.

like image 945
GoingMyWay Avatar asked Aug 12 '15 07:08

GoingMyWay


People also ask

How do you express a list in Python?

In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item.

What is expression in Python list its types?

Python expressions only contain identifiers, literals, and operators. So, what are these? Identifiers: Any name that is used to define a class, function, variable module, or object is an identifier. Literals: These are language-independent terms in Python and should exist independently in any programming language.

What does -> list mean in Python?

-> List[int] means that the function should return a list of integers.

Is list () and [] the same in Python?

In practical terms there's no difference. I'd expect [] to be faster, because it does not involve a global lookup followed by a function call. Other than that, it's the same.


2 Answers

Here are some samples to help you understand what is going on:

An expression list containing at least one comma yields a tuple.

This means, that if you have 1,2, this will create a tuple. The length is how many items you have.

The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases.

This means that if you want to create a tuple with one item, you need to have a comma at the end, like this 1,, otherwise:

A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression.

So 1 is not creating a tuple, what will happen is the express will be evaluated. This sounds like an obvious point but it makes sense if you write (1) and then expect it to be evaluated as a tupe (in the case of (1), it will be evaluated to the integer value 1).

Finally

(To create an empty tuple, use an empty pair of parentheses: ().)

If for some reason you want to create an empty tuple as an expression, use the following special syntax ()

It is a common practice to surround expressions (especially in the case of tuples) with (), but this is not required - although sometimes it helps with readability. 1,2 and (1,2) are equal:

>>> a = 1,2
>>> type(a)
<type 'tuple'>

>>> b = (1,2)
>>> type(b)
<type 'tuple'>

>>> a == b
True
like image 197
Burhan Khalid Avatar answered Oct 31 '22 23:10

Burhan Khalid


This talks about how you write tuples.

For instance,

>>> 1, 2
(1, 2)

is a two element tuple, as is

>>> 7*8, 5-6
(56, -1)

Tuples are usually written with parentheses around them for clarity, but they are unnecessary; except in case of the 0-element tuple, ().

One-element tuples are another exception, as there it is mandatory to have the comma:

>>> 1,
(1, )

Without the comma it wouldn't be possible to distinguish this from the normal number 1. You can add an extra comma after multi-element tuples too, but it doesn't do anything in that case:

>>> 1, 2,
(1, 2)
like image 31
RemcoGerlich Avatar answered Oct 31 '22 22:10

RemcoGerlich