+ 2
How this second statement works
s="Rooose" v={i for i in s} print(len(v))
5 ответов
+ 6
#it could also be done like this:
print(len(set('Rooose')))
We use set() to convert 'Rooose' to a set, that only can hold unique characters. Then the length of this set is printed.
+ 5
s="Rooose"
v={i for i in s}
print(v)
#Here s has stored Rooose in i takes one by one value in s and converted in str each letter & make set of value in output= {"R","o","s","e"}
print(len(v))
+ 2
v={i for i in s} is set comprehenaion. Set stores only the unique values, so created set has following values,
{'R', 'o', 's', 'e'}
+ 2
You can undetstand it if you read the statement as follows:
you take each i from the for loop, which iterates over all items in s. The all items are then closed with {}, which build up a set of them.
0
Thank you everyone