×

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

How to Download Python

How to Download Python Python is the most renowned programming language and developers across the globe are working on projects that are made using Python and its libraries. Here we will...

4 minutes read.

Python String lstrip() method

Python String lstrip() method The string. lstrip () method in Python returns a copy of the string with leading characters removed (based on the string argument passed). Syntax string.lstrip([chars]) Parameter chars(optional): This parameter represents a...

1 minute read.

Python md5_file() function

Python md5_file() function The md5_file() function in PHP calculates the md5 hash of a given file. Syntax md5_file ( string $filename [, bool $raw_output ] )  Parameter filename(required)- This parameter signifies the file to be calculated. raw_output(optional)- It takes a boolean value that specifies hex or binary...

1 minute read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Python AIOHTTP

Python 3.5 introduced some new syntax that makes it simpler for developers to make asynchronous programmes and packages. Aiohttp, an HTTP client/server for asyncio, is one such package. In essence,...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Python delattr() function

Python delattr() function The delattr() function in Python is used to delete the named attribute from the object, with the prior permission of the object. Syntax delattr(object, name) Parameter object : This parameter represents the object from which...

1 minute read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

How to Install PIP In Python

How to Install PIP In Python The libraries for Python have made our work easier than we expected. From a simple addition of two numbers to applying algorithms on the big...

4 minutes read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

Python List extend() method

Python List extend() method The list.extend() method extends the list by appending all the items from the iterable. Syntax list.extend(iterable) Parameter iterable: It is a required parameter which represents any iterable unlike list, set, tuple, etc. Example 1 # Python...

1 minute read.