Python Program to Swap Two Variables

Опубликовано: 01 Сентябрь 2019
на канале: IT WORLD
19
0

Using temporary variable :
In this program, we use the temp variable to temporarily hold the value of x. We then put the value of y in x and later temp in y. In this way, the values get exchanged.
Without Using Temporary Variable :
In python programming, there is a simple construct to swap variables. The following code does the same as above but without the use of any temporary variable.

x,y = y,x
If the variables are both numbers, we can use arithmetic operations to do the same. It might not look intuitive at the first sight. But if you think about it, its pretty easy to figure it out.Here are a few example
Addition and Subtraction

x = x + y
y = x - y
x = x - y
Multiplication and Division

x = x * y
y = x / y
x = x / y


На этой странице сайта вы можете посмотреть видео онлайн Python Program to Swap Two Variables длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь IT WORLD 01 Сентябрь 2019, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 19 раз и оно понравилось 0 зрителям. Приятного просмотра!