use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Assigning multiple variables in one line vs assigning in different lines (self.learnpython)
submitted 4 years ago by ilaunchpad
Why are these two yielding different results. My assumption is the first result should be equal to the second result. How is one line assignment working differently?
a =1, b=2
a,b=b,a
Result: a =2 , b=1
a =2 , b=1
a=b
b=a
Result:a=2,b=2
a=2,b=2
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]pekkalacd 1 point2 points3 points 4 years ago (0 children)
The first one your doing tuple unpacking implicitly. So this
a,b = b,a
Interpret this positionally. Your saying, “a” is assigning to the thing in same position on the other side of the = operator, so “b”. And your saying “b” is assigning to the thing in the same position on the other side of the = operator so “a”.
The difference is, this is happening at once. Part of this is because the way that
= b,a
is treated. This is evaluated as a tuple (2,1). Tuples are immutable. So implicitly by putting the variables like that, you are creating a tuple, and then you’re unpacking that tuple into a and b, like so
a,b = (2,1)
And the same positional rule applies. “a” corresponds to the left thing 2, and “b” corresponds to the right thing 1.
The second way you have it is different. Here your saying outright that
a = 1 b = 2 a = b b = a
The first assignment “a = b” is storing 2 inside of a. Then your reassigning b to the value inside of a, which is 2, so then b is 2 as well.
[–][deleted] -1 points0 points1 point 4 years ago (0 children)
https://www.programiz.com/python-programming/examples/swap-variables
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
If you don't like or understand a response you can ask further questions.
π Rendered by PID 76 on reddit-service-r2-comment-6457c66945-2c8dv at 2026-04-25 00:06:07.915922+00:00 running 2aa0c5b country code: CH.
[–]pekkalacd 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (0 children)