Skip to main content

Swap Variables in Python

5 Ways to Swap Variables in Python:

5 Ways to Swap Variables in Python
(Image by - Sharma Guides | Subham232330)


1. Using a Temporary Variable -

a=2
b=3
temp=a
a=b
b=temp
print(a)
print(b)

Output:-
a=3
b=2





2. Using Arithmetic Operators -

a=2
b=3
a=a+b  # a=5, b=3
b=a-b  # a=5, b=2
a=a-b  # a=3, b=2
print(a)
print(b)

Output:-
a=3
b=2




3. Using Comma -

a=2
b=3
a,b=b,a
print(a)
print(b)

Output:-
a=3
b=2





4. Using XOR(^)

a=2
b=3
a=a^b
b=a^b
a=a^b
print(a)
print(b)

Output:-
a=3
b=2





5. Using Multiplication and Division -

a=2
b=3
a=a*b
b=a/b
a=a/b
print(a,b)

Output:-
a=3, b=2




Comments

Popular posts from this blog

SQL Injection

SQL Injection The SQL Injection is a code penetration technique that might cause loss to our database. It is one of the most practiced web hacking techniques to place malicious code in SQL statements, via webpage input. SQL injection can be used to manipulate the application's web server by malicious users. SQL injection generally occurs when we ask a user to input their username/userID. Instead of a name or ID, the user gives us an SQL statement that we will unknowingly run on our database. For Example - we create a SELECT statement by adding a variable "demoUserID" to select a string. The variable will be fetched from user input (getRequestString). demoUserI = getrequestString("UserId"); demoSQL = "SELECT * FROM users WHERE UserId =" +demoUserId; Types of SQL injection attacks SQL injections can do more harm other than passing the login algorithms. Some of the SQL injection attacks include: Updating, deleting, and inserting the data: An attack can mo...

Schedule in DBMS

Schedule A series of operation from one transaction to another transaction is known as schedule. It is used to preserve the order of the operation in each of the individual transaction. 1. Serial Schedule The serial schedule is a type of schedule where one transaction is executed completely before starting another transaction. In the serial schedule, when the first transaction completes its cycle, then the next transaction is executed. For example: Suppose there are two transactions T1 and T2 which have some operations. If it has no interleaving of operations, then there are the following two possible outcomes: Execute all the operations of T1 which was followed by all the operations of T2.  Execute all the operations of T1 which was followed by all the operations of T2.  In the given (a) figure, Schedule A shows the serial schedule where T1 followed by T2. In the given (b) figure, Schedule B shows the serial schedule where T2 followed by T1. 2. Non-serial Schedule If interle...

Computer Short Questions

Computer Short Questions & Answers: 1. What is any part of the computer that you can physically touch? – Hardware 2. Which generation of computers is still under development? – Fifth 3. What is the most common storage device for the personal computer? – Hard Disk Drive 4. Which key is used in combination with another key to perform a specific task? – Control 5. What is the pattern of printed lines on most products? – Barcodes 6. To make the number pad act as a directional arrow, we press which key? – Shift 7. Which devices let the computer communicate with you? – Input 8. What is the most frequently used piece of hardware for inputting data? – Hardware 9. What is the place where the computer stores programs and data? – Storage unit 10. What is the process of dividing the disk into tracks and sectors? – Formatting 11. What is the space in your computer that loads’ and works with data? – RAM memory 12. What is the storage which stores or retains data after power off? – Non-volatile s...