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
• 880 points

recategorized Jun 11, 2018 by Sudhir 565 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
4 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,510 points
122,499 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
643 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
435 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
• 58,020 points
11,777 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
• 58,020 points
2,030 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
• 13,300 points
840 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,467 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,127 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP