Overview of Swapping in Python
Several programming languages help us to apply logic and develop applications. Python is among those programming languages and one of the most preferred programming languages due to its vast range of inbuilt functions. Though it has pre-defined functions for almost all of the complex logic, one can define their own functions in order to develop the customized application. In this section, we will see how two variables could be swapped using the python programming language.
How to Swap two numbers in Python?
To swap two variables, we will require to use a temporary variable that will help us by temporarily storing the values. So that we can swap the values, we will need the values first. There are two ways of putting the values in the variable. Either we can assign the values or take values from the user while the program runs. To assign the values, we can simply define a variable and can initialize it. But if we need to take the input from the user, we will need to use the function called input. The input function lets us take the value from the user so that it could be used for further processing. Below is the program to swap two values. Also, we have attached the image with the output so that you can get a real idea of how the output will be when the below-given code runs.
# taking values from the user
x = input()
y = input()
print('The value of x is {}'.format(x))
print('The value of y is {}'.format(y))
# swapping the values
temp_var = x
x = y
y = temp_var
print('The value of x after swapping is {}'.format(x))
print('The value of y after swapping is {}'.format(y))
In the above example, we have used comments to make you aware of the reason behind the particular set of code. Hash(#) is used to comment on the line. Anything written after hashing till the end of the line is ignored by the compiler while compiling the program; the next two lines are used to take the input from the users so that the values could be used in swapping. After that, the print function has been used, which serves the purpose of print or echo anything on the screen. Later the logic to swap the values has been applied, and eventually, the swapped values have been applied. In the beginning, we have specified the values assigned to x and y so that it could be visible that what values have been given by the user and to ensure that the expected variable is holding the expected values. Once the swapping of the values is completed, we have shown in the output as well to make sure that the values have been swapped.
How to Swap three numbers in Python?
This code of code is similar to that of the previous code section that was intended to swap two numbers. This section of code will see how three numbers could be swapped using the temporary variable. The way to take the value from the user is similar to that of the previous code that is by using the input function. Later the values have been displayed using the print function.
The user has taken the values swapped. It can be of any data type as python can assign the data type to the variable dynamically by analyzing the input from the user. In other programming languages, the developer is supposed to assign the data type to the variable, and the variable becomes bound to hold the value of that particular data type only, but it is not in the case of python. These are some of the plus points that python provides to make working easily with it.
# taking values from the user
x = input()
y = input()
z = input()
print('The value of x is {}'.format(x))
print('The value of y is {}'.format(y))
print('The value of z is {}'.format(z))
# swapping the values
temp_var = x
x = y
y = z
z = temp_var
print('The value of x after swapping is {}'.format(x))
print('The value of y after swapping is {}'.format(y))
print('The value of z after swapping is {}'.format(z))
In this program, we have taken the input from the user. 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. Then the value of y is assigned to x, and the value of z is assigned to y. At last, the value of temp_var that was having the value of x has been assigned to z, and in following this way, all the three values have been swapped.
When the program runs, it will show all the values that have been assigned to different variables so that we can confirm the values that have been assigned to the variables. Once the swapping is completed after applying the swapping logic, we have shown that in the output screen to ensure that the values have been swapped. In the below image, the above-mentioned code has been run to see what actually gives the output when this code is compiled. The numbers 1,2, and 3 written at the beginning of the output screen are actually the input that the user accepted, and once the input was received, the logic was performed to swap the values.
Conclusion – Swapping in Python
Python provides us with several inbuilt functions that can simply take the users’ input and give the desired output. But when it comes to leveraging python to develop the actual application for any organization, it is always preferred to write the codes in a customized manner, and that is why it is important to develop the skills related to logic. This section will give you an idea about how the two values could be swapped. Using the same program, you can also swap other values. For instance, one can swap the first and the last name of any user in the application where it will be required.
Recommended Articles
This is a guide to Swapping in Python. Here we discuss how the use of python programming language could be swapped using two variables in detail. You may also look at the following article to learn more –