Https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-syntax/exercises/variables

In the Python 3 lesson under Variables, my question is:

If you change a variable such as in:

Greeting

message_string = “Hello there”
print(message_string)

Farewell

message_string = “Hasta la vista”
print(message_string)

Is the message_string = “Hasta la vista” from here on out forever? Or was it just changed for the specific context of a farewell?

If so, how can I change a variable only under a specific context (like a farewell) without changing the original meaning throughout the rest of the code?

Hey there @alexguerrero97112389!! Welcome to the forums!! :grinning:

If you create a variable:

x = 7

and then modify it:

x = 15 #x is now equal to 15

Than it will keep this new value until it is changed again.

I don’t believe there is a specific way to do this, usually you would simply use multiple lines of code:

message = "hello" #creates a variable and assigns a value
print(message) #prints hello

message = "farewell" #reassigns message to equal farewell
print(message) #prints farewell

message = "hello" #reassigns message to hello
2 Likes

As @8-bitgaming said, variables keep their changed values. You could create a copy of a variable:

a = 9
b = a
a = 6

In this situation, b still equals 9, even after the last line of code.

2 Likes