0
How to swap two or more numbers using if conditions in python?
Using if conditions only
8 ответов
+ 2
[4:4] @brattham (2020-04-25)
Actually Python helps us out here. Before Python starts it’s assignment process, it is saving its the initial values in its own temporary copy T of the tuple (b, a). That gives that
T = (33, 10).
So T will not be affected by the assignments process, although references to a and b will be changed under the assignment process. That is because to Python, the assignment looks more like this:
Python knows that
a = 10 and b = 33
So when we try to assign:
a, b = b, a
Python first starts to creates its own tuple:
=> T = (b, a) <=> T = (33, 10)
So it’s ‘easy’ for Python to see that
=> (a, b) = T <=> (a, b) = (33, 10)
And after the swap (it is true that):
a == 33 and b == 10.
So the conclusion is:
a, b = b, a
works fine for swapping the variables references to each other’s objects, and we can feel save doing so.
Regards
/Per B 🙂
+ 3
Just swap them normally
a, b = b, a
Why must you use if conditions?
+ 2
About swapping variables
[1:4] @brattham (2020-04-25)
First: If-statements and assignments have really nothing to do with each other, and can be used separately from each other.
Now: Let a = 10; b = 33.
Then a and b are two references to the two integer objects 10 and 33. So if you ‘swap’ a and b with each other, you swap a:s and b:s references to the to the place in the memory where the objects are.
Before you can swap the two variables (a and b) references to the two objects (10 and 33), you first have to save of a copy of the information contained in one of the variables, for example a, in a temporary variable, for example t, before the value contained in b can be copied to a. Otherwise the reference to the original object (= 10), will be lost.
We know that a = 10; b = 33.
So to swap a and b:s references with each other, let:
t = a; a = b; b =t
=> print(’a=’, a, ’ b=’, b)
>> a= 33 b= 10 # Swapped!
- - -
+ 2
[2:4] @brattham (2020-04-25)
Python allows tuple assignments. With tuple assignment, Python ’unpack’ the values to the right in the assignment expression and assign the values to the reference name inside the tuple to the left in a positional order.
(a, b) = (10, 33) => a = 10; b = 33
A generalization of this is the sequential assignment, in which you can skip the parentheses:
a, b = 10, 33 => a = 10; b = 33
Now when we have seen that sequential assignments is possible, the question is: can we swap the references at a and b so they pointing on each other’s objects, using sequences assignments. And: if so, is it possibility to do it without saving any values in some temporary variable?
So once more, we let:
a = 10; b = 33.
Now we want to swap a and b so
a = 33 and b = 10 (our goal).
- - -
+ 2
[3:4] @brattham (2020-04-25)
So for a tuple, we want:
(a, b) = (33, 10).
But for the moment,
a = 10 and b =33,
so it would be nice if we could use our references who are pointing at the objects values and just write:
(a, b)= (b, a),
or even more simplified, using sequential assignment:
a, b = b, a
And the thing is: it actually works! But why?
It first feels a bit risky at a first glance! When the assignments process starts, the value of a and b starts to change reference and begin to point at each other’s objects, so how does we know that it works?
- - -
+ 1
Wher is your try?
+ 1
Per Bratthammar well , thanks
0
Per Bratthammar iam beginer can u explain in more detail