Variables, constants & data types · ตัวแปร, ค่าคงที่ & ประเภทข้อมูล
A variable is a named box
- A variable is a name that stores a value.
- You put a value in with
=— this is called assignment. - Use a clear name so the program is easy to read.
ตัวแปรคือกล่องที่มีชื่อ
- ตัวแปร คือชื่อที่ใช้เก็บค่า
- คุณใส่ค่าเข้าไปด้วย
=— นี่คือสิ่งที่เรียกว่า assignment - ใช้ชื่อที่ชัดเจนเพื่อให้โปรแกรมอ่านง่าย
age = 15
price = 2.50
name = "Ada"
print(age, price, name)
Variables as named boxes · ตัวแปรเป็นกล่องที่มีชื่อ
Each = puts a value into a named box you can use later. · แต่ละ = ใส่ค่าลงใน กล่องที่มีชื่อ ที่คุณสามารถใช้ทีหลัง
The five data types
- IGCSE uses five basic data types:
- integer (whole number) → Python
int; real (decimal) →float. - char (one letter), string (text) →
str; Boolean (True/False) →bool.
ประเภทข้อมูลทั้งห้า
- IGCSE ใช้ ประเภทข้อมูล พื้นฐาน 5 ชนิด:
- integer (จำนวนเต็ม) → Python
int; real (ทศนิยม) →float - char (ตัวอักษรเดียว), string (ข้อความ) →
str; Boolean (True/False) →bool
score = 42 # integer
height = 1.75 # real
grade = "A" # char
town = "Cairo" # string
passed = True # Boolean
Constants stay the same
- A constant is a value that never changes while the program runs.
- Python has no special keyword, so we write constants in
CAPITALS. - This warns other readers: do not change me.
ค่าคงที่ไม่เปลี่ยนแปลง
- ค่าคงที่ คือค่าที่ไม่เปลี่ยนแปลงตลอดช่วงที่โปรแกรมทำงาน
- Python ไม่มีคำ reserved เฉพาะ所以我们用大写字母写常量在
CAPITALS - สิ่งนี้จะเตือนผู้อ่านว่า: อย่าเปลี่ยนฉัน
PI = 3.14159
VAT_RATE = 0.20
print(PI)
Changing between types
input()gives a string, so convert it when you need a number.int(x)makes a whole number;float(x)a decimal;str(x)makes text.
การแปลงระหว่างประเภทข้อมูล
input()ให้เป็น string ดังนั้นต้องแปลงเมื่อต้องการตัวเลขint(x)สร้างจำนวนเต็ม;float(x)สร้างทศนิยม;str(x)สร้างข้อความ
n = int("5")
print(n + 1) # 6
print("Total: " + str(n)) # Total: 5
In the exam: DECLARE and CONSTANT
- Pseudocode names a type with
DECLARE, and fixes a value withCONSTANT.
ในการสอบ: DECLARE และ CONSTANT
- Pseudocode กำหนดชนิดด้วย
DECLAREและกำหนดค่าด้วยCONSTANT
DECLARE Age : INTEGER
Age ← 15
CONSTANT Pi ← 3.14
Common mistakes
- Give a variable a value before you use it.
- A constant is a value you do not change — name it clearly.
- Keep the right type: numbers for maths, strings for text.
ข้อผิดพลาดที่พบบ่อย
- กำหนดค่าให้ตัวแปรก่อนนำไปใช้
- ค่าคงที่เป็นค่าที่คุณไม่เปลี่ยน — ตั้งชื่อให้ชัดเจน
- รักษาชนิดให้ถูกต้อง:的数字用于数学,字符串用于文本。
Now you try
- Make variables and do a little maths with them.
- Press Check answer to test your code.
ลองดูเลย
- สร้างตัวแปรและคำนวณทางคณิตศาสตร์เล็กน้อยด้วยมัน
- กด Check answer เพื่อทดสอบโค้ดของคุณ
The variables width and height are set for you. Work out the rectangle's area (width × height) and store it in a variable called area. · ตัวแปร width และ height ถูกกำหนดให้คุณแล้ว. หา พื้นที่ ของสี่เหลี่ยมผืนผ้า (width × height) และเก็บไว้ในตัวแปรชื่อ area
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
price is a real number and quantity is an integer. Work out the total cost (price × quantity). · price เป็น real number และ quantity เป็น integer. หาค่า total (ราคา × จำนวน)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Read a whole number with input(), convert it with int(...), then print the number plus 3. For input 7, the output should be 10. · อ่านจำนวนเต็มด้วย input(), แปลงด้วย int(...), แล้วพิมพ์จำนวนนั้น บวก 3. สำหรับอินพุต 7, เอาต์พุตควรจะเป็น 10
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่