Swapping variables in Python | Object Identity in Python | Variable Casting in Python

In this article we will elaborate on the operations which can be performed on the Python variables and data types such as swapping variables, object identity and id() function, and casting.

Swapping variables in Python

Swapping variables in programming languages refers to the mutual exchange of variable values during program execution. Variable swapping can be achieved either by using a third variable to temporarily store the value or without using a third variable. Let us look at the logic of swapping variables in both methods through the help of an algorithm.

There is yet another python language specific way exist to swap variable that has super simple syntax.

Swapping variable values using extra variable

In this method to swap variable values we use an extra variable to temporarily hold the values for swapping to be done.

// Algorithm of swapping variable values using extra variable

STEP 1:Declare variables a, b and c
STEP 2:Define the values of a and b variables
STEP 3:c=a
STEP 4:a=b
STEP 5:b=c
STEP 6:PRINT a and b

Implementation of swapping variable values using extra variable according to Python standards and syntax:

# Program to swap the values of two variables using  third variable in python
x = 15
y = 20

# Create a temporary variable and swap the values
temp = x
x = y
y = temp

print('The value of x after swapping :', x)
print('The value of y after swapping :', y)
# Output:
The value of x after swapping : 20
The value of y after swapping : 15

Swapping variable values without using extra variable

In this we will see how to swap variable values without using any extra variable. We will use some arithmetic operation to do this.

// Algorithm of swapping variable values without using third variable

STEP 1: Declare variables x and y
STEP 2: Define the values of x and y variables
STEP 3: x = x + y
STEP 4: y= x - y
STEP 5: x = x - y
STEP 6: PRINT x, y

Python program example of swapping variable values without using any extra variable:

# Program to swap the values of two variables without using third variable
x = 15
y = 20

# Swapping without third variable logic
x = x + y
y = x - y
x = x - y

print('The value of x after swapping :', x)
print('The value of y after swapping :', y)
# Output:
The value of x after swapping : 20
The value of y after swapping : 15

Explanation:

  • First we did x = x + y, which essentially added both values and allocated to x. Now x contains sum of original value of x and y
  • Then we did y = x – y, we subtracted y from x(total). This subtraction results the original value of x, which we given to y. Now y contains the original value of x.
  • Lastly we did x = x – y, which is essentially x(total) – y(original value of x) that will result original value of y which we allocated to x.

Python exclusive way to swap variables

This way is exclusive to python programming language.

// Python language exclusive swapping

STEP 1: Declare variables x and y
STEP 2: Define the values of x and y variables
STEP 3: Just do x, y = y, x

Below is the simple implementation of the python programming language exclusive swapping method

# Program to swap the values of two variables without using third variable
x = 15
y = 20

# Swapping
x, y  = y, x

print('The value of x after swapping :', x)
print('The value of y after swapping :', y)
# Output
The value of x after swapping : 20
The value of y after swapping : 15

Object Identity in Python

Objects are a representation of data, data in Python is always represented as objects or as the relation between the various different objects. Each object has a distinct identity, relationship or values. In the programming languages, particularly in python the object identity represents the constant location or address where the object is stored in the computer memory while the program is run.

To simplify our understanding we can compare the concepts of objects/object identity to hotel rooms. Imagine a hotel room(In place of the memory) with many residents(various objects). All the rooms were initially unoccupied (), all rooms are free. So when a new resident pays for a room (A new object that is defined), you assign an empty room to that particular resident. Each room has a distinct room number (Object identity) and the id() function tells us in which room the guest (object) is staying.

The id() function returns a unique id for the specified object, however each time the program is terminated and run again, the object identity and hence the unique memory address will change. Let us look at examples of utilizing the id() function to find out the object identities of variables.

# Program to show the usage of the id() function.

x = ('Red', 'Green', 'Blue')
z= 89
w= id(z)
y = id(x)
print(y)
print(w)
Output:
140282741681600
140282881016288

Variable Casting in Python

Since Python is a dynamically typed language which automatically assumes the data types of variables by judging the values assigned to it, we usually do not need to mention the data types. However in specific circumstances, certain needs may require us to directly specify the kind of data type which we need to use. It is possible to specify the data types of variables using constructor functions:

  • int() – It constructs the integer number from an integer literal, a float literal , or a string literal(If the string value is a number)
  • float() – It constructs the float number from an integer literal, a float literal or a string literal (if the string value is a float/decimal number)
  • str() – constructs a string from a wide variety of data types, including strings, integer literals and float literals
x = int(1.09000)
y = float(2.8)
z = int("3")
print(x)
print(y)
print(z)
Output:
1
2.8
3

In the next article we will examine lists and tuple operations.

Leave a Reply

Your email address will not be published. Required fields are marked *