ตัวเลขและตัวดำเนินการ
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.
ลำดับความสำคัญของการคำนวณ
Python คำนวณ × และ ÷ ก่อน + และ − จากซ้ายไปขวา
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
กำหนดให้ length เท่ากับ 8 และ width เท่ากับ 5 จากนั้นเก็บผลคูณไว้ในตัวแปรชื่อ area
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
คุณแบ่ง 17 ลูกอมให้ 5 คน. เก็บจำนวนที่ แต่ละ คนจะได้รับไว้ใน each (ใช้ //), และจำนวนที่ เหลือ ไว้ใน left (ใช้ %).
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
จัดเก็บ 3 ** 2 + 1 ใน result จากนั้นใช้วงเล็บเพื่อจัดเก็บ (3 + 1) ** 2 ใน grouped
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
celsius คือ 37 จัดเก็บอุณหภูมิเป็นฟาเรนไฮต์ลงใน fahrenheit โดยใช้สูตร celsius × 9 ÷ 5 + 32
คลิก Run เพื่อดูผลลัพธ์ที่นี่