FAQ: Variables - Changing the Value of a Variable

This community-built FAQ covers the “Changing the Value of a Variable” exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn How to Code

FAQs on the exercise _Changing the Value of a Variable _

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

where is the drop down menu to change the game options located

1 Like

It says the value of a variable can be changed part way through the code. You are using the example “my_number” in this case. How does the computer not get confused over which value belongs to the variable name as it is the same for both the old and the new number?

The latest assignment takes the place of the earlier one. The value first referenced is no longer in effect. my_number will now refer to new_number, as it were.

It’s very common to have variables change their values. In fact that is why we refer to them as variable. They are subject to change. The only ones that cannot be changed are constants.

const pi = 3.1415;

Interesting. I get it!
A follow up question then:

  • Isnt it better to “clean up” the code then and remove old variabler no longer needed or used?

Thanks for your help

2 Likes

Programs are meant to be dynamic, not static. We want to build in as much flexibility as possible so the code may be re-used.

function times_2_plus_3()  {
    return 5 * 2 + 3;
}

The above is a static function. It contains no variables, only literals and returns a constant value, 13. How can we make it dynamic and reusable? By letting the user input any value, not just 5.

function times_2_plus_3(number)  {
    return number * 2 + 3;
}

Now can reuse this function on any number, which will be doubled and added to 3.

Cleaning up the code only means removing redundancy, repetition and simplifying bloated code patterns. It does not mean manually reseting values on variables. The program needs to be able to do that on the fly in the normal course of action.

Perhaps you could post the code sample in question? That will give us something to bounce this discussion off of.

2 Likes

In the section “We could also change the value of my_number part way through our program:”, I can see how the values of my_number can be changed with the multiply * and division / symbols. Yet I see an equal sign = next to 1, & an equal sign next to 3. How can an equal sign change the value of a number since an equal sign means equivalent, after all equal sign in an equation can’t change the value of a number.

In Python, the equal sign is not equivalence, but assignment.

a = 3

is not a comparison, but an assignment to the variable a of the value 3. We can still manipulate the value of a,

a = a + 3
print (a)    # 6

Mathematically it makes very little sense, but in programming we refer to the right side as an expression and the left side as the reference.

The expression is evaluated first and the outcome is assigned back to the same variable.

A comparison would use ==, as in,

print (a == 6)    # True