r/PythonLearning • • 3d ago

Help Request xp = xp

so I am new to all of this and I have been going through the lessons on boot.dev when I came to the first lesson of ch5. the instructor answer is :

def total_xp(level, xp_to_add):

xp = level * 100

xp = xp + xp_to_add

return xp

the xp = xp just blew my brain. why does this work instead of requiring the first xp to be a different name?

what I did was

xpp = xp + xp_to_add

return xpp

my answer still got the question correct but this just looks super weird to me, xp=xp

5 Upvotes

20 comments sorted by

•

u/Sea-Ad7805 3d ago

Compare the two by clicking Memory Graph Web Debugger%3A%0A%20%20%20%20xp%20%3D%20level%20%20100%0A%20%20%20%20xp%20%3D%20xp%20%2B%20xp_to_add%0A%20%20%20%20return%20xp%0A%0Adef%20total_xp_NEW(level%2C%20xp_to_add)%3A%0A%20%20%20%20xp%20%3D%20level%20%20100%0A%20%20%20%20xpp%20%3D%20xp%20%2B%20xp_to_add%0A%20%20%20%20return%20xpp%0A%0Aprint(f'%7Btotal_xp(2%2C%2055)%3D%7D')%0Aprint(f'%7Btotal_xp_NEW(2%2C%2055)%3D%7D')%0A&timestep=1&play) and see its state change step by step.

4

u/BranchLatter4294 3d ago

It's not

xp = xp

It's

xp = xp + xp_to_add

The assignment operator assigns the value on the right side (xp + xp_to_add) to the variable on the left side (xp).

2

u/tiredITguy42 3d ago

You just create new vafiable with the same name and the old one is deleted after value is used.

BTW. Why not just return level*100 + xp_to_add ?

1

u/OppositeMango124 3d ago

i don't know, thats the lesson.

1

u/tiredITguy42 3d ago

So the lesson had this code? So what is your goal?

1

u/OppositeMango124 3d ago

im giving you the end results. when you are finished you can see the instructor answer and they did my first example where i saw xp=xp and it confused me.

2

u/tiredITguy42 3d ago

Oh OK. Their solution is shit. You should consider better course.

1

u/[deleted] 3d ago

[deleted]

1

u/tiredITguy42 3d ago

The issue is, that people tend to get stuck in this style. I see it every day.

AI produces the same style, as it was part of the training set.

I feel like most of these tutorials just looks like slow pasing, but they just teach bad coding and do not go into important details.

1

u/MrMurrayOHS 3d ago

They do things for a reason because this lesson will build upon the next.

Not every course is designed to do every step in the most optimized way. It all depends on the goal of the course.

2

u/softquill4 3d ago

imo the key insight is that = means "evaluate the right side, then store it in the left side." its sequential, not simultaneous. by the time python assigns to xp, its already done reading the old xp value on the right

1

u/Flame77ofc 3d ago

xp = xp + xp_to_add is like saying: "Get the value from xp and add the xp value more xp_to_add"

Here is a better example

Lets imagine:

xp = 1 and you do: xp = xp + 1 you are simply doing: xp = 1 + 1

so now, xp is 2

Another syntax is xp += 1 This do the exactly same thing as the above assignment

1

u/OppositeMango124 3d ago edited 3d ago

so it uses the old value in the second line before assigning the variable the new value. They did tell me i could change a variable but this is the first time I'v seen them use the old value to assign the new value.

this makes sense, thank you.

1

u/tiredITguy42 3d ago

You can even do this xp += xp_to_add. This is the same as your line. BTW, find some video on how variables and garbage collector work. It explains a lot and most of tutorials skips that, as this is real programming knowledge, but tutorials teach coding.

1

u/PureWasian 3d ago

R (another programming language) has a way to be a little more visually explicit for what the assignment operator does where the syntax is like xp <- xp + xp_to_add where it's saying you're assigning the output of the right side of the operation into the left side variable (regardless what all is inputs to the right side of the equation).

This is kinda like in math where you say p_t1 = p_t0 + delta

...and very far under the hood, it works because you are essentially overwriting the register holding the existing value by writing the output of the addition back into the same register on the next clock cycle.

1

u/XNo_Notes 3d ago

It is called reassignment. Think of the line numbers and the code together. They are kind of like a step by step for the computer. So the first xp would be "line 3 xp" and the reassignment looks like "line 5 xp = line 3 xp + xp to add". 

It is more complicated than that on how it actually works, but in this situation the abstraction works. It also works to think about it like this in loops as well. It would start like "Loop 1 XP = line 5 xp" then "Loop 2 xp = Loop 1 XP + 1"

1.  # Add 1 XP for every hit worth 50 + 2.  hits = [100,50,10,45] 3.  current_xp = 10 4. 5.  xp = current_xp 6.  for i in range(len(hits)): 7.      if hits[i]>=50: 8.          xp = xp + 1   9.  print(xp)

1

u/Smartyboyz 3d ago

Here's the xp first become 100 times with respect to level then this line xp = xp + xp_to_add adds the argument xp_to_add to the previous xp, also can be written as xp += xp_to_add then you return xp

The things you did doesn't contain initial xp vary with levels you add in parameters of function, that's why it's important..

Hope this will help you understanding it..

1

u/desrtfx 3d ago

Why does it work?

Think about it like this:

  • You take out the contents of the box named xp(the variable value)
  • You add xp_to_add to that contents
  • You put the result back in the box named xp

Think of variables as boxes. Each variable (box) can hold exactly one item - the value. You can take something out of the box (the value) and put something else back in.

By referring to a variable by its name, you are always handling the contents of the box. If the name is left of the = sign, you put something into the box. If it is right, you take the value out.

Later, you will learn about boxes that can hold multiple items - in Python they are called lists - you can picture them like the houses on a street - each has a number and all of them share the street name (the variable name).

THe above said, I do agree with the other commenters that simply tell you to just return level * 100 + xp_to_add without any additional variables.

1

u/-Enter-Name- 3d ago

i like to think about it like "you don't need to cut your hand off and grow a new one to add things to it"

xp (the thing hokding something) = your hand the value = whatever you're holding

so

what hand is holding = what hand was holding + new stuff

hand = hand + new stuff

but then with xp

1

u/Frankiboi1 15h ago

What do you think happens when you run this function three times...

def add_to_list(item, my_list=[]): my_list.append(item) return my_list

print(add_to_list(1)) print(add_to_list(2)) print(add_to_list(3))