Python Tuple Declaration

In python, you can explicitly declare a tuple using parentheses as such:

>>> x = (0.25, 0.25, 0.25, 0.25)
>>> x
(0.25, 0.25, 0.25, 0.25)
>>> type(x)
<type 'tuple'>

Alternatively, without parentheses, python automatically wraps it in an immutable tuple:

>>> x = 0.25, 0.25, 0.25, 0.25
>>> x
(0.25, 0.25, 0.25, 0.25)
>>> type(x)
<type 'tuple'>

Is there a pythonic style for declaring a tuple? If yes, please also refer to the appropriate PEP or link.

There is no difference in the "final product" for getting a tuple, but is there a difference in how the tuple is initialized with and without parentheses (in CPython)?

+4
source share
2 answers

, . import this moto:

" , ".

, : one_tuple = (15, ).

+4

"" . , ( ). x = 1,2,3 , x = (1,2,3) ( ).

, , , . , l , 1,2,l.count(1) , l.count , , (1,2,l).count(1) , count. , (2+3)*4 ( ).

, , . fx x = (), . , , (.. x = 1,), 1.

-1

Source: https://habr.com/ru/post/1624413/


All Articles