Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Explicit Type Casting ในภาษา Python


เราทุกคนสามารถประกาศและทำงานกับชนิดข้อมูลได้ เคยสงสัยไหมเกี่ยวกับการแปลงชนิดข้อมูลระหว่างกัน? ในบทความนี้ เราจะเรียนรู้วิธีที่เราสามารถแปลงชนิดข้อมูลเหล่านี้โดยใช้ built-in functions ใน Python ซึ่งเรียกว่า Type Casting Type Casting แบ่งได้ 2 ประเภท: Implicit และ Explicit ในโมดูลนี้ เราจะพูดถึง Explicit type casting เท่านั้น

ตอนนี้มาดูการแปลงชนิดข้อมูลพื้นฐานบางอย่าง

Integer Type Conversion ใน Python

ฟังก์ชัน int() อนุญาตให้เราแปลงชนิดข้อมูลใดๆ เป็น integer รับพารามิเตอร์ 2 ตัวคือ Base และ Number โดย Base บ่งชี้ฐานของค่า integer ที่เป็นของ (binary[2], octal[8], hexadecimal[16])

Float Type Conversion ใน Python

ฟังก์ชัน float() อนุญาตให้เราแปลงชนิดข้อมูลใดๆ เป็น floating point number รับพารามิเตอร์ 1 ตัวคือค่าของชนิดข้อมูลที่ต้องการแปลง

ตัวอย่าง

# Type casting
value = "0010110"
# int base 2
p = int(value, 2)
print("integer of base 2 format : ", p)
# default base
d = int(value)
print("integer of default base format : ", d)
# float
e = float(value)
print("corresponding float : ", e)

ผลลัพธ์

integer of base 2 format : 22
integer of default base format : 10110
corresponding float : 10110.0

ในโค้ดข้างต้น เราได้แปลง integer พร้อม base ใน Python ด้วย

Tuple Type Conversion ใน Python

ฟังก์ชัน tuple() อนุญาตให้เราแปลงเป็น tuple รับพารามิเตอร์ 1 ตัวคือ string หรือ list

List Type Conversion ใน Python

ฟังก์ชัน list() อนุญาตให้เราแปลงชนิดข้อมูลใดๆ เป็น list type รับพารามิเตอร์ 1 ตัว

Dictionary Type Conversion ใน Python

ฟังก์ชัน dict() ใช้แปลงทูเพิลลำดับ (key, value) เป็น dictionary Key ต้องเป็น unique มิฉะนั้นค่าซ้ำจะถูกทับ

ตัวอย่าง

# Type casting
str_inp = 'Tutorialspoint'
# conversion to list
j = list(str_inp)
print("string to list : ")
print(j)
# conversion to tuple
i = tuple(str_inp)
print("string to tuple : ")
print(i)
# nested tuple
tup_inp = (('Tutorials', 0), ('Point', 1))
# conversion to dictionary
c = dict(tup_inp)
print("list to dictionary : ", c)
# nested list
list_inp = [['Tutorials', 0], ['Point', 1]]
# conversion to dictionary
d = dict(list_inp)
print("list to dictionary : ", d)

ผลลัพธ์

string to list : ['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't']
string to tuple : ('T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't')
list to dictionary : {'Tutorials': 0, 'Point': 1}
list to dictionary : {'Tutorials': 0, 'Point': 1}

String Type Conversion ใน Python

ฟังก์ชัน str() ใช้แปลง integer หรือ float เป็น string รับอาร์กิวเมนต์ 1 ตัว

Ascii chr() และ ord() Type Conversion ใน Python

chr() - ฟังก์ชันนี้ใช้แปลง integer type เป็น character type

ord() - ฟังก์ชันนี้ใช้แปลง character type เป็น integer type

ตัวอย่าง

# Type casting
char_inp = 'T'
# converting character to corresponding integer value
print("corresponding ASCII VALUE: ", ord(char_inp))
int_inp = 92
# converting integer to corresponding Ascii Equivalent
print("corresponding ASCII EQUIVALENT: ", chr(int_inp))
# integer and float value
inp_i = 231
inp_f = 78.9
# string equivalent
print("String equivalent", str(inp_i))
print("String equivalent", str(inp_f))

ผลลัพธ์

corresponding ASCII VALUE: 84
corresponding ASCII EQUIVALENT: \
String equivalent 231
String equivalent 78.9

สรุป

ในบทความนี้ เราได้เรียนรู้เกี่ยวกับ explicit type casting ใน Python 3.x หรือเวอร์ชันก่อนหน้า