M
M
Magic Code2020-07-16 17:20:18
Python
Magic Code, 2020-07-16 17:20:18

Why does for i in work with a set like for in in range?

#  создаем множество
a = set('123')
#  получаем доступ к элементам
for i in a: print(i)
>>> {1, 2, 3}
>>> {1, 2, 3}
>>> {1, 2, 3}

Why is this happening?
And one more thing, why it is impossible to set a set, as follows:
a = set(1, 2, 3) 
>>> Error
a = set('1,2,3')
>>> {'1', '2', '3'}

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2020-07-16
@Panda_Code

'123' is a string. A string is a sequence of characters. Therefore, a string can be turned into any other sequence, it can be sorted character by character, it can be overtaken into a list character by character.
But with the transfer of the string to the set () set, there will be a bummer: you will get one instance of each character from the string :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question