Identifying a variable with a method will execute the method?

In the exercise removing list elements using the .pop method:

https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-lists/lessons/use-python-list/exercises/removing-by-index-pop

I notice that identifying the removed element in order to save it results in the method being automatically executed?

So just identifying “removed_element” as “cs_topics.pop()” RUNS the method “cs_topics.pop()” as well? Is this something unique to methods or .pop? I’d have though you needed to have a line before or after that executed the “pop” method before telling python to hang onto that element for later as “removed_element”?

In previous exercises, something like:

grade = 6
new_grade = grade + 1
print(grade)
print(new_grade)

saying “new_grade = grade + 1” doesn’t automatically add 1 and change “grade”

but in the .pop exercise, simply saying “if we take this thing out, save it as this” removes the element from the original list.

Does my question make sense? :smiley:

Hi there! Welcome :smiley:

Too be honest, I had to read it twice before I understood what you were asking (at least, I think :stuck_out_tongue: ) :slight_smile:

Your question seems to build down to this specific part:

removed_element = cs_topics.pop()

So just identifying “removed_element” as “cs_topics.pop()” RUNS the method “cs_topics.pop()”

In that line you are not identifying removed_element as cs_topics.pop(), but you are assigning cs_topics.pop() return value to removed_element. So indeed the method is run, but this is because you are explicitly telling it to do so (calling the method).

saying “new_grade = grade + 1” doesn’t automatically add 1 and change “grade”

Correct! Here we are assigning the value of grade +1 to a new variable. Grade indeed stays as it is.

but in the .pop exercise, simply saying “if we take this thing out, save it as this” removes the element from the original list.

Not quite! The pop method could be simplified to “Take this thing out and return it”. Then assigning it to a variable makes it so you can reuse it later on.

I hope I was able to clarify things a bit, if not then please let me know!

Thank you so much for fast and incredibly clear reply! I’m new to this! (and yay forums!)

You understood my question exactly. If I’m understanding the answer correctly, the difference in the two instances is when you modify “grade” with a mathematical function, grade stays the same. But METHODS used similarly will RUN in the event you “call” them?

I think for my brain, the “grade” instance is a conditional “re-label” in the event you tell python to print or use “new_grade,” but methods are actions that are run in the code regardless of their placement and return a new “list?”

Additionally…and hopefully not confusing…

I think you’re talking about variable assignment. (?)

grade = 77
new_grade = grade + 1

new_grade = 97
print(new_grade)

“new_grade” will be whatever you assign to it.
Some explanations:
https://realpython.com/python-variables/
https://cs.stanford.edu/people/nick/py/python-var.html

.pop(i) is a built in list function that removes and returns an item from a list. (“i” , or index is optional. If not noted, then it removes the last item from the list.)

to see/understand how .pop() works:
https://docs.python.org/3/tutorial/datastructures.html

Each type of data structure has built-in methods. In this case—lists-- and .pop() is a built in method.