How this second statement works | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How this second statement works

s="Rooose" v={i for i in s} print(len(v))

22nd Jul 2020, 4:09 PM
Shreepriya HA
5 Answers
+ 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.
22nd Jul 2020, 4:23 PM
Lothar
Lothar - avatar
+ 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))
22nd Jul 2020, 4:20 PM
Sâñtôsh
Sâñtôsh - avatar
+ 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'}
22nd Jul 2020, 4:18 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 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.
22nd Jul 2020, 4:30 PM
JaScript
JaScript - avatar
0
Thank you everyone
25th Jul 2020, 7:23 AM
Shreepriya HA