Wednesday, May 19, 2021
More

    Python Type Casting

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

    In this article, we discuss Python Type Casting. Typecasting is used to ensure variables are correctly processed by a function. In other words, we can say that typecasting is a method of changing an entity from one data type to another. An example of typecasting is converting a floating point number to an integer. For example, there is no need for decimal precision in the program. Here typecasting method is used to convert float numbers into an integer. 

    In the Python programming language, there are mainly two types of type conversion.

    • Implicit Type Conversion
    • Explicit Type Conversion

    Implicit Type Conversion is executed by the Python interpreter automatically. Python Type Casting is also known as Explicit Type Conversion. Here we also discuss inbuilt type casting functions.

    Implicit Type Conversion

    Python automatically changes one data type to another data type by the python interpreter.

    a = 100
    b = 10.10
    
    sum = a + b
    print("sum = ",sum)
    
    
    print("datatype of variable a is ",type(a))
    print("datatype of variable b is ",type(b))
    print("datatype of sum is",type(sum))

    As seen in the above program we take two variable and add them into a new variable sum and find the data type for all the variables. In the Python programming language, it converts the smaller data types to larger data types. The output of the above program is as shown below.

    Output

    sum =  110.1
    datatype of variable a is  class 'int'
    datatype of variable b is  class 'float'
    datatype of sum is class 'float'

    Type Casting or Explicit Type Conversion

    Explicit Type Conversion is used to change the data type of object. As per our requierement, we can convert the data type of object using predefined functions like int(), float(), str().

    Without Type Casting

    a = 100
    b = "20"
    sum = a + b
    print("sum = ",sum)
    
    
    print("datatype of variable a is ",type(a))
    print("datatype of variable b is ",type(b))
    print("datatype of sum is",type(sum))
    Output
    sum = a + b
    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    With Type Casting

    a = 100
    b = "20"
    print("datatype of variable b is ",type(b))
    b = int(b)
    sum = a + b
    print("sum = ",sum)
    print("datatype of variable a is ",type(a))
    print("datatype of variable b is ",type(b))
    print("datatype of sum is",type(sum))
    Output
    datatype of variable b is class 'str'
    sum =  120
    datatype of variable a is class 'int'
    datatype of variable b is class 'int'
    datatype of sum is class 'int'

    As seen in the above example we can use the type casting to convert one data type into another. In the first example, we try to add integer and string variable. when we run the code it throws an error for unsupported operand type. While in the second example we try to solve this error using the typecasting. Here we convert the string into the integer type. In explicit data type conversion, there may be a risk of information loss when you’re forcing an expression to be of a specific type.

     

    There are many inbuilt type casting functions available in Python language which performs data type conversion from one type to another.

    Typecast Function Description Example Output
    int(a,base) converts any data type to integer int(“10101”,2) 21
    float() convert any data type to a floating point number float(111001) 111001.0
    str() convert integer into a string str(10) ’10’
    complex(real,imag) convert real numbers to complex number complex(10,20) (10+20j)
    ord() convert a character to integer ord(‘a’) 97
    hex() convert integer to hexadecimal string hex(20) ‘0x14’
    oct() convert integer to octal string oct(10) ‘0o12’
    bin() convert decimal to binary bin(80) ‘0b1010000’
    list() convert any data type to a list type list(“python”) [‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]
    tuple() convert to a tuple tuple(“python”) (‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’)
    set() convert into set set(“python”) {‘t’, ‘n’, ‘h’, ‘o’, ‘p’, ‘y’}
    dict() convert a tuple of order (key,value) into a dictionary dict(((‘x’ ,1),(‘y’, 2), (‘z’, 3))) {‘x’: 1, ‘y’: 2, ‘z’: 3}

    As we have discussed how we can easily type cast from float to int and int to char, string to list, tuple or set and tuple to dictionary with key and values, integet to binary, octal, hexadecimal and so on. Hope you cleared your learning concepts for casting in python programming language.

    See More: Type Casting In C Language

    Previous articleHe’s a dead man
    Next articleSQL in real Life

    Latest Articles

    More Recipes Like This