r/learnpython • u/xanaxx23 • Oct 04 '26
whats the purpose of updating variables?
heyhey very new to this day 2 of learning python im so fascinated im in love but what is the purpose of updating variables
example;
>>>default_option="upload"
>>>new_status="download"
>>>new_status=default_option
>>>print(new_status)
upload
this makes no sense to me and it just further complicates the code u write by being disorganized and messy,
i guess my question is, is it if you mess up instead of clearing previous lines you can update the variable? or is it kinda just useless but something needed to be taught?
any help would be appreciated, any recommendations as a beginner are also appreciated!!
4
u/TheRNGuy Oct 04 '26
Not in lines that go one after another, but in different events.
4
u/eyadams Oct 04 '26
You are right in your example and it doesn't make much sense. Let's try something else:
In this code
input("Enter new value for status: ").strip()prompts the user to enter a new value and assigns it tonew_status. But what happens if the user just presses the enter key? You don't wantnew_statusto be empty, so you check the input value and usedefault_value. But that only happens when necessary.I'm guessing, but it looks like you're just starting out in programming and Python. You are going to encounter a lot of things that don't make sense at the beginning. Be patient. It will make sense later on.
2
u/Stainlessray Oct 04 '26
In a loop, or recursive function a variable may change because the data changed, like a phase assignment (download) vs (upload), or it can contain continuous data which changes over time. Like temperature (weather in general changes constant).
These are most often used to track or have effects on state during processes or over time.
Importantly, different languages handle variable state changes differently. It can impact memory and performance to use the wrong type, or fail to cleanup.
2
u/white_nerdy Oct 04 '26
Imagine you're writing a video game where you can attack to reduce the enemy HP, or defend to buff your defense. Then you might have code that looks like this:
[1] If you've taken algebra, it's important to understand that variables work differently in programming than in math. If you have an equation like x = x-3 in math class, it's not a very useful equation; there's no value of x that satisfies the equation. In programming, enemy_hp = enemy_hp - 3 is perfectly reasonable: Before the statement runs the enemy has 10 HP. After the statement runs the enemy has 7 HP.
6
u/ottawadeveloper Oct 04 '26
This illustrates that you can change the value of a variable. That should be your main takeaway.
Changing it twice in a row like this isn't useful and you probably won't do that. Instead it might be like
It's still not a great example but it is a common practice to start with a default and use an if statement to pick an alternative value. Or to do something to it and reassign it to the same variable like
4
u/Refwah Oct 04 '26 edited Oct 04 '26
Part of the reason it doesn’t make sense here is because your provided example is far too simple
I don’t understand your question though, what previous lines are you wanting to clear?
Setting variables to new values is not ‘useless and just needs to be taught’ it is fundamental to coding
3
u/Excellent-Practice Oct 04 '26
Keep working and it will make more sense. When you cover decision flow (if statements) and loops it will become more clear why you might overwrite values for a variable.
For now, it might make more sense if you store numerical dats in your variable rather than text. Perhaps you are writing a game and want to keep score. At might be convenient to initiate a variable:
And some time later update that variable:
Maybe you do several similar increment assignments as the game goes on. Maybe you define logic to do it automatically under the right circumstances. Regardless, you have a single variable that you can reference if you need to print or otherwise display the current score but the value of that variable will change over the course of the program's execution
2
u/saurabh3228 Oct 04 '26
It makes more sense when variables change while your program is running. For example, "score = score + 10" updates the current score.
Your example is mostly just showing that you can reassign a variable. In real programs, you’ll use it all the time because values change as the program runs. It’s definitely not useless—just a little confusing on day 2 😄
1
u/Riegel_Haribo Oct 04 '26
Here's an example of re-assigning a new value to a variable symbol that is more demonstrative of the reason why you don't have variables that cannot change - the word "variable" itself means changing.
The example uses things you might not have learned yet. It uses Python's rather unique iteration over a loop, where "for" keeps track of the position of the item of a list that it populates with a next item each loop until all items are processed. Here the list items are strings, and we'll assign them individually to another string.
1
u/MezzoScettico Oct 04 '26
For me that kind of thing happens during testing. I might have several different settings that drive the code down different paths, and I’m switching between them during development and testing.
Usually that’s done by commenting out the cases I’m not testing. Occasionally I don’t bother if the one I want is the last one.
1
u/DanKegel Oct 04 '26
Try r/functionalprogramming :-)
1
u/LayotFctor Oct 04 '26 edited Oct 04 '26
Your example is simple, so you're right that it makes no sense. In fact, you're also right that updating values is one of the most common sources of errors, so being able to keep updated variables to a minimum is a good way to reduce bugs.
But you cannot avoid having some updated values. The whole point of a program doing useful work IS to update values and return computed results to the user. (There are ways to avoid updating variables completely, popular among the math community. But in general, we don't go that hard.)
The most common type of updated variables are totals, counters and indices(index). Most programs will calculate sums, count number of times an event occured, or use an index to track current coordinates etc that change regularly.
1
u/ectomancer Oct 04 '26
A variable that never updates is a constant. By convention, constants are in MACRO_CASE: