+ 4
Python One Liner
Can this program be written in one line? If yes, how? for i, j in zip(range(1, 11), range(1, 12)): print(i,'miles = ',round(j*1.609,3),'km') I did this parallel iteration using zip() but don't know how to unpack when writing one line as this single iteration can be written though. print(*[i for i in range(1,11)])
3 odpowiedzi
+ 5
You don't need the zip() function, a basic comprehension is sufficient. You even can save the round() function, this can be done like shown in the code with a format specifier.
[print(f'{i} miles = {i*1.609:.3f} km') for i in range(1,11)]
+ 4
thanks 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥
i didn't thought of the way that it can print directly if it is in list
thanks a lot again
+ 3
thanks a lot Lothar
i like the way 👍