Can someone explain this python code please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain this python code please?

I was practicing the C++ lesson of sololearn named "Crack the code"(Something like that). And i googled same thing for python and got this. Please Explain it for me. #Code Code="416e206572726f72206f636375727265642e0a506c6561736520636865636b20796f757220696e7465726e657420636f6e6e656374696f6e20616e642074727920616761696e2e" for i in range(len(Code)//2): print(chr(int(Code[2*i:+2*(i+1)],16)),end="")

1st Sep 2020, 4:27 PM
Raju
Raju - avatar
1 Answer
0
Loop runs half the length of Code string times , Code[2*i:+2*(i+1)] slices the two characters and returns them Like Code[0:2]=>41 ,Code[2:4]=>6e and so on(should be clear by now why the loop runs half the length of string ) int(41,16),16 represents that the string is a hexadecimal representation which needs to be converted to decimal ,in decimal 41 is equal to 16+16+16+16+1=>65 chr(65) returns the character representation for 65 as specified in ASCII table which is "A" and this whole thing happens for every two characters which forms the following string , "An error occurred. Please check your internet connection and try again"
1st Sep 2020, 5:13 PM
Abhay
Abhay - avatar