Numbers & operators · ตัวเลขและตัวดำเนินการ
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.
จำนวนสองประเภท
- จำนวนเต็ม (integer) (
int) คือจำนวนเต็ม เช่น7หรือ-3 - ทศนิยม คือจำนวนที่มีจุดทศนิยม เช่น
2.5 - การหารด้วย
/จะให้ผลเป็นทศนิยมเสมอ แม้4 / 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.
บวก ลบ คูณ หาร
+บวก,-ลบ,*คูณ,/หาร- คุณสามารถใช้它与ตัวแปรหรือกับตัวเลขธรรมดา
- ช่องว่างรอบ operator เป็นทางเลือกแต่ทำให้โค้ดอ่านง่ายขึ้น
print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
Whole-number division, remainder, powers
//divides and keeps only the whole part (floor division).%gives the remainder (called modulo).**raises to a power:2 ** 3is 2×2×2.
การหารจำนวนเต็ม เศษเหลือ พลัง
//หารและเก็บเฉพาะส่วน จำนวนเต็ม (floor division)%ให้ เศษเหลือ (เรียกว่า modulo)**ยกกำลัง:2 ** 3คือ 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.
ลำดับความสำคัญของการดำเนินการ
- Python ทำ
**ก่อน แล้ว*///%แล้ว+- - นี่คือลำดับเดียวกันที่คุณใช้ในคณิตศาสตร์
- ใช้ วงเล็บ
(...)เพื่อเปลี่ยนลำดับ
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.
ข้อผิดพลาดที่พบบ่อย
/ให้ทศนิยมเสมอ (4 / 2คือ2.0); ใช้//เมื่อต้องการจำนวนเต็ม- พลังคือ
**ไม่ใช่^ "3" + "4"เชื่อมข้อความเข้าด้วยกันเป็น"34"— มันไม่บวกกับ7
Now you try
- Store each answer in the variable named in the task.
- Press Check answer to test your code.
ลองดูเลย
- เก็บคำตอบแต่ละอันในตัวแปรตามชื่อที่กำหนดในงาน
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Order of operations · ลำดับความสำคัญของการคำนวณ
Python evaluates × and ÷ before + and −, left to right. · Python คำนวณ × และ ÷ ก่อน + และ − จากซ้ายไปขวา
Set length to 8 and width to 5. Then store their product in a variable called area. · กำหนดให้ length เท่ากับ 8 และ width เท่ากับ 5 จากนั้นเก็บผลคูณไว้ในตัวแปรชื่อ area
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
You share 17 sweets among 5 children. Store how many each child gets in each (use //), and how many are left over in left (use %). · คุณแบ่ง 17 ลูกอมให้ 5 คน. เก็บจำนวนที่ แต่ละ คนจะได้รับไว้ใน each (ใช้ //), และจำนวนที่ เหลือ ไว้ใน left (ใช้ %).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Store 3 ** 2 + 1 in result. Then use parentheses to store (3 + 1) ** 2 in grouped. · จัดเก็บ 3 ** 2 + 1 ใน result จากนั้นใช้วงเล็บเพื่อจัดเก็บ (3 + 1) ** 2 ใน grouped
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
celsius is 37. Store the temperature in Fahrenheit in fahrenheit, using the formula celsius × 9 ÷ 5 + 32. · celsius คือ 37 จัดเก็บอุณหภูมิเป็นฟาเรนไฮต์ลงใน fahrenheit โดยใช้สูตร celsius × 9 ÷ 5 + 32
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่