Python Integers: Type Casting and Doubling Integers | Swapping Values | Concatenation of Integers

In this article we will revise the Python Integer concepts we have previously gone through in while discussing Integer data types, we will have a look at how to solve common Integer problems involving type casting/conversion, doubling, swapping of integer values and the concatenation of integer values in Python.

Typecasting and doubling Python Integers problem

  1. Given an input num as a string. You need to typecast into an integer and double it.

Solution: The problem statement is quite clear to us, to solve it first we need to accept a user input into a variable ‘num’. The variable must be forced into becoming integer data type. We can utilize the input() method for accepting the user defined input, the string( ) method is used to convert values of other data types into the string data type. However, since the default data type of the input( ) method is the string data type so we need to specify conversion of string to integer. The int( ) method makes sure that the entered input value which is processed into a distinct integer value. We then need to use the * multiplication operator to multiply the variable num twice for the end result.

num=(int(input("Enter number : ")))
doubled=num*2
print("The doubled value is:", doubled)
Output:
Enter number: 4
The doubled value is: 8

In the above example program, a user input is processed through the int( ) method, which is then subsequently doubled in value through multiplication and then the value is printed out. The entered value is stored in the variable num, the value of variable num is multiplied twice as the value of variable ‘doubled’. Finally we display the value of the variable ‘doubled’ as the resultant output.

Swapping variable values in Python Integers

2. Given two numbers a and b, you need to swap their values so a holds the value of b and b holds the value of a.

This problem is a repeat of the swapping variable values without using a third variable which we have seen earlier, in this case we will use a special syntax that is exclusive to Python for swapping the variable values.

a=1
b=2
a,b = b,a
print("The value of variable a after swapping: ",a)
print("The value of variable a after swapping: ",b)
Output:
The value of variable a after swapping:  2
The value of variable a after swapping:  1

In the above program swapping of values is achieved without the use of a third variable. This form of simple syntax is not present in most other commonly used programming languages. The variable values are declared as 1 and 2 respectively, but later each of the variable’s value is defined as the other variable. Finally we display the swapped output variable values along with a print statement.

Concatenation of Python Integers

3. Given two integers a and b, you need to concatenate them so the output is ab.

The simplest way to concatenate integers is by utilizing the str( ) method for conversion along with int( ) to get back the value as an integer.

a = 890
b = 349
concatenation_output = int(str(a) + str(b))
print(concatenation_output)
Output:
890349

We define the values of the variables a and b as 890 and 349. Since concatenation is a common string feature we try to utilize this feature for our integer values. The values of ‘890’ and 349′ are both converted into a string and then combined together before being reconverted into an integer and stored in the variable ‘concatenationoutput’. Finally we print out the value of the concatenation result. Thus, we obtain the concatenation result of ab(809349) as output.

Next we will look at the concepts of Tuples, Dictionaries, Sets, Input and Output through various different problems and the appropriate solution programs/logic to deal with them.

Leave a Comment