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'>
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'>
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ความหมาย |
|---|---|---|
== | Equal | equal toเท่ากัน |
!= | Not equal | not equal toไม่เท่ากัน |
> | Greater than | greater thanมากกว่า |
< | Less than | less thanน้อยกว่า |
>= | Greater or equal | greater than or equalมากกว่าหรือเท่ากับ |
<= | Less or equal | less than or equalน้อยกว่าหรือเท่ากับ |
3.1 Try comparing numbers 3.1 ทดลองเปรียบเทียบตัวเลข
Try >, <=, and != with numbers and see the boolean result.
ลองใช้ > <= และ != กับตัวเลข แล้วดูผลเป็นบูลีน
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 += a | x = x + a |
-= | x -= a | x = x - a |
*= | x *= a | x = x * a |
/= | x /= a | x = x / a |
//= | x //= a | x = x // a |
%= | x %= a | x = x % a |
**= | x **= a | x = x ** a |
4.1 Try arithmetic and += 4.1 ทดลองเลขคณิตและ +=
Try // floor division, ** power, and += to increase a variable.
ลอง // หารปัดลง, ** ยกกำลัง และ += เพื่อเพิ่มค่าตัวแปร
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) — ตัดเครื่องหมายลบออก
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 จะตัดส่วนทศนิยมทิ้ง (ไม่ปัดเศษ)
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 หรือจากสตริงตัวเลข
2.0
5.4 max()
max() returns the largest value among several arguments (or from a list).
max() คืนค่าที่มากที่สุดจากอาร์กิวเมนต์หลายตัว (หรือจาก list)
4
5.5 min()
min() returns the smallest value among several arguments (or from a list).
min() คืนค่าที่น้อยที่สุดจากอาร์กิวเมนต์หลายตัว (หรือจาก list)
-2
5.6 pow()
pow(a, b) returns a to the power b — the same as the **
operator.
pow(a, b) คืนค่า a ยกกำลัง b — เทียบเท่าตัวดำเนินการ **
4096576 8
5.7 round() — one argument 5.7 round() — อาร์กิวเมนต์เดียว
round(x) rounds x to the nearest whole number.
round(x) ปัด x เป็นจำนวนเต็มที่ใกล้ที่สุด
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
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 แสดงค่าได้ชัด
[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
[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
[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:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
- Numeric types — Built-in Types — Numeric types (int / float and arithmetic) (int / float และการคำนวณ)
-
Comparisons —
Built-in Types — Comparisons
(
==!=><>=<=) -
Built-in functions —
Built-in Functions
(
absintfloatmaxminpowroundrange)