Variables · ตัวแปร
A box with a name
- A variable is a named box that holds a value.
- You put a value in the box, then use the name later.
- The value can be a number or a string.
กล่องที่มีชื่อ
- ตัวแปร คือกล่องที่มีชื่อเก็บค่า
- คุณใส่ค่าลงในกล่อง แล้วใช้ชื่อนั้นในภายหลัง
- ค่าอาจเป็นจำนวนหรือสตริง
Make a variable
- Write
name = valueto store a value. - The name is on the left. The value is on the right.
=means "store this value", not "is equal to".
สร้างตัวแปร
- เขียน
name = valueเพื่อเก็บค่า - ชื่อ อยู่ทางซ้าย ค่า อยู่ทางขวา
=หมายถึง "เก็บค่านี้นี้" ไม่ใช่ "เท่ากับ"
city = "Paris"
print(city)
Naming rules
- Use letters, digits, and the underscore
_. - A name cannot start with a digit and cannot have spaces.
- Names are case-sensitive:
scoreandScoreare different. - Pick clear names like
ageortotal_score.
กฎการตั้งชื่อ
- ใช้ตัวอักษร ตัวเลข และขีดล่าง
_ - ชื่อไม่สามารถเริ่มด้วยตัวเลขและไม่สามารถมีช่องว่าง
- ชื่อ แยกตัวพิมพ์ใหญ่-เล็ก:
scoreและScoreแตกต่างกัน - เลือกชื่อที่ชัดเจนเช่น
ageหรือtotal_score
Change a variable
- You can store a new value at any time.
- The new value replaces the old one.
- The name still points to one value — the latest one.
เปลี่ยนค่าตัวแปร
- คุณสามารถเก็บค่าใหม่ได้ทุกเมื่อ
- ค่าใหม่จะ แทนที่ ค่าเก่า
- ชื่อยังคงชี้ไปที่ค่าหนึ่ง — ค่าล่าสุด
A variable is a labelled box · ตัวแปรคือกล่องที่มีป้ายชื่อ
Step through the program. Each line stores a value in a named box; reassigning count overwrites its box. One name, one current value. · ทำทีละขั้นตอนผ่านโปรแกรม แต่ละบรรทัดจะเก็บค่าลงในกล่องที่มีชื่อ; การกำหนดค่าใหม่ count จะทับค่าเดิมในกล่องนั้น มีชื่อเดียวมีค่าปัจจุบันเพียงค่าเดียว
count = 1
count = 2
print(count)
Constants
- A constant is a value that should not change.
- Python has no special keyword for it.
- By convention, we write constant names in
UPPERCASE.
ค่านิยาม
- ค่านิยาม คือค่าที่ไม่ควรเปลี่ยนแปลง
- Python ไม่มีคำสำคัญพิเศษสำหรับสิ่งนี้
- ตาม ธรรมเนียม เราเขียนชื่อกนนิยามเป็นตัวพิมพ์ใหญ่
UPPERCASE
PI = 3.14159
print(PI)
Common mistakes
- Use
=to store a value: the name goes on the left, the value on the right. - Use a variable only after you have given it a value, or you get a NameError.
- Names cannot start with a number or contain spaces.
ข้อผิดพลาดที่พบบ่อย
- ใช้
=เพื่อเก็บค่า: ชื่ออยู่ทางซ้าย ค่าอยู่ทางขวา - ใช้ตัวแปรก็ต่อเมื่อคุณกำหนดค่าให้มันแล้ว มิฉะนั้นคุณจะเจอ NameError
- ชื่อไม่สามารถเริ่มด้วยตัวเลขหรือมีช่องว่าง
Now you try
- Use a variable for each task below.
- Press Check answer to test your code.
ลองดูเลย
- ใช้ตัวแปรสำหรับแต่ละงานด้านล่าง
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Create a variable called age and store the number 15 in it. · สร้างตัวแปรชื่อ age และเก็บตัวเลข 15 ไว้ในตัวแปรนั้น
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The variable color starts as "red". Reassign it so that it becomes "blue". · ตัวแปร color เริ่มต้นเป็น "red" กำหนดค่าใหม่ ให้กลายเป็น "blue"
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Constants are written in UPPERCASE by convention. Create a constant MAX_SCORE and set it to 100. · ค่าคงที่มักเขียนเป็นตัวพิมพ์ใหญ่ตามธรรมเนียม สร้างค่าคงที่ MAX_SCORE และตั้งค่าให้เท่ากับ 100
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
a is 1 and b is 2. Swap them, so a becomes 2 and b becomes 1, without typing the numbers 1 or 2 again. (Tip: a third variable can hold one value for a moment.) · a เป็น 1 และ b เป็น 2 สลับค่ากัน โดยทำให้ a กลายเป็น 2 และ b กลายเป็น 1 โดยไม่ต้องพิมพ์ตัวเลข 1 หรือ 2 ใหม่ (เคล็ดลับ: ตัวแปรที่สามสามารถเก็บค่าหนึ่งไว้ชั่วคราวได้)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่