Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python swap numbers

x, y = 10, 20
print(x, y) 	# 10 20
x, y = y, x 
print(x, y) 	# 20 10
Comment

python swap numbers

# Swap without using any temp variable
x = 66
y = 77 

print("Original: ", x,y)

x = x - y
y = x + y
x = y - x 

print("Swapped 1st time: ", x,y)

x = x + y
y = x - y
x = x - y 

print("Swapped 2nd time: ", x,y)

# Output:
# Original:  66 77
# Swapped 1st time:  77 66
# Swapped 2nd time:  66 77 
Comment

python swap two numbers

def swap(a, b):
  a = a + b
  b = a - b
  a = a - b
  return a, b
Comment

python swap two numbers

pos1, pos2  = 1, 3
Comment

how to swap numbers in python mathematically

def swap_by_temp_var(num1,num2):
    temp = num1
    num1 = num2
    num2 = temp
    return num1, num2
def swap_by_mathamatically(num1,num2):
    num1 = num1 + num2
    num2 = num1-num2
    num1 = num1-num2
    return num1, num2
print(swap_by_temp_var(100,80))
print(swap_by_mathamatically(100,80))
Comment

PREVIOUS NEXT
Code Example
Python :: nested python list 
Python :: camp cretaceous.com 
Python :: na in python 
Python :: timeit command line 
Python :: python anonymous object 
Python :: optimization in python 
Python :: creating dynamic variable in python 
Python :: python random distribution 
Python :: qtablewidget add row python 
Python :: Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming 
Python :: make guessing game by python 
Python :: tkinter bind function with arguments 
Python :: Reverse an string Using Reversed 
Python :: Get git sha 
Python :: restricting user access to web pages 
Python :: how to change color of square in pygame with keypress 
Python :: python while loop and recursion 
Python :: light fm cold start problem 
Python :: stackoverflow ocr,cropping letters 
Python :: sphinx, where to write the glossary of a sofware project 
Shell :: error: cannot install "code": classic confinement requires snaps under /snap or symlink from /snap 
Shell :: ubuntu XAMPP Starting Apache...fail 
Shell :: git update gitignore 
Shell :: how to restart nginx 
Shell :: install imutils 
Shell :: restart redis ubuntu 
Shell :: install redis on mac pro 
Shell :: install wps ubuntu 20.04 multilanguage 
Shell :: linux sort folders by size 
Shell :: Building wheels for collected packages: opencv-python Building wheel for opencv-python (PEP 517) ... 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =