Swapping values

0 votes

I have seen this code used to swap the values of two variables -

a, b = b, a

How does this work?

Jun 8, 2018 in Python by nightshade
• 870 points

recategorized Jun 11, 2018 by Sudhir 14 views

1 answer to this question.

0 votes
a,b = b,a

Here, python interprets the comma separated variables as tuples. The RHS is first evaluated as a tuple and then stored in memory. The LHS is then evaluated as a tuple and is assigned the values of the RHS. Hence, the net effect is that the values of the variables are swapped.

answered Jun 8, 2018 by Hamartia's Mask
• 1,580 points

Related Questions In Python

+2 votes
2 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,310 points
3,855 views
0 votes
1 answer

Looping over dictionary values

You can loop over the dictionary and ...READ MORE

answered Apr 17, 2018 in Python by Nietzsche's daemon
• 4,260 points
17 views
0 votes
1 answer

Returning values in python

Place your values in a tuple and ...READ MORE

answered Apr 26, 2018 in Python by Nietzsche's daemon
• 4,260 points
14 views
0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 56,160 points
575 views
0 votes
1 answer

How to Parse values from a JSON file?

import json from pprint import pprint with open('data.json') as ...READ MORE

answered Oct 15, 2018 in Python by Priyaj
• 56,160 points
38 views
0 votes
1 answer

Array values in Python

You can use the enumerate function and ...READ MORE

answered Nov 28, 2018 in Python by SDeb
• 12,280 points
23 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 124 views
+4 votes
6 answers