Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap Three Numbers In Single Statement

Tags:

c

logic

Is there any possiblty to swap three numbers in a single statement

Eg :

  • a = 10
  • b = 20
  • c = 30

I want values to be changed as per the following list

a = 20
b = 30
c = 10

Can these values be transferred in a single line?

like image 545
Prabhu Avatar asked Dec 26 '11 12:12

Prabhu


People also ask

How do you swap 3 numbers in Python?

The user has to provide three inputs that will be assigned the three variables x, y, and z. The values will be later used to get swapped among the variables. In the swapping logic, first, the value of x has been temporarily stored in the variable named temp_var.


1 Answers

$ python
>>> a, b, c = 10, 20, 30
>>> print a, b, c
10 20 30
>>> a, b, c = b, c, a
>>> print a, b, c
20 30 10
like image 174
kev Avatar answered Sep 26 '22 23:09

kev