+ 3

Somebody explain me this python Code!

This is something new to me. I have never seen this code before. print('YNEOS'[5%~int(input())%2::2]) I know what is the outcome of the code but has no idea how this work, please explain

12th Apr 2022, 9:57 AM
Anshuman Routray
Anshuman Routray - avatar
2 RĂ©ponses
+ 4
:: is called slicing, it's syntax is start:stop:step here, start includes 5%~int(input())%2 stop is between : : which means everything in the string 'YNEOS' and 2 is the step ~int(input()) is taking user input and producing it's one's complement so what ever will be the value of the expression given in "start" then according to that the string 'YNEOS' will be sliced for example if value of start came out 1 then we have print( 'YNEOS'[ 1::2 ] ) will PRINT "NO" HOW??? start = 1 so slicing of string will start from index 1 which is "N" and step value is 2 it means index will jump 2 steps forward at index (1+2) = 3. the element at index 3 is "O" HENCE, output will be "NO" understanding Slicing in python 👇👇👇 https://stackoverflow.com/questions/509211/understanding-slicing How to understand Slicing👇👇👇👇 https://www.oreilly.com/content/how-do-i-use-the-slice-notation-in-python/
12th Apr 2022, 11:09 AM
NonStop CODING
NonStop CODING - avatar
+ 3
Thank you, I understood completely
12th Apr 2022, 11:32 AM
Anshuman Routray
Anshuman Routray - avatar