Variables, constants & data types
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
English
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.
Tiếng Việt
Một biến là một hộp có tên
- Một biến là một tên lưu trữ một giá trị.
- Bạn đưa một giá trị vào bằng
=— điều này gọi là assignment (gán giá trị). - Sử dụng tên rõ ràng để chương trình dễ đọc.
age = 15
price = 2.50
name = "Ada"
print(age, price, name)
Explore · Khám phá
Variables as named boxes
Each = puts a value into a named box you can use later.
English
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.
Tiếng Việt
Năm loại dữ liệu
- IGCSE sử dụng năm loại dữ liệu cơ bản:
- integer (số nguyên) → Python
int; real (số thập phân) →float. - char (một ký tự), string (văn bản) →
str; Boolean (True/False) →bool.
score = 42 # integer
height = 1.75 # real
grade = "A" # char
town = "Cairo" # string
passed = True # Boolean
English
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.
Tiếng Việt
Hằng số không thay đổi
- Một hằng số là một giá trị không bao giờ thay đổi trong suốt quá trình chương trình chạy.
- Python không có từ khóa đặc biệt, nên chúng ta viết hằng số bằng
CAPITALS. - Điều này cảnh báo những người đọc khác: đừng thay đổi tôi.
PI = 3.14159
VAT_RATE = 0.20
print(PI)
English
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.
Tiếng Việt
Chuyển đổi giữa các loại
input()trả về một string, nên hãy chuyển đổi khi bạn cần một con số.int(x)tạo ra số nguyên;float(x)tạo ra số thập phân;str(x)tạo ra văn bản.
n = int("5")
print(n + 1) # 6
print("Total: " + str(n)) # Total: 5
English
In the exam: DECLARE and CONSTANT
- Pseudocode names a type with
DECLARE, and fixes a value withCONSTANT.
Tiếng Việt
Trong kỳ thi: DECLARE và CONSTANT
- Pseudocode chỉ định một loại bằng
DECLARE, và cố định một giá trị bằngCONSTANT.
DECLARE Age : INTEGER
Age ← 15
CONSTANT Pi ← 3.14
English
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.
Tiếng Việt
Lỗi thường gặp
- Gán giá trị cho biến trước khi sử dụng nó.
- Hằng số là một giá trị bạn không thay đổi — hãy đặt tên rõ ràng cho nó.
- Giữ đúng loại: số cho toán học, string cho văn bản.
English
Now you try
- Make variables and do a little maths with them.
- Press Check answer to test your code.
Tiếng Việt
Bây giờ bạn thử
- Tạo biến và thực hiện một chút toán học với chúng.
- Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
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.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
price is a real number and quantity is an integer. Work out the total cost (price × quantity).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Read a whole number with input(), convert it with int(...), then print the number plus 3. For input 7, the output should be 10.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.