เรื่องของตัวแปร Variables และก็ชนิดของตัวแปร ฮิตๆ
ก็ต้อง string, int, float
- การกำหนดตัวแปรแบบ float แบบไม่ได้ใส่ . ทศนิยม
ให้ใส่ float( ) ด้วย เช่น myfloat = float(10) ก็จะเท่ากับ 10.0
- ตัวแปรแบบ string จะอยู่ใน single quote หรือ double quotes เช่น mystring = ‘hello’ หรือ mystring = "hello"
- การกำหนดตัวแปร จะใส่ลงไปเป็นชุดเลยก็ได้ เช่น จะกำหนด a=3, b=4 ก็สามารถกำหนด a, b = 3, 4 ได้เลย
- ตัวแปรแบบ string จะอยู่ใน single quote หรือ double quotes เช่น mystring = ‘hello’ หรือ mystring = "hello"
- การกำหนดตัวแปร จะใส่ลงไปเป็นชุดเลยก็ได้ เช่น จะกำหนด a=3, b=4 ก็สามารถกำหนด a, b = 3, 4 ได้เลย
#กำหนดค่าให้ตัวแปร
mystring = "hello"
myfloat = 10.0
myint = 20
# testing code
if mystring == "hello":
print "String: %s" % mystring
# %s เอาไว้บอกตำแหน่งที่ตัวแปร
mystring จะมาแสดงค่า โดย %s ไว้สำหรับค่าที่เป็น
string
if isinstance(myfloat, float) and
myfloat == 10.0:
print "Float: %d" % myfloat
if isinstance(myint, int) or myint
==20 :
print "Integer: %d" % myint
# %d เอาไว้บอกตำแหน่งที่ตัวแปรที่ค่าเป็นตัวเลขมาแสดง( การ comment จะใช้ # )
ไม่มีความคิดเห็น:
แสดงความคิดเห็น