Can you explain this Python3 syntax ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you explain this Python3 syntax ?

Hello SoloLearn, can you explain this code in details ? https://code.sololearn.com/chyf6a3UO2A9/?ref=app Have a nice day !

27th Aug 2017, 1:02 PM
coutable.n
coutable.n - avatar
3 Answers
+ 4
n=1000 k=67 print(*n//k*[k]+[n%k]*(n%k>0)) n%k=62 n//k=14 so last line can be simplified to print(*14*[67]+[62]*(62>0)) which is actually 2 parts, * 14*[67] + [62]*(62>0) because 62 IS BIGGER THAN 0, 62>0 is True True is represented in python (as an integer) using 1 True==1 False==0 so [62]*(62>0) can be simplified as [62]*(1) =[62] so now first statrment becomes * 14*[67] + [62] [67]*14 is [67,67,67,67,67,67,67,67,67,67,67,67,67,67] so statement is now * [67,67,67,67,67,67,67,67,67,67,67,67,67,67]+[62] = * [67,67,67,67,67,67,67,67,67,67,67,67,67,67,62] *mark is here is taken as unpacking operator so it unpacks list and gives individual items eg: *[1,2,4] = 1,2,4 *[6,0,7]= 6,0,7 so statement is now print(67,67,67,67,67,67,67,67,67,67,67,67,67,67,62) so it prints the answer.
27th Aug 2017, 1:55 PM
Sunera Avinash
Sunera Avinash - avatar
+ 2
look on operator precedent in python to understand why this happened in exactly this order
27th Aug 2017, 2:04 PM
Sunera Avinash
Sunera Avinash - avatar
0
@Sunera Avinash, very well, I thank you alot for your time. By the way, it was on CodinGame multiplayer, I've lost against a Python3 player and I always need to understand when my 15 lines code is beated in a single line Powerfull tool 🙂
27th Aug 2017, 2:12 PM
coutable.n
coutable.n - avatar