Įveskite Casting Python (numanomas ir aiškus) su pavyzdžiais
Tipo perdavimas yra būdas konvertuoti Python kintamąjį Python implicit tipo konvertavimas
Netiesioginis tipo konvertavimas Python
Taikant šį metodą, Python automatiškai konvertuoja duomenų tipą į kitą duomenų tipą. Vartotojai neprivalo dalyvauti šiame procese.
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))> |
Išvestis:
10.0 21.0
Aiškus tipo konvertavimas Python
Taikant šį metodą, „Python“ turi įtraukti vartotoją, kad konvertuotų kintamąjį duomenų tipą į reikiamą duomenų tipą.
Tipo perdavimo pavyzdžiai „Python“.
Iš esmės tipo liejimą galima atlikti naudojant šias duomenų tipo funkcijas:
- Int(): Python Int() funkcija take float arba string kaip argumentą ir grąžina int tipo objektą.
- plūdė(): Python plūdė() funkcija take int arba string kaip argumentą ir grąžina float tipo objektą.
- str(): Python str() funkcija paima float arba int kaip argumentą ir grąžina eilutės tipo objektą.
Python Convert Int į Float
Štai ir mes „Int“ konvertavimas į „Float“ Python su plūdė() funkcija.
Python3
# Python program to demonstrate> # type Casting> # int variable> a> => 5> # typecast to float> n> => float> (a)> print> (n)> print> (> type> (n))> |
Išvestis:
5.0
Python Konvertuoti Float į Int
Štai ir mes Konvertavimas Float to int duomenų tipą Python su int() funkcija.
Python3
# Python program to demonstrate> # type Casting> # int variable> a> => 5.9> # typecast to int> n> => int> (a)> print> (n)> print> (> type> (n))> |
Išvestis:
5
Python konvertuoti į eilutę
Štai ir mes Konvertavimas int į String duomenų tipą Python su str() funkcija.
Python3
# Python program to demonstrate> # type Casting> # int variable> a> => 5> # typecast to str> n> => str> (a)> print> (n)> print> (> type> (n))> |
Išvestis:
5
Python konvertuoti eilutę į plūduriuojančią
Čia mes perduodame eilutės duomenų tipą į plūduriuojančių duomenų tipą su plūdė() funkcija.
Python3
# Python program to demonstrate> # type Casting> # string variable> a> => '5.9'> # typecast to float> n> => float> (a)> print> (n)> print> (> type> (n))> |
Išvestis:
5.9
Python Konvertuoti eilutę į int
Štai ir mes Konvertavimas iš eilutės į int duomenų tipą Python su int() funkcija. Jei nurodyta eilutė nėra skaičius, ji išmes klaidą.
Python3
# string variable> a> => '5'> b> => 't'> # typecast to int> n> => int> (a)> print> (n)> print> (> type> (n))> print> (> int> (b))> print> (> type> (b))> |
Išvestis:
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)) Value Error: neteisingas int() literatas su 10 baze: 't'
Eilutės ir sveikojo skaičiaus pridėjimas naudojant aiškią konversiją
Python3
# integer variable> a> => 5> # string variable> b> => 't'> # typecast to int> n> => a> +> b> print> (n)> print> (> type> (n))> |
Išvestis:
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))