Showing posts with the label Swap

Python: How to Swap Variables with Tuple

S wap two strings no need to define third variable in Python explained. To achieve this, you need a tuple , explained with an example of how to do it. Python ideas How to Swap Two Strings in Python You can swap two strings in Python using a tuple. You can have multiple values in a tuple. In this example, I have given a and b. When you use the swap function, it swaps the values. def swap(a, b):  return b,a  Example to Swap Two Strings. i = "Hello world" j = "This is ApplyBigAnalytics"  i, j = swap(i, j)  print(i)  print(j)  Example to Swap Two Numbers. i = 1  j = 2  i, j = swap(i,j)  print(i)   print(j) The Result is: This is ApplyBigAnalytics  Hello world  2  1 Keep Reading The Real Purpose of Underscore in Python