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

Function Keys Shortcuts

Function Keys Shortcuts (F1 - F12) (Function Keys) F1 It is used to open the help window in almost all programs. Also used to enter BIOS or CMOS Some computers allow you to enter BIOS setup using different keys like F2, F10, Delete, Esc. Pressing Window + F1 will open the Microsoft Windows Help and Support Center. F2 In Microsoft Windows, it is used to rename an icon , file , or folder that the user selects. In Microsoft Excel, the F2 key allows you to edit the selected cell in the Excel sheet. In Microsoft Word, pressing Ctrl+F2 will open the print preview window and Alt+Ctrl+F2 will open the new file or document. In addition, it is also used to enter the CMOS setup. F3 It is mainly used to open a search function for many programs. At the MS-DOS or Windows command line, it gives users the option to repeat the last command entered. In Microsoft Word, if you press Shift + F3 , it allows you to change the selected text from uppercase to lowercase or a capital letter at the beginni...

Addressing Modes & Types of Addressing Modes

Addressing Modes & Types of Addressing Modes Addressing modes: The different ways of specifying the location of an operand in instruction are called addressing modes. Types of Addressing Modes: In computer architecture, there are the following types of addressing modes: Implied / Implicit Addressing Mode Stack Addressing Mode Immediate Addressing Mode Direct Addressing Mode Indirect Addressing Mode Register Direct Addressing Mode Register Indirect Addressing Mode Relative Addressing Mode Indexed Addressing Mode Base Register Addressing Mode Auto-Increment Addressing Mode Auto-Decrement Addressing Mode (Types of Addressing Modes) In this article, we will discuss these addressing modes in detail. 1. Implied Addressing Mode: In this addressing mode, The definition of the instruction itself specifies the operands implicitly. It is also called an implicit addressing mode. Examples: The instruction “Complement Accumulator” is an implied mode of instruction. In a stack-organized compute...

Object Oriented and Relational Database

Object Relational and Object-Oriented Database What Is Object Relational Database? An object-relational database (ORD) is a database management system (DBMS) that’s composed of both a relational database (RDBMS) and an object-oriented database (OODBMS). An object-relational database acts as an interface between relational and object-oriented databases because it contains aspects and characteristics from both models. Object-oriented database (ORD) serves two main purposes: It connects the divide between relational databases and the object-oriented modeling techniques that are usually used in programming languages like C#, Java and C++. It bridges the gap between conceptual data modeling techniques for relational and object-oriented databases like entry-relationship diagram (ERD) and object-relational mapping (ORM). What Is Object Oriented Database? An object-oriented database is organized around objects rather than actions and data rather than logic. Therefore, an object database is a d...