TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Swap Two Numbers Without a Temp in Python
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python ExamplesPython Tutorials

Swap Two Numbers Without a Temp in Python

Last updated: Nov 05, 2023 12:06 am
By Meenakshi Agarwal
Share
2 Min Read
Python program to swap two numbers
Python program to swap two numbers
SHARE

In this sample program, you will learn to swap two numbers without using a temporary variable and show the result using the print() function.

To understand this demo program, you should have the basic Python programming knowledge:

  • Python Data Types
  • Python Operators

Introduction

There are two main methods for swapping two numbers without a temp variable in Python:

Using arithmetic: This method uses the addition and subtraction operators to swap the values of the two numbers.

Using bitwise XOR: This method uses the bitwise XOR operator to swap the values of the two numbers.

In the sample below, we are taking inputs from the user and storing them in two different variables. To swap them without a temp variable, we need to add both numbers and store the result in the first one. Next, we’ve to subtract the second variable from the first one and save the result for the second one.

Finally, we’ll use the first variable to subtract from the second and also to store the result of this operation. At this point, both variables have swapped their values. We’ll now print the result.

Sample Code: Swap Two Numbers Without Temp Variable

Here is an example of a Python program to swap two numbers without a temp variable using the arithmetic method:

# This program swaps two numbers

int1 = int(input("Enter first number: "))
int2 = int(input("Enter second number: "))

print('Old value of int1 is {0} and int2 is {1}'.format(int1, int2))

int1 = int1 + int2
int2 = int1 - int2
int1 = int1 - int2

# Display the result
print('New value of int1 is {0} and int2 is {1}'.format(int1, int2))

The output of the above code is as follows:

Enter first number:  11
Enter second number:  22
Old value of int1 is 11 and int2 is 22
New value of int1 is 22 and int2 is 11

By the way, as we have said in the beginning you can use the Python xor operator for the same purpose, so you can also try it. Moreover, you may even like to explore our list of 40 Python exercises for beginners.

Conclusion

Overall, swapping without a temp variable is a simple and efficient technique that can be used in a variety of Python programs. Here are some additional points for you to think over.

  • Performance: The two methods for swapping two numbers without a temp variable in Python have similar performance. However, the bitwise XOR method is slightly faster for integers.
  • Memory usage: Both methods for swapping two numbers without a temp variable in Python use the same amount of memory.
  • Use cases: Swapping two numbers without a temp variable can be useful in situations where memory usage is a constraint, such as in embedded systems. It can also be useful in situations where performance is a constraint, such as in real-time systems.

Also Check: Python Program to Print Diamond Pattern

Also, do let us know if you seek our help in creating any Python program which you are facing difficulty with. We’ll promptly provide you with the desired solution.

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python program - sum of two numbers Python Program to Find Sum of Two Numbers
Next Article Generate Random Integer Numbers Python Program to Generate Random Integers

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x