ฟังก์ชั่นสำหรับแปลงชนิดข้อมูล ใน ไพทอน Python Data Type Conversion
ไพทอน ถึงแม้จะเป็นภาษาที่ กำหนด ชนิดของข้อมูลให้เราเองตอนที่เราสร้าง ตัวแปร แล้วแต่บางครั้งเราอาจต้องการแปรชนิดของ ตัวแปร เพื่อนำไปใช้ ต่อ มาดู ฟังก์ชั่นที่ ไพทอน เตรียมไว้ให้เราใช้สำหรับ การแสดง ชนิดของข้อมูลกัน (ฟังก์ชั่น built-in คือมีให้เราเรียกใช้ได้เลย)

ชื่อฟังก์ชัน คำอธิบาย
type(x) type เป็น ฟังก์ชั่นที่ไว้ตรวจสอบ ตัวแปรว่ามีชนิดข้อมูลเป็นอะไร

int(x [,base])

แปลง x เป็น integer. โดยสามารถระบุฐานได้ เมื่อ x เป็น string

long(x [,base] )

แปลง x เป็น  long integer. โดยสามารถระบุฐานได้ เมื่อ x เป็น string

float(x)

แปลง x เป็น floating-point number.

complex(real [,imag])

สร้าง complex number.

str(x)

แปลง object x เป็น a string

repr(x)

แปลง object x เป็น expression string.

eval(str)

หาค่า  string เป็น object.

tuple(s)

แปลง s เป็น tuple.

list(s)

แปลง s เป็น a list.

set(s)

แปลง s เป็น set.

dict(d)

สร้างตัวแปร dictionary. d ต้องเป็น sequence of (key,value) tuples.

frozenset(s)

แปลง s เป็น frozen set.

chr(x)

แปลง integer เป็น character.

unichr(x)

แปลง integer เป็น Unicode character.

ord(x)

แปลง character ตัวเดี่ยวๆ เป็นค่า integer.

hex(x)

แปลง integer เป็น  hexadecimal string.

oct(x)

แปลง  integer เป็น octal string.

ไพทอนสนับสนุน Unicode character.

ตัวอย่างการ type.py


var_dict = {}
var_dict['one'] = "This is one"
var_dict[2]     = "This is two"

print (type(var_dict))


ผลที่ได้: <class 'dict'>

กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
Warning: Unknown: Your script possibly relies on a session side-effect which exi
โดย Anonymous จ 22 มี.ค. 2010 12:18 am บอร์ด Programming - PHP
3
1135
จ 29 มี.ค. 2010 1:30 am โดย mindphp View Topic Warning: Unknown: Your script possibly relies on a session side-effect which exi
อยากทราบว่าในจูมล่า การย้ายข้อมูลจากเครื่องไปยังเซิฟเวอร์จริง
โดย beginner อ 21 มี.ค. 2010 8:35 pm บอร์ด สอบถามปัญหาการใช้ phpBB3, SMF, Joomla, Wordpress, CMS, CRM
8
3026
ศ 25 มิ.ย. 2010 12:57 pm โดย babypak View Topic อยากทราบว่าในจูมล่า  การย้ายข้อมูลจากเครื่องไปยังเซิฟเวอร์จริง
Smarty 3 ใกล้ๆ จะออกตัวเต็มแล้ว
โดย mindphp อ 21 มี.ค. 2010 2:18 am บอร์ด MindPHP News & Feedback
0
1273
อ 21 มี.ค. 2010 2:18 am โดย mindphp View Topic Smarty 3 ใกล้ๆ จะออกตัวเต็มแล้ว
Strict Standards: Only variables should be assigned by reference
โดย Anonymous ส 20 มี.ค. 2010 3:14 pm บอร์ด Programming - PHP
1
1353
ส 20 มี.ค. 2010 10:04 pm โดย mindphp View Topic Strict Standards: Only variables should be assigned by reference
เกี่ยวกับ Smarty Template ครับ
โดย Karol ส 20 มี.ค. 2010 1:42 pm บอร์ด Programming - PHP
3
1369
อ 21 มี.ค. 2010 2:10 am โดย mindphp View Topic เกี่ยวกับ Smarty Template ครับ
คลิกตรงหน้าmsnไม่ได้ครับทำไงดีครับ
โดย autsawin24 ส 20 มี.ค. 2010 6:30 am บอร์ด ถาม - ตอบ คอมพิวเตอร์
0
1147
ส 20 มี.ค. 2010 6:30 am โดย autsawin24 View Topic คลิกตรงหน้าmsnไม่ได้ครับทำไงดีครับ
ตัว & ใน function ครับ
โดย Karol ส 20 มี.ค. 2010 2:16 am บอร์ด Programming - PHP
2
1208
ส 20 มี.ค. 2010 1:35 pm โดย Karol View Topic ตัว & ใน function ครับ
ทำไมคลิกลิงค์ที่หน้าMSNไม่ไดครับ
โดย autsawin24 ศ 19 มี.ค. 2010 10:53 pm บอร์ด ถาม - ตอบ คอมพิวเตอร์
0
1310
ศ 19 มี.ค. 2010 10:53 pm โดย autsawin24 View Topic ทำไมคลิกลิงค์ที่หน้าMSNไม่ไดครับ