×

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data types in Python Language: Numerical, Set, String, List, and Tuple. Every input we give is stored as a data type.

The variable type plays a vital role while converting one variable into another data type. The data type of a variable differs from code to code based on the purpose of the code. The interpreter of Python doesn't make any changes to the variable data types in advance. It is possible to change a variable's data type in Python. There are mainly two types of conversion, which are:

Implicit type conversion:  This type of conversion will convert one data type to another without involving the user's commands.

Let us look at an example code to understand the implicit type conversion.

Code:

#Implicit type conversion
# Python itself converts an into an integer
a = 10
print(type(a))
 # Python itself converts b into float
b = 5.0
print(type(b))
 # Python itself converts c into a float as its a floor division
c = a//b
print(c)
print(type(c))

Output:

<class 'int'>
<class 'float'>
2.0
<class 'float'>

Through the above code, we understood that Python declares the data type of a variable by itself.

Explicit type conversion: In this type of conversion, the user must convert the variable's data type. We can convert the data type by using functions like int(), str(), float(), etc. to understand this conversion better, let us look at an example code.

In this code, let us convert integer to string.

Code:

a= 50
# we used the str() function to convert int to string
b= str(a)
print(type(b))
print(b)

Output:

<class 'str'>
50

We have converted integer to string using the str() function in this code. And in this conversion, the user must be involved in adding the function.

String to number conversion:

We can convert a string into a number by using int() or float() functions. We have to give an appropriate string containing numerical values for proper conversions. Let us now look at an example code for the string to number conversion

Code:

# type conversion of string to number
a = "2002"
 # Converting using int()
b = int(a)
print(b)
print(type(b))
 
# Converting using float()
c = float(a)
print(c)
print(type(c))

Output:

2002
<class 'int'>
2002.0
<class 'float'>

Number conversions:

There are two data types in numbers in Python, which are integers and floats. When required, we change in between these data types. Let us look at these conversions one by one, including some example codes.

Float to integer conversion:

A float can be converted into an integer using the function int(). Let us get this concept by taking an example code using this int() function.

Code:

#float to integer conversion with int() function
a= 23.45
b= int(a)
print(b)
print(type(b))

Output:

23
<class 'int'>

Integer to float conversion:

An integer can be converted into float using the function float(). Let us get this concept by taking an example code using this float() function.

Code:

# integer to float conversion with float() function
a= 234
b= float(a)
print(b)
print(type(b))

Output:

234.0
<class 'float'>

Tuple to list conversions:

We can convert a tuple to a list by using the function list(). Let us understand this concept by using the list() function as an example code.

Code:

# converting tuple to list using the list() function
a=(3,5,7,9,10)
b= list(a)
print(b)
print(type(b))

Output:

[3, 5, 7, 9, 10]
<class 'list'>

List to tuple conversions:

We can convert a list to a tuple by using the function tuple(). Let us get this concept by taking an example code using the tuple() function.

Code:

# converting the list to a tuple using the tuple() function
a=[2,4,6,8,10]
b= tuple(a)
print(b)
print(type(b))

Output:

(2, 4, 6, 8, 10)
<class 'tuple'>
>

Conclusion:

In this article, we learned how to convert data types in python language. We first learned about the data types in Python. And we also discussed the implicit and explicit type conversions. We saw all the codes of integer to float, float to integer, list to a tuple, tuple to list, string to an integer, and integer to string conversions.


Related Topics

End Parameter in python

print(): The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value. Syntax: print(“hi”) Output: hi will be displayed on the output...

3 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Copying a file from one folder to Another with Python

In this article, we shall discuss copying a file from one folder to another using python functions. A file is a collection of data or information stored on a computer....

3 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

Python Coroutine

Introduction In Python, Coroutines are defined as the special type of function that freely allows control to its caller without losing the state. Coroutines and generates are similar but coroutines consist...

3 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

Python Skyline

Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Exponentiation in Python

What is an Exponent in Python? Exponent is a fundamental mathematical concept that is used in many different areas, such as engineering, physics, and finance. In mathematics, an exponent is a...

4 minutes read.