Type Casting est la méthode pour convertir la variable Python Conversion de type implicite Python
Conversion de type implicite en Python
Dans cette méthode, Python convertit automatiquement le type de données en un autre type de données. Les utilisateurs n’ont pas à s’impliquer dans ce processus.
Python3
Katrina Kaif
# 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))> |
>
>
Sortir:
10.0 21.0>
Conversion de type explicite en Python
Dans cette méthode, Python nécessite l'implication de l'utilisateur pour convertir le type de données variable en type de données requis.
Exemples de conversion de type en Python
La conversion de type peut principalement être effectuée avec ces fonctions de type de données :
- Int() : Python Int() la fonction prend float ou string comme argument et renvoie un objet de type int.
- flotter(): Python flotter() La fonction prend un entier ou une chaîne comme argument et renvoie un objet de type float.
- str() : Python str() la fonction prend float ou int comme argument et renvoie un objet de type chaîne.
Python Convertir Int en Float
Nous voilà Conversion d'int en float en Python avec le flotter() fonction.
Python3
# Python program to demonstrate> # type Casting> # int variable> a>=> 5> # typecast to float> n>=> float>(a)> print>(n)> print>(>type>(n))> |
>
tuple de tri python
>
Sortir:
5.0>
Python Convertir Float en Int
Nous voilà Conversion Flotteur vers le type de données int en Python avec int() fonction.
Python3
# Python program to demonstrate> # type Casting> # int variable> a>=> 5.9> # typecast to int> n>=> int>(a)> print>(n)> print>(>type>(n))> |
>
>
Sortir:
5>
Python Convertir un entier en chaîne
Nous voilà Conversion int au type de données String en Python avec str() fonction.
Python3
liste de tableaux triée en Java
# Python program to demonstrate> # type Casting> # int variable> a>=> 5> # typecast to str> n>=> str>(a)> print>(n)> print>(>type>(n))> |
>
>
Sortir:
5>
Python Convertir une chaîne en flottant
Ici, nous transformons le type de données chaîne en type de données float avec flotter() fonction.
Python3
# Python program to demonstrate> # type Casting> # string variable> a>=> '5.9'> # typecast to float> n>=> float>(a)> print>(n)> print>(>type>(n))> |
>
>
Sortir:
5.9>
Python Convertir une chaîne en entier
Nous voilà Conversion chaîne en type de données int en Python avec int() fonction. Si la chaîne donnée n’est pas un nombre, une erreur sera générée.
Python3
xd sens
# string variable> a>=> '5'> b>=> 't'> # typecast to int> n>=> int>(a)> print>(n)> print>(>type>(n))> print>(>int>(b))> print>(>type>(b))> |
>
>
Sortir:
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 : littéral invalide pour int() avec base 10 : 't'>
Ajout de chaîne et d'entier à l'aide de la conversion explicite
Python3
carte java de l'itérateur
# integer variable> a>=> 5> # string variable> b>=> 't'> # typecast to int> n>=> a>+>b> print>(n)> print>(>type>(n))> |
>
>
Sortir:
TypeError Traceback (most recent call last) Cell In[5], line 10 7 b = 't' 9 # typecast to int --->10 n = a+b 12 impression(n) 13 impression(type(n))>