Загрузка...

🔁 Swap two numbers without temp in python #shorts #coding

Learn how to 🔁 Swap any two numbers without using a temporary variable in python

This is a classic logic question asked in many coding interviews and online assessments

Tags:

#swapnumbers #pythonshorts
#logicbuilding #codingtricks
#python

_______________________________________________

Explanation:

Swapping two numbers without using a temp variable works in python using Arithmetic Operations.

How it works:

let's take 👉 a = 3
👉 b = 5
___________________________________

✨ Step-1:

a= a+b # a= 3+5 =8

now: a= 8
b= 5
___________________________________

✨ Step-2:

b= a-b # b= 8-5 = 3

now: a= 8
b= 3 ( at starting I take a=3 )
___________________________________

✨ Step-3:

a= a-b # a= 8-3 = 5

now: a= 5 ( ai starting I take b= 5)
b= 3
___________________________________

Result:
a = 5
b = 3

Swapped successfully ✅ without using a temporary variable

Видео 🔁 Swap two numbers without temp in python #shorts #coding канала CodeZen Studio
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки