Escriviu Casting en Python (implícit i explícit) amb exemples

Type Casting és el mètode per convertir la variable Python Conversió de tipus implícita de Python

  • Conversió de tipus explícit de Python
  • Conversió de tipus implícita en Python

    En aquest mètode, Python converteix automàticament el tipus de dades en un altre tipus de dades. Els usuaris no han d'implicar-se en aquest procés.

    Python 3




    # 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))>

    Sortida:

      10.0  21.0 

    Conversió de tipus explícita a Python

    En aquest mètode, Python necessita la participació de l'usuari per convertir el tipus de dades variable en el tipus de dades requerit.

    Exemples de càsting de tipus a Python

    Principalment, el càsting de tipus es pot fer amb aquestes funcions de tipus de dades:

    • Int(): Python Int() La funció pren float o string com a argument i retorna l'objecte de tipus int.
    • float(): Python flota () La funció pren int o string com a argument i retorna un objecte de tipus flotant.
    • str(): Python str() La funció pren float o int com a argument i retorna un objecte de tipus cadena.

    Python Converteix Int a Float

    Aquí estem Convertint Int a Float en Python amb el flota () funció.

    Python 3




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

    Sortida:

    5.0 

    Python Converteix Float a Int

    Aquí estem Convertint Float al tipus de dades int a Python amb int() funció.

    Python 3




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

    Sortida:

    5 

    Python Converteix int a String

    Aquí estem Convertint int al tipus de dades String a Python amb str() funció.

    Python 3




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

    Sortida:

    5 

    Python Converteix String en flotant

    Aquí, estem emetent el tipus de dades de cadena al tipus de dades flotant amb flota () funció.

    Python 3




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

    Sortida:

    5.9 

    Python Converteix cadena a int

    Aquí estem Convertint string al tipus de dades int a Python amb int() funció. Si la cadena donada no és un número, generarà un error.

    Python 3




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

    Sortida:

    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: literal no vàlid per a int() amb base 10: 't' 

    Addició de cadena i nombre enter mitjançant la conversió explícita

    Python 3




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

    Sortida:

    10 n = a+b 12 imprimir(n) 13 imprimir(tipus(n))>>>