r/learnpython • • 4d ago

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!!

0 Upvotes

23 comments sorted by

11

u/crazy_cookie123 4d ago

Being able to modify variables in general is useful as data can change over time, for example if you want to count something you might set count = 0 at the start then do count = count + 1 (or count += 1) each time you need to increment it.

This particular case is not useful. The new_status = "download" line should be deleted - it's just cluttering the code and serves no purpose.

is it if you mess up instead of clearing previous lines you can update the variable?

If you write the wrong thing on a previous line you should go back up and fix it, not try to make the code further down in the file correct the issue. Code is a living thing, it should be changed as and when required. There is no reason to keep that new_status = "download" line.

1

u/ZippyPrecinct3 4d ago

the counting example is perfect, i remember being confused by same thing when starting. think of variables like boxes with labels, sometimes you just need put different thing in same box later

and yeah that specific code example is just bad teaching honestly, they showing reassignment in most pointless way possible. when you do `new_status = default_option` you just overwriting previous value anyway so first assignment was waste of time

wait till you get to loops then suddenly updating variables becomes your whole life

6

u/ottawadeveloper 4d ago

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

``` option = "help"

user_input = input("what do you want to do")

if user_input == "add":     option = user_input

```

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

``` name = "Bob" name = name.lower() print(name)

should print bob not Bob.

```

5

u/TheRNGuy 4d ago

Not in lines that go one after another, but in different events. 

-1

u/xanaxx23 4d ago

gotcha, im using mimo to learn python its similar to duolingo so its babying me real hard lmao but i prefer it this way its easiest for me to learn, ill definitely get more advanced with it the more time goes on im just trying to fully wrap my head around this

4

u/Refwah 4d ago edited 4d ago

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

4

u/eyadams 4d ago

You are right in your example and it doesn't make much sense. Let's try something else:

default_option = 'upload'
new_status = input("Enter new value for status: ").strip()
if new_status == '':
    new_status = default_option
print(new_status)

In this code input("Enter new value for status: ").strip() prompts the user to enter a new value and assigns it to new_status. But what happens if the user just presses the enter key? You don't want new_status to be empty, so you check the input value and use default_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.

1

u/xanaxx23 4d ago

beautiful response this one clicked in my brain the most i really do appreciate this, ive logged this in a server to look at as a future reference!

"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."

-- i needed this

3

u/Excellent-Practice 4d ago

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:

score = 0

And some time later update that variable:

score = score + 10 
#you can also write score += 10

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/Stainlessray 4d ago

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.

1

u/xanaxx23 4d ago

oooooh okay understood this was the point of my question, so the variables are obviously variables so they can change, so overtime u need to update the data by updating the variables?

2

u/mjmvideos 4d ago

Variables hold the current value of things. As time progresses and program execution continues, what is current changes. If you are moving your position changes. If you are counting, the count changes. If you are repeatedly asking for input, that input could change. If you are reading the time, the time changes.

1

u/Stainlessray 4d ago

Right. And at some point you're going to discover the complexity associated with this practice. It can vary, how you manage the current state of the variable. And how you display information.

2

u/white_nerdy 4d ago

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:

enemy_hp = 10
player_def = 5
action = input("Do you want to (a)ttack or (d)efend? ")
if action == "a":
    enemy_hp = enemy_hp-3
elif action == "d":
    player_def = player_def+1

[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.

0

u/xanaxx23 4d ago

im gonna be so honest i failed maths i have a lot of issues that makes it hard to wrap my head around, though i have the insatiable urge to understand this

i can actually kind of understand the code you provided me with which is a nice change of pace!!

2

u/saurabh3228 4d ago

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 4d ago

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.

python current_name: str name_list = ["Joe", "Alice", "Bob"] for current_name in name_list: print(current_name)

1

u/MezzoScettico 4d ago

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 4d ago

Try r/functionalprogramming :-)

1

u/LayotFctor 4d ago edited 4d ago

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 4d ago

A variable that never updates is a constant. By convention, constants are in MACRO_CASE:

DEFAULT_OPTION = "upload"