Saturday, July 21, 2018

What is the meaning of "int(a[::-1])" in Python?




I cannot understand this. I have seen this in people's code. But cannot figure out what it does. This is in Python.



str(int(a[::-1]))


Answer



Assumming a is a string. The Slice notation in python has the syntax -



list[::]


So, when you do a[::-1] , it starts from the end, towards the first, taking each element. So it reverses a. This is applicable for lists/tuples as well.



Example -




>>> a = '1232'
>>> a[::-1]
'2321'


Then you convert it to int and then back to string (Though not sure why you do that) , that just gives you back the string.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...