×

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

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

AES CTR Python

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 print in the same line in Python?

How to print in the same line in python By default, the print function in Python takes us to the next line and prints the desired statement in the output. In this...

5 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

Screen Rotation app Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you...

4 minutes read.

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Pascal Triangle

Python Pascal Triangle Pascal triangle A pascal triangle is a number pattern of triangular array of the binomial coefficients. For designing a pascal triangle, we write a function in the program which...

5 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

Syntax of Map function in Python

In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We...

3 minutes read.