Python define funciones de conversión de tipo para convertir directamente un tipo de datos a otro, lo que es útil en la programación diaria y competitiva. Este artículo tiene como objetivo proporcionar información sobre ciertas funciones de conversión.
Hay dos tipos de conversión de tipos en Python:
Python3
x = 10 print("x is of type:",type(x)) y = 10.6 print("y is of type:",type(y)) z = x + y print(z) print("z is of type:",type(z))
Python3
# Python code to demonstrate Type conversion # using int(), float() # initializing string s = "10010" # printing string converting to int base 2 c = int(s,2) print ("After converting to integer base 2 : ", end="") print (c) # printing string converting to float e = float(s) print ("After converting to float : ", end="") print (e)
Python3
# Python code to demonstrate Type conversion # using ord(), hex(), oct() # initializing integer s = '4' # printing character converting to integer c = ord(s) print ("After converting character to integer : ",end="") print (c) # printing integer converting to hexadecimal string c = hex(56) print ("After converting 56 to hexadecimal string : ",end="") print (c) # printing integer converting to octal string c = oct(56) print ("After converting 56 to octal string : ",end="") print (c)
Python3
# Python code to demonstrate Type conversion # using tuple(), set(), list() # initializing string s = 'geeks' # printing string converting to tuple c = tuple(s) print ("After converting string to tuple : ",end="") print (c) # printing string converting to set c = set(s) print ("After converting string to set : ",end="") print (c) # printing string converting to list c = list(s) print ("After converting string to list : ",end="") print (c)
Python3
# Python code to demonstrate Type conversion # using dict(), complex(), str() # initializing integers a = 1 b = 2 # initializing tuple tup = (('a', 1) ,('f', 2), ('g', 3)) # printing integer converting to complex number c = complex(1,2) print ("After converting integer to complex number : ",end="") print (c) # printing integer converting to string c = str(a) print ("After converting integer to string : ",end="") print (c) # printing tuple converting to expression dictionary c = dict(tup) print ("After converting tuple to dictionary : ",end="") print (c)
Python3
# Convert ASCII value to characters a = chr(76) b = chr(77) print(a) print(b)
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA