Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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])) 
like image 477
sofa_maniac Avatar asked Jul 26 '15 04:07

sofa_maniac


People also ask

What does [- 1 :] mean in Python?

Python also allows you to index from the end of the list using a negative number, where [-1] returns the last element. This is super-useful since it means you don't have to programmatically find out the length of the iterable in order to work with elements at the end of it.

What does [:- 1 in Python do?

What Does [:-1] In Python Mean And Why Use It? What does [:-1] in Python actually do and why would you want to use it? [:-1] in Python is a slice operation used on strings or lists and captures all contents of the string or list except for the last character or element.

What does [:: 2 do in Python?

string[::2] reads “default start index, default stop index, step size is two—take every second element”.

What is the meaning of %% in Python?

The % symbol in Python is called the Modulo Operator. It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.


2 Answers

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

list[<start>:<stop>:<step>] 

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 = '1234' >>> a[::-1] '4321' 

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.

like image 72
Anand S Kumar Avatar answered Oct 15 '22 22:10

Anand S Kumar


The notation that is used in

a[::-1] 

means that for a given string/list/tuple, you can slice the said object using the format

<object_name>[<start_index>, <stop_index>, <step>] 

This means that the object is going to slice every "step" index from the given start index, till the stop index (excluding the stop index) and return it to you.

In case the start index or stop index is missing, it takes up the default value as the start index and stop index of the given string/list/tuple. If the step is left blank, then it takes the default value of 1 i.e it goes through each index.

So,

a = '1234' print a[::2] 

would print

13 

Now the indexing here and also the step count, support negative numbers. So, if you give a -1 index, it translates to len(a)-1 index. And if you give -x as the step count, then it would step every x'th value from the start index, till the stop index in the reverse direction. For example

a = '1234' print a[3:0:-1] 

This would return

432 

Note, that it doesn't return 4321 because, the stop index is not included.

Now in your case,

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

would just reverse a given integer, that is stored in a string, and then convert it back to a string

i.e "1234" -> "4321" -> 4321 -> "4321"

If what you are trying to do is just reverse the given string, then simply a[::-1] would work .

like image 22
Abhilash Panigrahi Avatar answered Oct 15 '22 22:10

Abhilash Panigrahi