Integers and Float: whole numbers, decimals, and arithmetic Integers and Float: จำนวนเต็ม ทศนิยม และการคำนวณ

Learn int / float, comparisons, arithmetic, and built-in mathematical functions เรียนรู้ int / float, การเปรียบเทียบ เลขคณิต และ built-in mathematical functions

Numbers in Python are mainly int (whole numbers) and float (decimals). This lesson compares numbers, does arithmetic, uses augmented assignment such as +=, and tries the language's built-in mathematical functions one by one.

จำนวนใน Python แบ่งหลักๆ เป็น int (จำนวนเต็ม) และ float (ทศนิยม) บทนี้จะเปรียบเทียบตัวเลข คำนวณด้วยเลขคณิต ใช้ augmented assignment เช่น += และทดลองฟังก์ชันคณิตศาสตร์ในตัวภาษาทีละตัว

1 Int (whole number) 1 Int (จำนวนเต็ม)

An int is a whole number with no decimal point, e.g. 10, -3, 0. type() shows it as <class 'int'>.

int คือจำนวนเต็ม ไม่มีจุดทศนิยม เช่น 10, -3, 0 ใช้ type() ตรวจชนิดได้ว่าเป็น <class 'int'>

[1]
[output]
10
<class 'int'>

2 Float (decimal) 2 Float (ทศนิยม)

A float is a decimal number with a dot, e.g. 3.14, -2.5. type() shows it as <class 'float'>.

float คือจำนวนทศนิยม มีจุด เช่น 3.14, -2.5 เมื่อแสดงชนิดด้วย type() จะได้ <class 'float'>

[2]
[output]
3.14
<class 'float'>

3 Comparison operators 3 Mathematical Operators (การเปรียบเทียบ)

Comparison operators work on numbers and return a boolean, True / False.

ตัวดำเนินการเปรียบเทียบใช้กับตัวเลขได้ และคืนค่าเป็นบูลีน True / False

Operator Name Meaningความหมาย
==Equalequal toเท่ากัน
!=Not equalnot equal toไม่เท่ากัน
>Greater thangreater thanมากกว่า
<Less thanless thanน้อยกว่า
>=Greater or equalgreater than or equalมากกว่าหรือเท่ากับ
<=Less or equalless than or equalน้อยกว่าหรือเท่ากับ

3.1 Try comparing numbers 3.1 ทดลองเปรียบเทียบตัวเลข

Try >, <=, and != with numbers and see the boolean result.

ลองใช้ > <= และ != กับตัวเลข แล้วดูผลเป็นบูลีน

[3]
[output]
True
True
True

4 Arithmetic operators 4 Arithmetic Operators

Basic arithmetic in Python: + add, - subtract, * multiply, / divide (gives a float), // floor-divide to a whole number, % remainder, ** power.

เลขคณิตพื้นฐานใน Python: + บวก, - ลบ, * คูณ, / หาร (ได้ float), // หารปัดลงเป็นจำนวนเต็ม, % หารเอาเศษ, ** ยกกำลัง

Augmented assignment is a shortcut that computes and stores back into the same variable, e.g. x += a is the same as x = x + a.

Augmented assignment ย่อการคำนวณแล้วเก็บกลับตัวแปรเดิม เช่น x += a เทียบเท่า x = x + a

Operator Exampleตัวอย่าง Equivalentเทียบเท่า
+=x += ax = x + a
-=x -= ax = x - a
*=x *= ax = x * a
/=x /= ax = x / a
//=x //= ax = x // a
%=x %= ax = x % a
**=x **= ax = x ** a

4.1 Try arithmetic and += 4.1 ทดลองเลขคณิตและ +=

Try // floor division, ** power, and += to increase a variable.

ลอง // หารปัดลง, ** ยกกำลัง และ += เพื่อเพิ่มค่าตัวแปร

[4]
[output]
3
8
7

5 Built-in mathematical functions 5 Built-In Mathematical Functions

Python has several built-in maths functions — each is explained below with a runnable example.

Python มีฟังก์ชันคณิตศาสตร์ในตัวหลายตัว — ด้านล่างอธิบายทีละฟังก์ชันพร้อมตัวอย่างกด Run ได้

5.1 abs()

abs() takes a number (int or float) and returns its absolute value — it drops the minus sign.

abs() รับตัวเลข (int หรือ float) แล้วคืนค่าสัมบูรณ์ (absolute value) — ตัดเครื่องหมายลบออก

[5]
[output]
2024
2024

5.2 int()

int() converts a value to a whole number — for a float it truncates (cuts off) the decimal part, it does not round.

int() แปลงค่าเป็นจำนวนเต็ม — ถ้าเป็น float จะตัดส่วนทศนิยมทิ้ง (ไม่ปัดเศษ)

[6]
[output]
2
2024
3
-3

5.3 float()

float() converts a value to a decimal, e.g. from an int or from a numeric string.

float() แปลงค่าเป็นทศนิยม เช่น จาก int หรือจากสตริงตัวเลข

[7]
[output]
2.0

5.4 max()

max() returns the largest value among several arguments (or from a list).

max() คืนค่าที่มากที่สุดจากอาร์กิวเมนต์หลายตัว (หรือจาก list)

[8]
[output]
4

5.5 min()

min() returns the smallest value among several arguments (or from a list).

min() คืนค่าที่น้อยที่สุดจากอาร์กิวเมนต์หลายตัว (หรือจาก list)

[9]
[output]
-2

5.6 pow()

pow(a, b) returns a to the power b — the same as the ** operator.

pow(a, b) คืนค่า a ยกกำลัง b — เทียบเท่าตัวดำเนินการ **

[10]
[output]
4096576
8

5.7 round() — one argument 5.7 round() — อาร์กิวเมนต์เดียว

round(x) rounds x to the nearest whole number.

round(x) ปัด x เป็นจำนวนเต็มที่ใกล้ที่สุด

[11]
[output]
2
-2

5.8 round() — two arguments 5.8 round() — สองอาร์กิวเมนต์

round(x, n) rounds x to n decimal places; the result is a float.

round(x, n) ปัด x ให้เหลือทศนิยม n ตำแหน่ง ผลลัพธ์เป็น float

[12]
[output]
2.02
-2.02

5.9 range() — one argument 5.9 range() — หนึ่งอาร์กิวเมนต์

range(n) makes a sequence of whole numbers from 0 to n - 1 (not including n). Use list() to turn it into a list so print shows the values clearly.

range(n) สร้างลำดับจำนวนเต็มตั้งแต่ 0 ถึง n - 1 (ไม่รวม n) ใช้ list() แปลงเป็น list เพื่อให้ print แสดงค่าได้ชัด

[13]
[output]
[0, 1, 2]

5.10 range() — two arguments 5.10 range() — สองอาร์กิวเมนต์

range(start, stop) starts at start and goes up to just before stop.

range(start, stop) เริ่มที่ start ไปจนถึงก่อน stop

[14]
[output]
[4, 5, 6, 7]

5.11 range() — three arguments 5.11 range() — สามอาร์กิวเมนต์

range(start, stop, step) counts up by step from start to just before stop.

range(start, stop, step) เพิ่มทีละ step จาก start จนถึงก่อน stop

[15]
[output]
[3, 7, 11, 15, 19]

Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab

References References / แหล่งอ้างอิง

Sources used for this lesson:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. Numeric types — Built-in Types — Numeric types (int / float and arithmetic) (int / float และการคำนวณ)
  2. Comparisons — Built-in Types — Comparisons (== != > < >= <=)
  3. Built-in functions — Built-in Functions (abs int float max min pow round range)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What type is 3.14 in Python?3.14 ใน Python เป็นชนิดข้อมูลใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) floatfloat

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) intint
    Wrong: int is a whole number with no decimal point.ผิด: int คือจำนวนเต็มไม่มีจุดทศนิยม
  • (B) floatfloat
    Correct! A number with a decimal point is a float.ถูกต้อง! ตัวเลขที่มีจุดทศนิยมเป็น float
  • (C) boolbool
    Wrong: bool is True / False.ผิด: bool คือ True / False
  • (D) strstr
    Wrong: str is text in quotation marks.ผิด: str คือข้อความในเครื่องหมายคำพูด

ข้อ 2

2 / 10

What is the result of 7 // 2?ผลลัพธ์ของ 7 // 2 คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 33

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 3.53.5
    Wrong: 3.5 comes from / normal division, not //.ผิด: 3.5 ได้จาก / หารธรรมดา ไม่ใช่ //
  • (B) 44
    Wrong: // rounds down, not up.ผิด: // หารแล้วปัดลง ไม่ปัดขึ้น
  • (C) 33
    Correct! // is floor division, giving 3.ถูกต้อง! // คือ floor division ได้ 3
  • (D) 11
    Wrong: 1 is the remainder 7 % 2, not the result of //.ผิด: 1 คือเศษจาก 7 % 2 ไม่ใช่ผลของ //

ข้อ 3

3 / 10

x += 2 is the same as which?x += 2 เทียบเท่ากับข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) x = x + 2x = x + 2

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) x = 2x = 2
    Wrong: that just sets x to 2 without adding the old value.ผิด: นั่นคือกำหนดค่าใหม่เป็น 2 โดยไม่บวกของเดิม
  • (B) x = x - 2x = x - 2
    Wrong: that is -=, not +=.ผิด: นั่นคือ -= ไม่ใช่ +=
  • (C) x = x * 2x = x * 2
    Wrong: that is *=.ผิด: นั่นคือ *=
  • (D) x = x + 2x = x + 2
    Correct! += is the augmented assignment for addition.ถูกต้อง! += คือ augmented assignment ของการบวก

ข้อ 4

4 / 10

What is the result of abs(-5)?ผลลัพธ์ของ abs(-5) คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 55

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 55
    Correct! abs() returns the absolute value, dropping the minus sign.ถูกต้อง! abs() คืนค่าสัมบูรณ์ ตัดเครื่องหมายลบ
  • (B) -5-5
    Wrong: abs() does not keep the minus sign.ผิด: abs() ไม่คงเครื่องหมายลบไว้
  • (C) 00
    Wrong: the absolute value of -5 is 5.ผิด: ค่าสัมบูรณ์ของ -5 คือ 5
  • (D) TrueTrue
    Wrong: abs() returns a number, not a boolean.ผิด: abs() คืนตัวเลข ไม่ใช่บูลีน

ข้อ 5

5 / 10

What is the result of list(range(3))?ผลลัพธ์ของ list(range(3)) คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) [0, 1, 2][0, 1, 2]

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) [1, 2, 3][1, 2, 3]
    Wrong: range(3) starts at 0 and does not include 3.ผิด: range(3) เริ่มที่ 0 และไม่รวม 3
  • (B) [0, 1, 2][0, 1, 2]
    Correct! range(3) gives 0, 1, 2.ถูกต้อง! range(3) ได้ 0, 1, 2
  • (C) [0, 1, 2, 3][0, 1, 2, 3]
    Wrong: the stop value is not included in a range.ผิด: ค่า stop ไม่ถูกรวมใน range
  • (D) [3][3]
    Wrong: range(3) does not mean a list containing only 3.ผิด: range(3) ไม่ได้หมายถึงรายการที่มีแค่ 3

ข้อ 6

6 / 10

What type is the result of 7 / 2?ผลลัพธ์ของ 7 / 2 เป็นชนิดใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) float (3.5)float (3.5)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) int, because both operands are intint เพราะทั้งสองตัวเป็น int
    Wrong: / always gives a float.ผิด: / ให้ผลเป็น float เสมอ
  • (B) strstr
    Wrong: dividing numbers does not give a string.ผิด: การหารตัวเลขไม่ได้ผลเป็นสตริง
  • (C) float (3.5)float (3.5)
    Correct! / always returns a float, unlike //.ถูกต้อง! / คืน float เสมอ ต่างจาก //
  • (D) boolbool
    Wrong: the result of division is a number, not a boolean.ผิด: ผลของการหารเป็นตัวเลข ไม่ใช่บูลีน

ข้อ 7

7 / 10

What does int(3.9) give?int(3.9) ให้ผลลัพธ์อะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 3, because the decimal is cut off3 เพราะตัดทศนิยมทิ้ง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 4, rounding up4 เพราะปัดขึ้น
    Wrong: int() truncates the decimal; it does not round.ผิด: int() ตัดทศนิยมทิ้ง ไม่ปัดเศษ
  • (B) 3.93.9
    Wrong: int() returns a whole number with no decimal left.ผิด: int() คืนจำนวนเต็ม ไม่เหลือทศนิยม
  • (C) ErrorError
    Wrong: int(3.9) works fine.ผิด: int(3.9) ทำได้ปกติ
  • (D) 3, because the decimal is cut off3 เพราะตัดทศนิยมทิ้ง
    Correct! int() drops the .9, leaving 3.ถูกต้อง! int() ตัดส่วน .9 ทิ้ง เหลือ 3

ข้อ 8

8 / 10

What is the result of 2 ** 3?ผลลัพธ์ของ 2 ** 3 คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 8 (2 to the power 3)8 (2 ยกกำลัง 3)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 8 (2 to the power 3)8 (2 ยกกำลัง 3)
    Correct! ** is power: 2*2*2 = 8.ถูกต้อง! ** คือยกกำลัง 2*2*2 = 8
  • (B) 6 (2 times 3)6 (2 คูณ 3)
    Wrong: ** is not multiplication.ผิด: ** ไม่ใช่การคูณ
  • (C) 9 (3 to the power 2)9 (3 ยกกำลัง 2)
    Wrong: the base is 2 and the exponent is 3.ผิด: ฐานคือ 2 เลขชี้กำลังคือ 3
  • (D) 2323
    Wrong: that is string joining, not arithmetic.ผิด: นั่นคือการต่อสตริง ไม่ใช่เลขคณิต

ข้อ 9

9 / 10

What is the result of list(range(4, 8))?ผลลัพธ์ของ list(range(4, 8)) คือข้อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) [4, 5, 6, 7][4, 5, 6, 7]

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) [4, 5, 6, 7, 8][4, 5, 6, 7, 8]
    Wrong: the stop value (8) is not included.ผิด: ค่า stop (8) ไม่ถูกรวม
  • (B) [5, 6, 7, 8][5, 6, 7, 8]
    Wrong: it starts at start (4), not 5.ผิด: เริ่มที่ start (4) ไม่ใช่ 5
  • (C) [4, 5, 6, 7][4, 5, 6, 7]
    Correct! It starts at 4 and stops before 8.ถูกต้อง! เริ่มที่ 4 หยุดก่อน 8
  • (D) [0, 1, 2, 3][0, 1, 2, 3]
    Wrong: that is range(4), not range(4, 8).ผิด: นั่นคือ range(4) ไม่ใช่ range(4, 8)

ข้อ 10

10 / 10

Which numbers does range(3, 20, 4) give?range(3, 20, 4) ให้ตัวเลขชุดใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 3, 7, 11, 15, 193, 7, 11, 15, 19

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 3, 4, 5, ... , 193, 4, 5, ... , 19
    Wrong: the step is 4, not 1.ผิด: step คือ 4 ไม่ใช่ 1
  • (B) 3, 7, 11, 15, 19, 233, 7, 11, 15, 19, 23
    Wrong: 23 is past stop (20), so it is not included.ผิด: 23 เกิน stop (20) จึงไม่ถูกรวม
  • (C) 4, 8, 12, 164, 8, 12, 16
    Wrong: it starts at start (3), not 4.ผิด: เริ่มที่ start (3) ไม่ใช่ 4
  • (D) 3, 7, 11, 15, 193, 7, 11, 15, 19
    Correct! Start at 3, step by 4, stop before 20.ถูกต้อง! เริ่มที่ 3 เพิ่มทีละ 4 หยุดก่อน 20