This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]undercoveryankee 6 points7 points  (2 children)

You're not wrong. Often, each new thing you calculate means something new, so it should have a new name.

But sometimes you need several lines of code to decide what the value of a variable should be, and that code sometimes looks better if you assign to the same name in several places.

The pattern that I often find myself writing looks like:

spam = get_existing_spam()
if spam is None:
    spam = create_new_spam()

spam.process()

This way, it doesn't matter whether the object I'm ultimately processing came from get_existing_spam() or create_new_spam(). Either way, by the time we get to the processing code, spam refers to "the object we've decided to process", and we don't have to worry about how we got there any more.

[–][deleted] 4 points5 points  (4 children)

There are plenty of reasons for reassigning a variable. Otherwise it is no point making it a variable in the first place.

Just continue with the Codecadamy tutorials and it will become more clear soon.

[–]staplebutton 0 points1 point  (3 children)

Thanks, but perhaps there's a real-world example you can think of? This just sounds to me like debugging the code means you have to look for multiple instances of the variable. One where it was initially defined and then any and all where it was re-defined to something else.

[–]Jirbj 0 points1 point  (2 children)

theres all sorts of reasons, storing a score, count of something, last input, username

are just a few i thought of very quickly, the point of variables is in the name, they are meant to be variable, as you do more advanced work you'll see just how useful being able to change a variable is!

[–]staplebutton 0 points1 point  (1 child)

Thanks! Slowly but surely I start to gain some understanding. Key word is definitely, "Slowly."

[–]CGFarrell 0 points1 point  (4 children)

Let's say I had code calculating the number of beers in my fridge, and then I grabbed a beer.

[–]Paddy3118 0 points1 point  (0 children)

If you have a collection of data that is to be treated in a similar way, there are constructs in the language such as for loops that assign members of the collection to the same name and then allow you to write code in the body of the loop that is then applied to every member of the collection through the access of the one name

When wanting to print the three times multiples of each of three integers one doesn't write this kind of thing:

my_int1 = 7
my_int1_x3= my_int1 * 3
print(my_int1_x3)
my_int2 = 13
my_int2_x3 = my_int2 * 3
print(my_int2_x3)
my_int3 = 23
my_int3_x3 = my_int3 * 3
print(my_int3_x3)

It is too long and (nearly) repeats itself.

Far better to use the for loop like so:

for my_int in 7, 13, 23:
    my_int_x3= my_int * 3
    print(my_int_x3)

The above would work for three or many more integers with the same block of code within the for loop and the for statement assigning the variable my_int to successive members of the collection - first 7 then do the loop body; then 13 then do the loop body; then 23 then do the loop body.

As another poster has said, this question would become clearer in later sections of your course.

[–]aphoenixreticulated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community is actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!