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 ได้โดยตรง

[1]
[output]
10
3.14

2 String literals 2 Literal สตริง

A string (str) is text inside quotation marks — you can use either '...' or "...".

สตริง (str) คือข้อความในเครื่องหมายคำพูด — ใช้ได้ทั้ง '...' และ "..."

[2]
[output]
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() ได้เช่นกัน

[3]
[output]
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() ได้ซ้ำ โดยชื่ออยู่ทางซ้ายของ = และค่าอยู่ทางขวา

[4]
[output]
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.

กำหนดค่าใหม่ให้ชื่อเดิมด้วย = อีกครั้ง — ค่าเก่าจะถูกแทนที่ด้วยค่าใหม่

[5]
[output]
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

[6]
[output]
<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 ถูกคอมเมนต์ไว้เพราะใช้ไม่ได้

[7]
[output]
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 (ทศนิยม)

[8]
[output]
10
3.14

9 Boolean

A truth value has only two values: True and False (capital first letter).

ค่าความจริงมีสองค่าเท่านั้น: True และ False (ตัวพิมพ์ใหญ่ตัวแรก)

[9]
[output]
True
False

10 String

Text goes inside quotation marks "..." or '...'. A single character is also a string (see the Char section below).

ข้อความอยู่ในเครื่องหมายคำพูด "..." หรือ '...' ตัวอักษรหนึ่งตัวก็เป็น string เช่นกัน (ดูหัวข้อ Char ด้านล่าง)

[10]
[output]
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

[11]
[output]
[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

[12]
[output]
{'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)

[13]
[output]
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"

[14]
[output]
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

[15]
[output]
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 คือลบชื่อออกไปเลย

[16]
[output]
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:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. Python keywords / identifiers — The Python Language Reference — Keywords / Identifiers (naming rules and reserved words) (กฎการตั้งชื่อและคำสงวน)
  2. Built-in types (number, bool, str, list, dict, None) — Built-in Types
  3. del statement — The del statement
  4. ord / chr — Built-in Functions
  5. ASCII table — Wikipedia: ASCII (source of this page's printable table); also (แหล่งสรุปตาราง printable ในหน้านี้); เสริม ascii(7)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What is a literal?Literal คืออะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) A value written directly in the codeค่าที่เขียนตรงในโค้ด

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) A value written directly in the codeค่าที่เขียนตรงในโค้ด
    Correct! A literal is a value written directly in the code, e.g. 10, 3.14, "Hello", True.ถูกต้อง! Literal คือค่าที่เขียนตรงในโค้ด เช่น 10, 3.14, "Hello", True
  • (B) A loop statementคำสั่งวนซ้ำ
    Wrong: a loop statement is not what 'literal' means here.ผิด: คำสั่งวนซ้ำไม่ใช่ความหมายของ Literal ในบทนี้
  • (C) A program file nameชื่อไฟล์โปรแกรม
    Wrong: a file name has nothing to do with a literal.ผิด: ชื่อไฟล์ไม่เกี่ยวข้องกับ Literal
  • (D) Only an output functionฟังก์ชันแสดงผลอย่างเดียว
    Wrong: the output function is print(), not a literal.ผิด: ฟังก์ชันแสดงผลคือ print() ไม่ใช่ Literal

ข้อ 2

2 / 10

Which value is a float literal?ค่าใดเป็น float literal?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) 3.143.14

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 1010
    Wrong: 10 is an integer (int) with no decimal point.ผิด: 10 เป็นจำนวนเต็ม (int) ไม่มีจุดทศนิยม
  • (B) 3.143.14
    Correct! 3.14 is a decimal (float), as in the lesson.ถูกต้อง! 3.14 เป็นทศนิยม (float) ตามตัวอย่างในบท
  • (C) "3.14""3.14"
    Wrong: "3.14" is a string (str) because it is in quotes.ผิด: "3.14" เป็นสตริง (str) เพราะอยู่ในเครื่องหมายคำพูด
  • (D) TrueTrue
    Wrong: True is a boolean (bool), not a float.ผิด: True เป็นค่าบูลีน (bool) ไม่ใช่ float

ข้อ 3

3 / 10

After x = 5 then x = 10, what is the value of x?หลังรัน x = 5 แล้ว x = 10 ค่าของ x คืออะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 1010

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 55
    Wrong: the 5 was replaced when x = 10 was assigned.ผิด: ค่า 5 ถูกแทนที่แล้วเมื่อกำหนด x = 10
  • (B) 1515
    Wrong: = assigns a new value; it does not add.ผิด: การใช้ = เป็นการกำหนดค่าใหม่ ไม่ใช่การบวก
  • (C) 1010
    Correct! Assigning again with = replaces the old value, so x is 10.ถูกต้อง! การกำหนดค่าใหม่ด้วย = จะแทนที่ค่าเก่า ดังนั้น x เหลือ 10
  • (D) ErrorError
    Wrong: a variable's value can be changed; there is no error.ผิด: สามารถเปลี่ยนค่าตัวแปรได้ ไม่เกิด Error

ข้อ 4

4 / 10

Which is a valid variable name in Python?ชื่อตัวแปรใดใช้ได้ใน Python?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) student_namestudent_name

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 2name2name
    Wrong: a variable name cannot start with a digit.ผิด: ชื่อตัวแปรขึ้นต้นด้วยตัวเลขไม่ได้
  • (B) my-namemy-name
    Wrong: a hyphen (-) is not allowed in a variable name.ผิด: ใช้เครื่องหมายขีด (-) ในชื่อตัวแปรไม่ได้
  • (C) classclass
    Wrong: class is a keyword and cannot be a variable name.ผิด: class เป็น keyword จึงใช้เป็นชื่อตัวแปรไม่ได้
  • (D) student_namestudent_name
    Correct! It starts with a letter and uses _, which the rules allow.ถูกต้อง! ขึ้นต้นด้วยตัวอักษร และใช้ _ ได้ตามกฎในตาราง

ข้อ 5

5 / 10

From the lesson: with c = "Hi", what does type(c) give?จากตัวอย่างในบท: c = "Hi" แล้ว type(c) ได้ผลลัพธ์ใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) <class 'str'><class 'str'>

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) <class 'int'><class 'int'>
    Wrong: int is the whole-number type, e.g. a = 10.ผิด: int เป็นชนิดของจำนวนเต็ม เช่น a = 10
  • (B) <class 'str'><class 'str'>
    Correct! c = "Hi" is a string, so you get <class 'str'>.ถูกต้อง! c = "Hi" เป็นสตริง จึงได้ <class 'str'>
  • (C) <class 'float'><class 'float'>
    Wrong: float is the decimal type, e.g. b = 3.14.ผิด: float เป็นชนิดของทศนิยม เช่น b = 3.14
  • (D) <class 'bool'><class 'bool'>
    Wrong: bool is the True/False type, not a string.ผิด: bool เป็นชนิดของ True/False ไม่ใช่สตริง

ข้อ 6

6 / 10

How do you reach the first item of the list scores = [80, 90, 75]?การเข้าถึงสมาชิกตัวแรกของ list scores = [80, 90, 75] เขียนอย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) scores[0]scores[0]

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) scores[1]scores[1]
    Wrong: indexing starts at 0, so scores[1] is 90.ผิด: ดัชนีเริ่มที่ 0 ดังนั้น scores[1] คือ 90
  • (B) scores.firstscores.first
    Wrong: a list has no 'first' attribute.ผิด: list ไม่มีแอตทริบิวต์ first
  • (C) scores[0]scores[0]
    Correct! Indexing starts at 0, so the first item is scores[0] = 80.ถูกต้อง! ดัชนีเริ่มจาก 0 สมาชิกตัวแรกคือ scores[0] = 80
  • (D) scores(0)scores(0)
    Wrong: use square brackets [ ], not round brackets.ผิด: ต้องใช้วงเล็บเหลี่ยม [ ] ไม่ใช่วงเล็บกลม

ข้อ 7

7 / 10

From person = {"name": "Ann", "age": 20}, how do you read the name?จาก person = {"name": "Ann", "age": 20} จะอ่านชื่อได้อย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) person["name"]person["name"]

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) person["name"]person["name"]
    Correct! A dict looks up a value by its key in square brackets.ถูกต้อง! dict เรียกค่าด้วย key ในวงเล็บเหลี่ยม
  • (B) person[0]person[0]
    Wrong: a dict uses keys, not numeric indexes like a list.ผิด: dict ใช้ key ไม่ใช่ดัชนีตัวเลขแบบ list
  • (C) person.nameperson.name
    Wrong: basic dict access uses ["key"].ผิด: การเข้าถึงค่าใน dict พื้นฐานใช้ ["key"]
  • (D) person("name")person("name")
    Wrong: a dict is not a function; use square brackets.ผิด: dict ไม่ใช่ฟังก์ชัน ต้องใช้วงเล็บเหลี่ยม

ข้อ 8

8 / 10

Which statement about None is correct?ข้อใดถูกต้องเกี่ยวกับ None?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) None means 'no value'; its type is NoneTypeNone หมายถึง 'ไม่มีค่า' ชนิดคือ NoneType

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) None equals 0None เท่ากับ 0
    Wrong: None is not 0.ผิด: None ไม่ใช่ 0
  • (B) None equals the empty string ""None เท่ากับสตริงว่าง ""
    Wrong: None is not an empty string.ผิด: None ไม่ใช่สตริงว่าง
  • (C) None means 'no value'; its type is NoneTypeNone หมายถึง 'ไม่มีค่า' ชนิดคือ NoneType
    Correct! As the lesson explains, None is the value that means 'no value'.ถูกต้อง! ตามที่บทอธิบาย None คือค่าที่แปลว่าไม่มีค่า
  • (D) None is the same as False in every caseNone เหมือนกับ False ทุกกรณี
    Wrong: None is not False, even though it is treated as falsy in a condition.ผิด: None ไม่ใช่ False แม้จะถือเป็นค่าเท็จในเงื่อนไข

ข้อ 9

9 / 10

What is the difference between x = None and del x?ความต่างระหว่าง x = None กับ del x คืออะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) x = None keeps the name x but its value is 'no value'; del x removes the name entirelyx = None เก็บชื่อ x ไว้แต่ค่าเป็น 'ไม่มีค่า' ส่วน del x ลบชื่อออกทั้งหมด

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Both remove the name x in the same wayทั้งสองลบชื่อ x ออกเหมือนกัน
    Wrong: after x = None the name x still exists.ผิด: x = None ยังมีชื่อ x อยู่
  • (B) del x sets x to 0del x ตั้งค่า x เป็น 0
    Wrong: del removes the name; it does not set it to 0.ผิด: del ลบชื่อออก ไม่ได้ตั้งค่าเป็น 0
  • (C) There is no differenceไม่มีความต่าง
    Wrong: there is a clear difference, as the lesson explains.ผิด: มีความต่างชัดเจนตามที่บทอธิบาย
  • (D) x = None keeps the name x but its value is 'no value'; del x removes the name entirelyx = None เก็บชื่อ x ไว้แต่ค่าเป็น 'ไม่มีค่า' ส่วน del x ลบชื่อออกทั้งหมด
    Correct! Exactly as the lesson explains.ถูกต้อง! ตามคำอธิบายในบท

ข้อ 10

10 / 10

What do ord("A") and chr(65) return?ord("A") และ chr(65) ให้ผลลัพธ์อะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 65 and "A"65 และ "A"

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 65 and "A"65 และ "A"
    Correct! ord turns a character into its ASCII code, and chr turns a code back into a character.ถูกต้อง! ord แปลงตัวอักษรเป็นรหัส ASCII และ chr แปลงรหัสกลับเป็นตัวอักษร
  • (B) "A" and 65"A" และ 65
    Wrong: they are swapped — ord("A") gives 65.ผิด: สลับกัน ord("A") ได้ 65
  • (C) 65 and 6665 และ 66
    Wrong: chr(65) is "A", not a number.ผิด: chr(65) คือ "A" ไม่ใช่ตัวเลข
  • (D) Both raise an errorทั้งคู่เกิด Error
    Wrong: ord and chr are built-in functions and work fine.ผิด: ord และ chr เป็น built-in function ใช้ได้ปกติ