Số & toán tử
Two kinds of number
- An integer (
int) is a whole number, like7or-3. - A float is a number with a decimal point, like
2.5. - Division with
/always gives a float, even4 / 2→2.0.
Add, subtract, multiply, divide
+adds,-subtracts,*multiplies,/divides.- You can use them with variables or with plain numbers.
- Spaces around operators are optional but make code easier to read.

print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
Whole-number division, remainder, powers
//divides and rounds down to a whole number (floor division):7 // 2is3, and-7 // 2is-4.%gives the remainder (called modulo).**raises to a power:2 ** 3is 2×2×2.
print(7 // 2)
print(7 % 2)
print(2 ** 5)
Order of operations
- Python does
**first, then*///%, then+-. - This is the same order you use in maths.
- Use parentheses
(...)to change the order.
print(2 + 3 * 4)
print((2 + 3) * 4)
Common mistakes
/always gives a float (4 / 2is2.0); use//when you want a whole number.- Power is
**, not^. "3" + "4"joins text into"34"— it does not add to7.
Now you try
- Store each answer in the variable named in the task.
- Press Check answer to test your code.
Thứ tự thực hiện phép tính
Python tính toán × và ÷ trước + và −, từ trái sang phải.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Gán length bằng 8 và width bằng 5. Sau đó lưu tích của chúng vào một biến tên là area.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Bạn chia 17 viên kẹo cho 5 đứa trẻ. Lưu số mỗi đứa trẻ nhận được vào each (sử dụng //), và số còn dư vào left (sử dụng %).
Nhấn Chạy để xem kết quả ở đây.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Lưu 3 ** 2 + 1 vào result. Sau đó dùng dấu ngoặc đơn để lưu (3 + 1) ** 2 vào grouped.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
celsius là 37. Lưu nhiệt độ theo độ F vào fahrenheit, sử dụng công thức celsius × 9 ÷ 5 + 32.
Nhấn Chạy để xem kết quả ở đây.