Member-only story

Interview question: Use Python to swap values of numeric values

Crystal X
2 min readJun 20, 2023

--

Any person who attends a Python developer’s interview may very well be asked to swap out the values of two variables, such as x and y. In this blog post I intend to demonstrate how to swap out values the way they are done in other programming languages and also the Pythonic way, so the aspiring Python developer will be knowledgeable of both ways.

I have written the code in Python 3.11 using Python’s IDLE, and have saved the code in a directory in my laptop. Python is a free open source program that can be found in the Microsoft store and installed on the computer. (I prefer to use software that is in the Microsoft store because I want my virus protection inbuilt into my laptop to stay intact.)

The screenshot below shows the code that I have used to swap the values. I have numbered each section of code, with the answers to the print function in parenthesis:-

  1. In the first code example, the variable x is defined as 1 and the variable y is defined as 5.
  2. In the second code example, the variable x becomes y (5) and y becomes x (5).
  3. In the third code example, the variable x is defined as 1 and the variable y is defined as 5.
  4. In the fourth code example, the variable, temp, is introduced to swap the values of x and y…

--

--

Crystal X
Crystal X

Written by Crystal X

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

No responses yet