Python question

What is type conversion in Python?

Type conversion refers to the conversion of one data type into another.

int() – converts any data type into integer type

float() – converts any data type into float type

ord() – converts characters into integer

hex( ) – converts integers to hexadecimal

oct() – converts integer to octal

tuple() – This function is used to convert to a tuple.

set() – This function returns the type after converting to set.

list() – This function is used to convert any data type to a list type.

dict() – This function is used to convert a tuple of order (key,value) into a dictionary.

str() – Used to convert integer into a string.

complex(real,imag) – This functionconverts real numbers to complex(real,imag) number.00

2 Likes

Yeah! as @pratikkr0444 said type conversation refers to conversation of one data type in to another, below you can find the example code for the conversation

Python code to demonstrate Type conversion

using int(), float()

initializing string

s = β€œ10010”

printing string converting to int base 2

c = int(s,2)
print (β€œAfter converting to integer base 2 : β€œ, end=””)
print Β©

printing string converting to float

e = float(s)
print (β€œAfter converting to float : β€œ, end=””)

Thank you,
Happy learning

Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming. This article is aimed at providing the information about certain conversion functions.

1. int(a,base) : This function converts any data type to integer . β€˜Base’ specifies the base in which string is if data type is string.

2. float() : This function is used to convert any data type to a floating point number
.

Type conversion is used to convert one type of data into another.There are two types of type conversion.One is type casting(explicit conversion) and the other is type coercion(implicit conversion).

eg for type casting- a=int(b) such that b is a number in string format.
eg for type coercion- a=10.4,b=2
quotient=a/b
Here data type of b is indirectly converted from integer to float.