Skriv Casting i Python (implicit og eksplicit) med eksempler

Type Casting er metoden til at konvertere Python-variablen Python implicit typekonvertering

  • Python eksplicit type konvertering
  • Implicit typekonvertering i Python

    I denne metode, Python konverterer datatypen til en anden datatype automatisk. Brugere behøver ikke at involvere sig i denne proces.

    Python3




    # Python program to demonstrate> # implicit type Casting> # Python automatically converts> # a to int> a> => 7> print> (> type> (a))> # Python automatically converts> # b to float> b> => 3.0> print> (> type> (b))> # Python automatically converts> # c to float as it is a float addition> c> => a> +> b> print> (c)> print> (> type> (c))> # Python automatically converts> # d to float as it is a float multiplication> d> => a> *> b> print> (d)> print> (> type> (d))>

    Produktion:

      10.0  21.0 

    Eksplicit typekonvertering i Python

    I denne metode har Python brug for brugerinddragelse for at konvertere den variable datatype til den nødvendige datatype.

    Eksempler på Typecasting i Python

    Hovedsageligt typestøbning kan udføres med disse datatypefunktioner:

    • Int(): Python Int() funktion tage float eller streng som et argument og returnerer objekt af typen int.
    • flyde(): Python flyde() funktion take int eller streng som et argument og returnere float type objekt.
    • str(): Python str() funktion tager float eller int som et argument og returnerer objekt af strengtype.

    Python Konverter Int til Float

    Her er vi Konvertering af Int til Float i Python med flyde() fungere.

    Python3




    # Python program to demonstrate> # type Casting> # int variable> a> => 5> # typecast to float> n> => float> (a)> print> (n)> print> (> type> (n))>

    Produktion:

    5.0 

    Python Konverter Float til Int

    Her er vi Konvertering Flyd til int datatype i Python med int() fungere.

    Python3




    # Python program to demonstrate> # type Casting> # int variable> a> => 5.9> # typecast to int> n> => int> (a)> print> (n)> print> (> type> (n))>

    Produktion:

    5 

    Python Konverter int til streng

    Her er vi Konvertering int til String datatype i Python med str() fungere.

    Python3




    # Python program to demonstrate> # type Casting> # int variable> a> => 5> # typecast to str> n> => str> (a)> print> (n)> print> (> type> (n))>

    Produktion:

    5 

    Python Konverter streng til flydende

    Her støber vi strengdatatype ind i flydende datatype med flyde() fungere.

    Python3




    # Python program to demonstrate> # type Casting> # string variable> a> => '5.9'> # typecast to float> n> => float> (a)> print> (n)> print> (> type> (n))>

    Produktion:

    5.9 

    Python Konverter streng til int

    Her er vi Konvertering streng til int datatype i Python med int() fungere. Hvis den givne streng ikke er nummer, vil den give en fejl.

    Python3




    # string variable> a> => '5'> b> => 't'> # typecast to int> n> => int> (a)> print> (n)> print> (> type> (n))> print> (> int> (b))> print> (> type> (b))>

    Produktion:

    5  --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[3], line 14  11 print(n)  12 print(type(n)) --->14 print(int(b)) 15 print(type(b)) ValueError: ugyldig literal for int() med base 10: 't' 

    Tilføjelse af streng og heltal ved hjælp af eksplicit konvertering

    Python3




    # integer variable> a> => 5> # string variable> b> => 't'> # typecast to int> n> => a> +> b> print> (n)> print> (> type> (n))>

    Produktion:

    TypeError Traceback (most recent call last) Cell In[5], line 10  7 b = 't'  9 # typecast to int --->10 n = a+b 12 print(n) 13 print(type(n))