In which version of Python was the initialization syntax set

I just noticed this feature today!

s={1,2,3} #Set initialisation
t={x for x in s if x!=3} #Set comprehension
t=={1,2}

In which version? I also noticed that he had established understanding. Was this added in the same version?

Resources

+3
source share
3 answers

The module setswas added in Python 2.3, but the built-in set type was added to the language in 2.4 with essentially the same interface. (Starting with version 2.6, the module is setsdeprecated.)

This way you can use sets as early as 2.3 if you

import sets

But you will get DeprecationWarningif you try this import in 2.6

To establish the understanding and syntax of a given literal, that is, the ability to say

a = { 1, 2, 3 }

Python 3.0. , , Python 3.0a1, Python 3.0, 2007 . Python 3

2.7. 3.x Python 2.7

+10

, :

>>> s = {1, 2, 3}
  File "<stdin>", line 1
    s = {1, 2, 3}
          ^
SyntaxError: invalid syntax

2.5, , - 2.6 (: 3.0, Ian ). , . , - set([1, 2, 3]).

, , , . Python , , .

0

The syntax of the set of letters and syntax and dictate was transferred to the 2.x torso about 2-3 days ago. Therefore, I assume that this function should be available from python 2.7.

0
source

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


All Articles