Devdit
 

type ใน Python คืออะไร ใช้งานอย่างไร

5.9K

บทความนี้ขอกล่าวถึงคำสั่ง type ในภาษา Python โดยคำสั่ง type มีไว้สำหรับตรวจสอบว่าตัวแปรดังกล่าวเป็นตัวแปรชนิดอะไร เช่น string, int, boolean หรือ float เป็นต้น สามารถเขียนโปรแกรมได้ดังนี้

 

ตัวอย่าง type ใน Python คืออะไร ใช้งานอย่างไร

i = 9
print( type(i), i )

s = 'Hello Python'
print( type(s), s )

b = True
print( type(b), b )

f = 99.58
print( type(f), f )

b = b'\x00\x1e'
print( type(b), b )

ผลลัพธ์

<class 'int'> 9
<class 'str'> Hello Python
<class 'bool'> True
<class 'float'> 99.58
<class 'bytes'> b'\x00\x1e'
เขียน 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ