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

python swap two elements

a = 1
b = 2
# Swap a and b
a, b = b, a
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

python program for swapping position of two numbers

#program to swap indexes of present to succedinng one or viceversa
list=eval(input("enter a list"))
print(list)
a=len(list)
if a%2!=0:
    a=a-1
for i in range(0,a,2):
    list[i],list[i+1]=list[i+1],list[i]
print("swapped position and the list is",list)
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

PREVIOUS NEXT
Code Example
Python :: how to get pygame key 
Python :: km/h a m/s 
Python :: Write a Python function to check whether a number is in a given range. 
Python :: console.log() python 
Python :: adding static file and its usage in Django 
Python :: how to check if a list is a subset of another list 
Python :: os system python 
Python :: internal server error 500 python flask 
Python :: pandas add list to dataframe as column 
Python :: fixed precision float python 
Python :: pgcd python 
Python :: pandas length of array in column 
Python :: series.Series to dataframe 
Python :: pandas create a calculated column 
Python :: loop through 2 items python 
Python :: deleting dataframe row in pandas based on column value 
Python :: python exceptions 
Python :: python file size in bytes 
Python :: how to import include in django 
Python :: import error in same directory python 
Python :: tqdm python 
Python :: shebang python 
Python :: calculate age python 
Python :: python log10 
Python :: django static files / templates 
Python :: how to round off values in columns in pandas in excel 
Python :: python how to change back to the later directory 
Python :: solve sympy 
Python :: after groupby how to add values in two rows to a list 
Python :: Python Requests Library Put Method 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =