Help with understanding difference between these codes

  • Thread starter Kolika28
  • Start date
  • Tags
    Difference
In summary: But because you just said:double(x)the change isn't saved to x. In summary, the first code returns a modified copy of the original list with all elements multiplied by 2, while the second code mutates the original list by adding 1 to the first element and doesn't return anything. It's important to note that in Python, functions that mutate objects in place should not return anything.
  • #1
Kolika28
146
28
TL;DR Summary
I don't understand why the first code won't change the list x, while the second code does change the list. Could someone explain?
Python:
#Code nr.1
x=[1,2]
def double(l):
    return [a*2 for a in l]
double(x)
print(x)

#Code nr.2
def f(x):
    x[0]=x[0]+1
    return x
x=[3]
f(x)
print(x)
 
Technology news on Phys.org
  • #2
The first code returns a newly constructed list whose elements are the elements of the original list multiplied by two. That doesn't change anything about the original list. Try having print(double(x)) instead of just double(x) and see what happens.

The second code doesn't construct a new list, it sets the first element of the list to its previous value plus 1.
 
  • Like
Likes Kolika28
  • #3
Note also that in the second code, f(x) returns the list x, but that return value isn't needed since you already have a variable pointing to the list x. It's a good general practice in Python that a function that mutates an object in place, instead of constructing a new object, should not return anything. That makes it clear that the function is not constructing anything new.
 
  • Like
Likes Kolika28
  • #4
[x for x in mylist] is a copy of mylist. Your first function uses this to create a modified copy, which it returns and you don't store or display. Your second function edits the list in place. You return the edited list and forget that too, but because you've edited it in place the original list is modified.

Edit: beaten to it by Peter, I see.
 
  • Like
Likes Kolika28
  • #5
Thank you so much, both of you! I understand now ! :)
 
  • #6
If you were to say:

x=double(x)

then x would be changed.
 
  • Like
Likes Kolika28

Related to Help with understanding difference between these codes

1. What is the difference between HTML, CSS, and JavaScript?

The main difference between HTML, CSS, and JavaScript is that HTML is used for creating the structure and content of a webpage, CSS is used for styling and formatting the webpage, and JavaScript is used for adding interactive and dynamic features to the webpage.

2. How do I know which code to use for a specific task?

To determine which code to use for a specific task, you first need to understand the purpose and functionality of each code. HTML is used for creating the basic structure of a webpage, CSS is used for styling and formatting the webpage, and JavaScript is used for adding interactivity and functionality to the webpage. Based on the task you want to achieve, you can choose the appropriate code to use.

3. Can I use all three codes together?

Yes, you can use all three codes together. In fact, it is recommended to use all three in combination to create a fully functional and visually appealing webpage. HTML, CSS, and JavaScript work together to create a seamless user experience on a webpage.

4. Are there any other codes I should learn besides HTML, CSS, and JavaScript?

Yes, there are other codes that are commonly used in web development such as PHP, Python, and Ruby. These codes are used for more complex tasks and backend development. It is beneficial to have a basic understanding of these codes, but HTML, CSS, and JavaScript are the essential ones to learn for front-end web development.

5. Is it necessary to have a deep understanding of coding to create a website?

It depends on the complexity of the website you want to create. For simple websites, a basic understanding of HTML, CSS, and JavaScript is enough. However, for more complex and dynamic websites, a deeper understanding of coding is necessary. It is always beneficial to have a good understanding of coding to create a functional and visually appealing website.

Similar threads

  • Programming and Computer Science
Replies
8
Views
831
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
4
Views
910
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top