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

swapping two numbers in pythin

x=int(input("Enter x:"))
y=int(input("Enter y:"))

# Method 1
x,y=y,x
print(x,y)

# Method 2
t=x
x=y
y=t
print(x,y)

# Method 3
x=x+y
y=x-y
x=x-y
print(x,y)

# Method 4
x=x^y
y=x^y
x=x^y
print(x,y)

# Method 5
x=x*y
y=x/y
x=x/y
print(x,y)
Comment

swapping of two numbers in python

a,b=5,6
print(a,b)  
a=a+b  
b=a-b   
a=a-b
print(a,b)
Comment

code to swap in python

a=5
b=10
a,b=b,a  #swapped 
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 :: py to exe 
Python :: python list to dict 
Python :: maior valor lista python 
Python :: how to encode emoji to text in python 
Python :: dynamic plot jupyter notebook 
Python :: python ctypes maximize window 
Python :: radians in python turtle 
Python :: python convert list of lists to array 
Python :: flatten a list 
Python :: python how to add to a list 
Python :: python regex to find year 
Python :: logarithmic scale fitting python 
Python :: input two numbers in python in a single line 
Python :: string in list py 
Python :: python get last element of array 
Python :: log log grid python 
Python :: range of y & x in scatter 
Python :: logarithms python 
Python :: increment decrement operator in python 
Python :: what if discord.py python add-in does not work 
Python :: what is django 
Python :: python mongodb schema 
Python :: jupyter notebook spark 
Python :: tkinter maximise window 
Python :: python code to get wifi 
Python :: pycharm update python version 
Python :: pandas write image to excel 
Python :: python discord bot 
Python :: python add 1 to 100 
Python :: create an array with a range of elements 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =