Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Computer Science

Thomas Dimnet
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Thomas Dimnet
Python Development Techdegree Graduate 43,610 Points

Result variable in video (value or reference)?

Hello there,

I was watching this video and I have a question related to the memory allocation subject. When we do this:

new_list = [1, 2, 3]
result = new_list[0]

What is result? Is it a copy of the reference new_list first array element (new_list[0]) or it is a copy of the value itself?

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

If that example is python 3, then for your specific example that answer would be a copy/different value, since it is just a single element and creates an integer. Without seeing the vid, I would guess that the reason the variable is called "result" is to show that an evaluated expression creates a new variable. Now, if you just set another variable to the first variable the result would be having another variable name share the value value in memory (a reference).

Here's some sample python 3 code to test:

new_list = [1, 2, 3]
result = new_list[0]
list_again = new_list

print("\nTypes\n")
print('new_list is type --> ' + str(type(new_list)))
print('result is type --> ' + str(type(result)))
print('list_again is type --> ' + str(type(list_again)))

print("\ninitial values\n")
print('new_list is --> ' + str(new_list))
print('result is --> ' + str(result))
print('list_again is --> ' + str(list_again))

new_list[0] = 'abc'
list_again[2] = 'new value'

print("\nAfter change values\n")
print('new_list is --> ' + str(new_list))
print('result is --> ' + str(result))
print('list_again is --> ' + str(list_again))
# output
Types

new_list is type --> <class 'list'>
result is type --> <class 'int'>
list_again is type --> <class 'list'>

initial values

new_list is --> [1, 2, 3]
result is --> 1
list_again is --> [1, 2, 3]

After change values

new_list is --> ['abc', 2, 'new value']
result is --> 1
list_again is --> ['abc', 2, 'new value']

:tropical_drink: :palm_tree: