Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
Swapping variables is a fundamental operation in programming that allows us to rearrange and manipulate data effortlessly. Whether you're a beginner programmer or an experienced developer, mastering the art of swapping of two numbers in Python is an essential skill that will enhance your problem-solving capabilities and streamline your coding endeavors.
Here, we will dive into variable swapping and explore different techniques to swap two numbers in Python.
By understanding and applying these programs, you'll gain a powerful tool in your programming arsenal. Swapping variables can be used in various scenarios, such as sorting algorithms, data manipulation, efficient memory management, and even in solving complex mathematical problems. It enables you to efficiently reassign values, update states, and simplify the logic of your programs.
Whether you're a student embarking on your coding journey, a seasoned developer seeking elegant solutions, or an enthusiast eager to expand your knowledge, this tutorial is designed to equip you with practical techniques to effortlessly write a program to swap two numbers in Python.
Program to Swap Two Numbers in Python
Code
# Swap values of two numbers
# Input the numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Swap the numbers using a temporary variable
temp = num1
num1 = num2
num2 = temp
# Print the swapped numbers
print("After swapping:")
print("First number:", num1)
print("Second number:", num2)
Output
Enter the first number: 5
Enter the second number: 3
After swapping:
First number: 3.0
Second number: 5.0
Explanation
-
We first take input from the user for the two numbers.
-
Next, we swap the values of the numbers using a temporary variable. The temporary variable (temp) stores the value of the first number (num1), then we assign the value of the second number (num2) to num1, and finally, assign the value of temp (which holds the original value of num1) to num2.
-
Finally, we print the swapped numbers.
Swap Two Numbers Without Using Third Variable in Python
Code
# Swap without using 3rd variable
# Input the numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Swap the numbers without using a third variable
num1, num2 = num2, num1
# Print the swapped numbers
print("After swapping:")
print("First number:", num1)
print("Second number:", num2)
Output
Enter the first number: 6
Enter the second number: 9
After swapping:
First number: 9.0
Second number: 6.0
Explanation
-
We take input from the user for the two numbers.
-
Next, we swap the values of the numbers using a technique called multiple assignment. By assigning the values num2, num1 to num1, num2 respectively, the values of the variables are interchanged without the need for a third variable.
-
Finally, we print the swapped numbers.
By running this program and providing the initial values for the two numbers, it will swap the values and display the result.
This approach leverages the power of Python's multiple assignment feature, allowing us to swap values between variables in a concise and elegant manner, without the need for a temporary variable.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program