# Why does this code work, python

• mr.me
In summary, the function provided can reverse the elements of a list by swapping elements using index manipulation. There are different ways to swap elements, and the code shown uses a unique technique specific to Python. The use of negative indexes and the 'reverse' method of the list class are also mentioned.
mr.me
This isn't quite homework, it was just in my textbook as an illustration. Below is a function to return a list in reverse order.

Why does it work? My brother tried to explain it to me but failed to clarify. The only line I don't understand is the third and what process takes place because of it.

Code:
def list_reverse(a_list):
for index in range(len(a_list) / 2):
a_list[index],a_list[-index-1] = a_list[-index-1],a_list[index]

return a_list

this_list=["happy", 1,2,3,4,5]
print list_reverse(this_list)

basically, in a list there are n elements, with indices 0, 1, 2, 3, ... , n-1

in the third line, you assign element n-1 to index 0, assign element n-2 to index 2, assign element n-1-x to index x, as well as the reverse, ie assign element x to index n-1-x

you have to do it in pairs because if you simply assign element x into index n-1-x, what was originally as element n-1-x would then be lost and (in most cases) irrecoverable

That third line is very tricky. The purpose of that line is to swap two elements of a list. Although you can do this in pretty much any programming language, the way it is done here is unique to Python.

The more usual way of swapping two elements in a list is this:
1) Store the value of the first variable in a temp variable.
2) Store the value of the second element in the first variable.
3) Store the value of temp in the second variable.

Here is Python code for reversing the elements of a list, using this technique.
Code:
def list_reverse(a_list):
for index in range(len(a_list) / 2):
temp = a_list[index]
a_list[index] = a_list[-index-1]
a_list[-index-1] = temp

return a_list

this_list=[0, 1,2,3,4,5, 6, 7, 8, 9]
print this_list
print list_reverse(this_list)

The other thing that is tricky about the code you showed is its use of negative indexes for the list. My code example can be rewritten so that it doesn't use negative list indexes.
Code:
def list_reverse(a_list):
length = len(a_list)
for index in range(len(a_list) / 2):
temp = a_list[index]
a_list[index] = a_list[len -index-1]
a_list[len -index-1] = temp

return a_list

this_list=[0, 1,2,3,4,5, 6, 7, 8, 9]
print this_list
print list_reverse(this_list)

This a good academic exercise, but everyone knows the real way to reverse a list in Python is to use the 'reverse' method of the list class, right? It's going to be faster than the Python code using tuples, and even faster than the pseudo-C code using 'temp'. Just checking...

Dick Said
Re: Why does this code work, python
This a good academic exercise, but everyone knows the real way to reverse a list in Python is to use the 'reverse' method of the list class, right? It's going to be faster than the Python code using tuples, and even faster than the pseudo-C code using 'temp'. Just checking...

Yes, that's understood

I think the point was to give us Hint as to What C might behave like in this instance

## 1. Why is indentation important in Python?

Indentation is important in Python because it is used to indicate the level of code blocks. It helps the interpreter understand the structure of the code and therefore, indentation errors can cause the code to not function properly. Additionally, indentation also improves the readability and organization of the code.

## 2. How does Python handle errors?

Python uses a mechanism called "exception handling" to handle errors. When an error occurs, an exception object is created and the interpreter looks for an appropriate exception handler to handle the error. If no handler is found, the program will terminate with an error message.

## 3. Can Python code be compiled?

Yes, Python code can be compiled using the built-in function "compile()". However, Python is an interpreted language and does not require compilation before execution. The compiled code is then executed by the Python virtual machine.

## 4. Why does Python use dynamic typing?

Python uses dynamic typing, which means that the type of a variable is determined at runtime. This allows for more flexibility and easier coding as variables can hold different types of data throughout the execution of the program. However, it also increases the chances of runtime errors if the wrong type of data is assigned to a variable.

## 5. What are some advantages of using Python?

Some advantages of using Python include its simple and easy-to-learn syntax, its large standard library that provides a wide range of functionalities, its cross-platform compatibility, and its extensive support for various programming paradigms such as object-oriented, functional, and procedural programming.

• Programming and Computer Science
Replies
3
Views
297
• Programming and Computer Science
Replies
34
Views
2K
• Programming and Computer Science
Replies
4
Views
3K
• Programming and Computer Science
Replies
22
Views
704
• Programming and Computer Science
Replies
5
Views
976
• Programming and Computer Science
Replies
55
Views
4K
• Programming and Computer Science
Replies
13
Views
1K
• Programming and Computer Science
Replies
2
Views
756
• Programming and Computer Science
Replies
5
Views
871
• Programming and Computer Science
Replies
9
Views
2K