Tastați Casting în Python (implicit și explicit) cu exemple
Type Casting este metoda de conversie a variabilei Python Conversie implicită de tip Python
Conversie implicită de tip în Python
In aceasta metoda, Piton convertește automat tipul de date într-un alt tip de date. Utilizatorii nu trebuie să se implice în acest 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))> |
Ieșire:
10.0 21.0
Conversie explicită de tip în Python
În această metodă, Python are nevoie de implicarea utilizatorului pentru a converti tipul de date variabile în tipul de date necesar.
Exemple de tip casting în Python
În principal, turnarea tipului se poate face cu aceste funcții de tip de date:
- Int(): Python Int() funcția ia float sau șir ca argument și returnează obiectul de tip int.
- pluti(): Piton pluti() funcția ia int sau șir ca argument și returnează obiectul de tip float.
- str(): Piton str() funcția ia float sau int ca argument și returnează obiectul de tip șir.
Python Convert Int în Float
Iată-ne Conversia Int în Float în Python cu pluti() funcţie.
Python3
# Python program to demonstrate> # type Casting> # int variable> a> => 5> # typecast to float> n> => float> (a)> print> (n)> print> (> type> (n))> |
Ieșire:
5.0
Python Convertește Float în Int
Iată-ne Conversia Float la tipul de date int în Python cu int() funcţie.
Python3
# Python program to demonstrate> # type Casting> # int variable> a> => 5.9> # typecast to int> n> => int> (a)> print> (n)> print> (> type> (n))> |
Ieșire:
5
Python Convertiți int în șir
Iată-ne Conversia int la tipul de date String în Python cu str() funcţie.
Python3
# Python program to demonstrate> # type Casting> # int variable> a> => 5> # typecast to str> n> => str> (a)> print> (n)> print> (> type> (n))> |
Ieșire:
5
Python Convert String în float
Aici, turnăm tipul de date șir în tip de date float cu pluti() funcţie.
Python3
# Python program to demonstrate> # type Casting> # string variable> a> => '5.9'> # typecast to float> n> => float> (a)> print> (n)> print> (> type> (n))> |
Ieșire:
5.9
Python Convertiți șirul în int
Iată-ne Conversia string la tipul de date int în Python cu int() funcţie. Dacă șirul dat nu este un număr, atunci va arunca o eroare.
Python3
# string variable> a> => '5'> b> => 't'> # typecast to int> n> => int> (a)> print> (n)> print> (> type> (n))> print> (> int> (b))> print> (> type> (b))> |
Ieșire:
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 invalid pentru int() cu baza 10: 't'
Adăugarea șirului și a întregului folosind conversia explicită
Python3
# integer variable> a> => 5> # string variable> b> => 't'> # typecast to int> n> => a> +> b> print> (n)> print> (> type> (n))> |
Ieșire: