Swap two variables
Traditional Way
Python3
first = 1 second = 2 temp = first first = second second = temp |
Here, we have used a variable temp to store the value of the variable first so that it doesn’t get lost while swapping
Python One Liner
Python3
first = 1 second = 2 first, second = second, first print (first, second) |
After the execution of the above code, the value of variable first will become what it was for second i.e. 2 and that of second will become 1.
10 Useful Python One Liners That Developers Must Know
Python is known for its easy-to-code and intuitive syntax. And one-liners are like the cherry on the cake which makes Python a more beautiful and beloved programming language. There are numerous features in Python Language and one of its most loved features is its one-liners. Writing Code in Python is already easy compared to other programming languages and using the one-liners makes it more easier and cool. Now let’s see what are these Python one-liners.