English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Typumwandlung in Python

Mit Python können wir Daten leicht in verschiedene Typen umwandeln. Typkonvertierungen haben verschiedene Funktionen. Wir können String-Objekte in numerische Typen konvertieren, Konvertierungen zwischen verschiedenen Container-Typen durchführen und so weiter.

In diesem Abschnitt sehen wir, wie wir mit Python Konvertierungen durchführen.

umwandeln.

den String in einen numerischen Typ konvertieren.int(),float()und anderen Methoden. Um einen String-Typobjekt in ein Numerisches Objekt zu konvertieren, können wirint()Methode, können wir jede Zahl in eine Zeichenkette umwandeln (mit10Basis). Es verwendet einen String-Typparameter und eine Standardbasis10durch eine spezifizierte Basis konvertiert werden, um aus einer Zeichenkette in dieser Basis eine Dezimalzahl zu erhalten.

Ähnlich kann mit derfloat()Methode, eine Zeichenkette, die Werte in Dezimalform enthält, in float konvertieren zu können.

Example code

str_number = '56'
print(int(str_number))  # Standardbasis 10
print(int(str_number, 16))  # Aus Hexadezimal
print(int(str_number, 12))  # Aus einer Zahl, deren Basis 12
str_number = '25.897'
print(float(str_number))  # String in Fließkommawert konvertieren

Output result

56
86
66
25.897

Zeichen zu Zeichenkonvertierung und Basisumwandlung

Es ist bekannt, dass ein String eine Sammlung von Zeichen ist. In Python können wir jedoch den ASCII-Wert eines Zeichens nicht direkt erhalten. Wir müssen dieord()Methoden, um ein Zeichen in seinen ASCII-Wert zu konvertieren.

Es gibt auch andere Methoden, wiehex(),ord(),bin()Umwandlung von Dezimalzahlen in Hexadezimal-, Oktal- und Binärzahlen mit Nummerierung.

Example code

print('ASCII-Wert von "G" ist: ') + str(ord('G')))
print('Hexadezimalwert von 254 is: ' + str(hex(254))
print('Oktalwert von 62 is: ' + str(oct(62))
print('Binary value of') 56 is: ' + str(bin(56))

Output result

ASCII value of 'G' is: 71
Hexadecimal value of 254 is: 0xfe
Octal value of 62 is: 0o76
Binary value of 56 is: 0b111000

Container Conversion

In Python, there are different container type objects like lists, tuples, sets, etc. We can change one type of container to another by changing one type of container to anotherlist(),tuple(),set()etc.

Example code

my_list = [10, 20, 30, 40, 50]
10, 10, 20, 30, 20, 50, 20}
print('From list to tuple: ' + 
print('From list to set: ' + 
print('From set to list: ' + str(list(my_set)))

Output result

From list to tuple: (10, 20, 30, 40, 50)
From list to set: {40, 10, 50, 20, 30}
From set to list: [10, 20, 50, 30]

Complex Number

In Python, there is a complex class. Therefore, using this method, we can convert two integers (real and imaginary parts) into a complex number.

Example code

my_complex = complex(10, 5)  # convert to complex number
print(my_complex)

Output result

(10+5j)

Tuple to Dictionary

Tuples are one of the most important container types in Python. Using tuples, we can store some ordered data. In Python, we can convert a Tuple type object with two values to a dictionary object. Thedict()The method can be converted.

Example code

my_tuples = (('Tiger', 4), ('Cat', 6), ('Dog', 8), ('Elephant', 10))
my_dict = dict(my_tuples)
print(my_dict)

Output result

{'Tiger': 4, 'Elephant': 10, 'Dog': 8, 'Cat': 6{}