Literals and Variables Literals and Variables: ค่าคงที่และตัวแปร
Learn literals, the basic data types, and storing values in variables เรียนรู้ literal ชนิดข้อมูลพื้นฐาน และการเก็บค่าในตัวแปร
A literal is a value written directly in the code, such as 10, 3.14,
"Hello", or True. A variable is a name that stores a value so you can
reuse it or change it later. This lesson follows on from Get Started, where you used print().
Literal คือค่าที่เขียนตรงในโค้ด เช่น 10, 3.14, "Hello", True
Variable คือชื่อที่ใช้เก็บค่า เพื่อนำไปใช้ซ้ำหรือเปลี่ยนค่าทีหลัง
บทนี้ต่อจาก Get Started ที่ใช้ print() แล้ว
1 Integer and float literals 1 Literal จำนวนเต็มและทศนิยม
A whole number (int) is written with no dot, e.g. 10. A decimal number (float) has a dot, e.g.
3.14. You can show a literal directly with print().
จำนวนเต็ม (int) เขียนเป็นตัวเลขไม่มีจุด เช่น 10 — ทศนิยม (float) มีจุด เช่น 3.14
ใช้ print() แสดงค่า literal ได้โดยตรง
10 3.14
2 String literals 2 Literal สตริง
A string (str) is text inside quotation marks — you can use either '...' or "...".
สตริง (str) คือข้อความในเครื่องหมายคำพูด — ใช้ได้ทั้ง '...' และ "..."
Hello Python
3 Boolean and None literals 3 Literal บูลีนและ None
A boolean has two values: True and False (capital first letter). None
means "no value" — it can also be shown with print().
ค่าบูลีนมีสองค่าคือ True และ False (ตัวพิมพ์ใหญ่ตัวแรก)
ส่วน None หมายถึงไม่มีค่า — แสดงผลด้วย print() ได้เช่นกัน
True False None
4 Create a variable with = 4 สร้างตัวแปรด้วย =
Use = to store a value under a variable name, then reuse that name with print(). The
name goes on the left of =, the value on the right.
ใช้เครื่องหมาย = เพื่อเก็บค่าไว้ในชื่อตัวแปร แล้วเรียกใช้ชื่อนั้นกับ print() ได้ซ้ำ
โดยชื่ออยู่ทางซ้ายของ = และค่าอยู่ทางขวา
PLS-C 20
5 Change a variable's value 5 เปลี่ยนค่าตัวแปร
Assign to the same name again with = — the old value is replaced by the new one.
กำหนดค่าใหม่ให้ชื่อเดิมด้วย = อีกครั้ง — ค่าเก่าจะถูกแทนที่ด้วยค่าใหม่
10
6 Check a type with type() 6 ดูชนิดด้วย type()
Use type() to see the type of a value — the result looks like <class '…'>,
e.g. int, float, str.
ใช้ type() เพื่อดูชนิดของค่า — ผลลัพธ์อยู่ในรูป <class '…'> เช่น int, float, str
<class 'int'> <class 'float'> <class 'str'>
Rules for naming variables กฎการตั้งชื่อตัวแปร
| Ruleกฎ | Allowedใช้ได้ | Not allowedใช้ไม่ได้ |
|---|---|---|
Start with a letter or _
ขึ้นต้นด้วยตัวอักษรหรือ _
|
name, _count |
2name |
Only letters, digits, and _
ใช้ได้เฉพาะตัวอักษร ตัวเลข และ _
|
student_name, age2 |
my-name, my name |
| Do not use a Python keyword as a name ห้ามใช้ Python keywords เป็นชื่อตัวแปร | total, msg |
class, True, for |
Keywords are reserved words that define Python's grammar — they cannot be used as variable names. The table below lists the basic keywords covered in this lesson (33 words).
Keywords คือคำสงวนของภาษา Python ที่ใช้กำหนดไวยากรณ์ — ห้ามนำมาตั้งเป็นชื่อตัวแปร ตารางด้านล่างคือ keywords พื้นฐานที่ใช้สอนในบทนี้ (33 คำ)
False |
None |
True |
and |
as |
assert |
break |
class |
continue |
def |
del |
elif |
else |
except |
finally |
for |
from |
global |
if |
import |
in |
is |
lambda |
nonlocal |
not |
or |
pass |
raise |
return |
try |
while |
with |
yield |
7 Examples of valid / invalid names 7 ตัวอย่างชื่อที่ใช้ได้ / ใช้ไม่ได้
A valid name can be assigned a value and printed — the keyword lines are commented out because they cannot be used as names.
ชื่อที่ถูกต้องสามารถกำหนดค่าและ print() ได้ — บรรทัดที่เป็น keywords ถูกคอมเมนต์ไว้เพราะใช้ไม่ได้
18 100
Note: a name like print is not a keyword but a built-in function name — it is
syntactically allowed as a variable name, but it would hide the original function, so avoid it.
หมายเหตุ: ชื่ออย่าง print ไม่ใช่ keyword แต่เป็นชื่อ built-in function —
ใช้เป็นชื่อตัวแปรได้ทางไวยากรณ์ แต่จะทับฟังก์ชันเดิม จึงควรหลีกเลี่ยง
Types of variable ชนิดของตัวแปร
A variable can hold values of several types — below are the basic types you meet most often in this lesson.
ค่าที่เก็บในตัวแปรมีหลายชนิด — ด้านล่างคือชนิดพื้นฐานที่พบบ่อยในบทเรียนนี้
8 Number
Numbers come in two main kinds: int (whole number) and float (decimal).
ตัวเลขมีสองแบบหลัก: int (จำนวนเต็ม) และ float (ทศนิยม)
10 3.14
9 Boolean
A truth value has only two values: True and False (capital first letter).
ค่าความจริงมีสองค่าเท่านั้น: True และ False (ตัวพิมพ์ใหญ่ตัวแรก)
True False
10 String
Text goes inside quotation marks "..." or '...'. A single character is also a string
(see the Char section below).
ข้อความอยู่ในเครื่องหมายคำพูด "..." หรือ '...'
ตัวอักษรหนึ่งตัวก็เป็น string เช่นกัน (ดูหัวข้อ Char ด้านล่าง)
Hello A
11 List
A list is a sequence of values in square brackets [] — reach each slot by an index starting
from 0.
List คือลำดับของค่าในวงเล็บเหลี่ยม [] — เข้าถึงแต่ละช่องด้วยดัชนีเริ่มจาก 0
[80, 90, 75] 80
12 Dictionary (dict)
A dictionary stores key–value pairs in curly braces {} — look up a value by its key.
Dictionary เก็บคู่ key–value ในวงเล็บปีกกา {} — เรียกค่าด้วย key
{'name': 'Ann', 'age': 20}
Ann
Char — single characters, and their link to lists and ASCII Char — ตัวอักษร เชื่อมกับ list และ ASCII
Python has no separate char type like C — a single character is a string of length
1, e.g. ch = "A".
ใน Python ไม่มีชนิด char แยกเหมือนภาษา C —
ตัวอักษรหนึ่งตัวคือ string ความยาว 1 เช่น ch = "A"
13 Char and lists 13 Char เชื่อมกับ list
A string is a sequence of characters, reachable by index like a list, e.g. "Hi"[0] gives
"H". Convert it to a list of characters with list(...) — each slot is a "char"
in this lesson's sense (a string of length 1).
String เป็นลำดับตัวอักษร เข้าถึงด้วยดัชนีได้คล้าย list เช่น "Hi"[0] ได้ "H"
แปลงเป็น list ของตัวอักษรได้ด้วย list(...) — แต่ละช่องคือ “char” ในความหมายบทเรียน (string ความยาว 1)
H ['H', 'i'] i
14 Char and ASCII 14 Char เชื่อมกับ ASCII
Computers store characters as numbers — the ASCII standard maps codes to characters. In
Python, ord("A") gives 65 and chr(65) gives "A".
คอมพิวเตอร์เก็บตัวอักษรเป็นตัวเลข — มาตรฐาน ASCII จับคู่รหัสกับตัวอักษร
ใน Python ใช้ ord("A") ได้ 65 และ chr(65) ได้ "A"
65 A 32
ASCII table (printable) ตาราง ASCII (printable)
The table below shows printable ASCII, decimal codes 32–126 (from space to
~).
ตารางด้านล่างแสดง printable ASCII รหัสทศนิยม 32–126 (จากช่องว่าง space ถึง ~)
| Dec | Hex | Char |
|---|---|---|
32 |
0x20 |
(space) |
33 |
0x21 |
! |
34 |
0x22 |
" |
35 |
0x23 |
# |
36 |
0x24 |
$ |
37 |
0x25 |
% |
38 |
0x26 |
& |
39 |
0x27 |
' |
40 |
0x28 |
( |
41 |
0x29 |
) |
42 |
0x2A |
* |
43 |
0x2B |
+ |
44 |
0x2C |
, |
45 |
0x2D |
- |
46 |
0x2E |
. |
47 |
0x2F |
/ |
48 |
0x30 |
0 |
49 |
0x31 |
1 |
50 |
0x32 |
2 |
51 |
0x33 |
3 |
52 |
0x34 |
4 |
53 |
0x35 |
5 |
54 |
0x36 |
6 |
55 |
0x37 |
7 |
56 |
0x38 |
8 |
57 |
0x39 |
9 |
58 |
0x3A |
: |
59 |
0x3B |
; |
60 |
0x3C |
< |
61 |
0x3D |
= |
62 |
0x3E |
> |
63 |
0x3F |
? |
64 |
0x40 |
@ |
65 |
0x41 |
A |
66 |
0x42 |
B |
67 |
0x43 |
C |
68 |
0x44 |
D |
69 |
0x45 |
E |
70 |
0x46 |
F |
71 |
0x47 |
G |
72 |
0x48 |
H |
73 |
0x49 |
I |
74 |
0x4A |
J |
75 |
0x4B |
K |
76 |
0x4C |
L |
77 |
0x4D |
M |
78 |
0x4E |
N |
79 |
0x4F |
O |
80 |
0x50 |
P |
81 |
0x51 |
Q |
82 |
0x52 |
R |
83 |
0x53 |
S |
84 |
0x54 |
T |
85 |
0x55 |
U |
86 |
0x56 |
V |
87 |
0x57 |
W |
88 |
0x58 |
X |
89 |
0x59 |
Y |
90 |
0x5A |
Z |
91 |
0x5B |
[ |
92 |
0x5C |
\ |
93 |
0x5D |
] |
94 |
0x5E |
^ |
95 |
0x5F |
_ |
96 |
0x60 |
` |
97 |
0x61 |
a |
98 |
0x62 |
b |
99 |
0x63 |
c |
100 |
0x64 |
d |
101 |
0x65 |
e |
102 |
0x66 |
f |
103 |
0x67 |
g |
104 |
0x68 |
h |
105 |
0x69 |
i |
106 |
0x6A |
j |
107 |
0x6B |
k |
108 |
0x6C |
l |
109 |
0x6D |
m |
110 |
0x6E |
n |
111 |
0x6F |
o |
112 |
0x70 |
p |
113 |
0x71 |
q |
114 |
0x72 |
r |
115 |
0x73 |
s |
116 |
0x74 |
t |
117 |
0x75 |
u |
118 |
0x76 |
v |
119 |
0x77 |
w |
120 |
0x78 |
x |
121 |
0x79 |
y |
122 |
0x7A |
z |
123 |
0x7B |
{ |
124 |
0x7C |
| |
125 |
0x7D |
} |
126 |
0x7E |
~ |
The printable ASCII table on this page is summarised from Wikipedia: ASCII — Printable character table. ตารางรหัส ASCII (printable) ในหน้านี้สรุปจาก Wikipedia: ASCII — Printable character table
Note: codes 0–31 and 127 are control characters and are not shown
here. Modern languages use Unicode, but ASCII is its first 128 characters.
หมายเหตุ: รหัส 0–31 และ 127 เป็น control characters จึงไม่แสดงในตารางนี้
ภาษาสมัยใหม่ใช้ Unicode แต่ ASCII คือชุดพื้นฐาน 128 ตัวแรก
15 None value
None means "no value" — its type is NoneType. Use it when a value is not set yet,
or to say a variable is deliberately empty. Important: None is not 0, not
"" (an empty string), and not False.
None หมายถึง “ไม่มีค่า” — ชนิดของมันคือ NoneType
ใช้เมื่อยังไม่กำหนดค่า หรือต้องการบอกว่าตัวแปรว่างโดยเจตนา
ความต่างสำคัญ: None ไม่ใช่ 0, ไม่ใช่ "" (สตริงว่าง) และไม่ใช่ False
None <class 'NoneType'>
16 Deleting a variable 16 Deleting Variable
The del statement removes a variable name from the current scope — using that name again after
del raises an error. This is different from x = None: assigning None keeps
the name but its value is "no value", while del x removes the name entirely.
คำสั่ง del ใช้ลบชื่อตัวแปรออกจากขอบเขตปัจจุบัน — หลัง del แล้วเรียกชื่อนั้นอีกจะเกิดข้อผิดพลาด
แยกจาก x = None: การกำหนดเป็น None คือยังมีชื่อตัวแปรอยู่ แต่ค่าเป็น “ไม่มีค่า”
ส่วน del x คือลบชื่อออกไปเลย
10
Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab
References References / แหล่งอ้างอิง
Sources used for this lesson:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
- Python keywords / identifiers — The Python Language Reference — Keywords / Identifiers (naming rules and reserved words) (กฎการตั้งชื่อและคำสงวน)
- Built-in types (number, bool, str, list, dict, None) — Built-in Types
-
delstatement — The del statement -
ord/chr— Built-in Functions - ASCII table — Wikipedia: ASCII (source of this page's printable table); also (แหล่งสรุปตาราง printable ในหน้านี้); เสริม ascii(7)