Different Methods in Python for Swapping Two Numbers without using third variable10 Jan 2025 | 4 min read In this tutorial, we will discuss different methods used for swapping two variables (n1 and n2) without using a third variable in Python programs. Example: Method 1: By using inbuilt methodThe inbuilt method can work with any data type values such as string, float, it. This method is very easy to use. Example: Output: Variables Value Before Swapping: Value of P: JavaTpoint Value of Q: Tutorial Variables Value After Swapping: Value of P: Tutorial Value of Q: JavaTpoint Method 2: By using Bitwise XOR operatorThe Bitwise XOR method only works with integers, and it works faster as it uses bit operation that is for same value result = 0 and for different value result = 1. Example: Output: Variables Value Before Swapping: Value of P: 5 Value of Q: 10 Variables Value After Swapping: Value of P: 10 Value of Q: 5 Method 3: By using Addition and Subtraction OperatorsThis method can be used only for numerical values. Example: Output: Variables Value Before Swapping: Value of P: 112 Value of Q: 211 Variables Value After Swapping: Value of P: 112 Value of Q: 211 Method 4: By using Multiplication and Division OperatorsThis method can be only used for numerical value except 0. Example: Output: Variables Value Before Swapping: Value of P: 11.2 Value of Q: 21.1 Variables Value After Swapping: Value of P: 21.1 Value of Q: 11.2 Method 5: By using both Bitwise operators and Arithmetic operatorsIn this method, we will use both bitwise operators and arithmetic operators. This method only works with integers and not with float types. Example: Output: Variables Value Before Swapping: Value of P: 112 Value of Q: 211 Variables Value After Swapping: Value of P: 211 Value of Q: 112 ConclusionIn this tutorial, we have discussed different methods used for swapping values of two variables without using the third variable. Next TopicHow to Change Plot Size in Matplotlib |
In Python, reduce() is a built-in function that applies a given function to the elements of an iterable, reducing them to a single value. The syntax for reduce() is as follows: functools.reduce(function, iterable[, initializer]) The function argument is a function that takes two arguments and returns a single value....
5 min read
In this tutorial, we will discuss the use of the Eel library in the Python programming language in order to design a user interface for Python programs with the help of HTML, CSS, and JavaScript. We have divided the tutorial into two sections. The first section is...
5 min read
In Python, a string is a sequence of characters, and a list is a collection of elements that can be of any data type, including strings. Subtraction of a string list involves removing elements that are common between two lists or strings. To subtract one string list...
4 min read
In Python, a `container` is an object that holds other objects. Containers provide a way to organize and manage collections of data. Python provides several built-in container types, including `lists`, `tuples`, `sets`, and `dictionaries`. Each type has its own characteristics and use cases. Lists Lists are ordered collections...
2 min read
Introduction Sorting is a central operation in computer science, with applications going from information recovery to enhancing algorithm execution. Among different sorting algorithms, QuickSort stands out for its speed and productivity. Nonetheless, QuickSort's effectiveness intensely relies upon the decision of the pivot element. In this article, we...
4 min read
When we install the Python in our local machine, Python interactive shell comes by default. In our coding journey, we might find some exiting features of the standard REPL. The IPython is an advance version of the interactive Python that can enhance our coding capabilities. IPython...
13 min read
SNMP, short for Simple Network Management Protocol, is a necessary tool for SDN and is the best option to control devices in software. Even apart from this, in-application access is the primary purpose of SNMP. Without a doubt, all monitoring systems utilize SNMP in order to...
11 min read
In the ious tutorial, we have discussed various operations that we can perform on a PDF file using different packages and modules of the Python programming language. We have learned the working of different Python libraries which can be used to manipulate PDF files. Moreover, we...
8 min read
In the following tutorial, we will discuss a Python package called LanguageTool and understand how to create a simple grammar and spell checker using the Python programming language. So, let's get begun. Understanding the LanguageTool library in Python LanguageTool is an open-source tool used for grammar and spell-checking purposes,...
7 min read
Introduction: In this article, we are discussing parse date from string python. To convert a string to datetime in Python, use the strptime() method. strptime() is a built-in method of the datetime class used to convert a string representation of a date/time to a datetime object. Syntax...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India