+ 3

What's with this syntax?

msg:str = "malayalam"; used:str = ""; i:int; for i in range(len(msg)): tmp:str = msg[i]; if (tmp not in used): used += tmp; print(tmp,":", end = ""); j: int; count:int = 0; for j in range(len(msg)): if (tmp == msg[j]): count += 1; print(count); here, even if i replace all the "str" with "float" the output doesn't change. so why was this implemented in the first case?

29th Apr 2022, 5:19 PM
Harsha S
Harsha S - avatar
3 Respostas
+ 7
Str is just a hint but finally we have duck typing. The value determines the type.
29th Apr 2022, 5:30 PM
Oma Falk
Oma Falk - avatar
+ 6
Lothar oh those ☕ They must do 🧠 yoga when they 🐍 🐍 . No semicolon, type anarchy.... A cultural shock 😲 😳 😐 😑
1st May 2022, 5:51 AM
Oma Falk
Oma Falk - avatar
+ 5
Harsha S , just some comments to the code: - we don't need to use semicolons at the end of the lines - the task can be also be done by using the string method count() and a variable that indicates if a character is already seen / counted or not: msg = "malayalam" seen = '' for char in msg: if char not in seen: print(char, msg.count(char)) seen += char
30th Apr 2022, 9:12 AM
Lothar
Lothar - avatar