7. Kiểu dữ liệu số thực
7.1 Số thực
Ở bài học trước, bạn đã tìm hiểu về số nguyên (int). Số nguyên là những số không có phần thập phân, như 3, 5, -7. Nhưng trong cuộc sống, bạn sẽ bắt gặp các số có phần thập phân. Ví dụ chiều cao của cái cây là 1.65m, khối lượng của bạn mèo là 4.5kg. Đó chính là lúc bạn cần dùng đến kiểu dữ liệu số thực.
Số thực là gì?
Số thực (trong Python gọi là float) là kiểu dữ liệu dùng để lưu trữ các con số có phần thập phân. Khi đó, một số sẽ gồm 2 phần: phần nguyên và phần thập phân.
Trong Python, chúng ta dùng dấu chấm (.) để phân tách phần nguyên và phần thập.
Ví dụ: 3.14; 0.5; -23.15 đều là các số thực.
Với số thực 3.14, chúng ta có: 3 là phần nguyên và 14 là phần thập phân.
Như bạn đã biết, Python sẽ dựa vào giá trị để xác định kiểu dữ liệu. Ví dụ, nếu bạn viết là 5, thì Python sẽ hiểu 5 là số nguyên. Nhưng nếu bạn viết là 5.0, thì Python sẽ hiểu đây là số thực.
Sử dụng số thực trong Python
Để khai báo một số thực, bạn sẽ viết một số dưới dạng số thập phân, ví dụ:
ChieuCao = 1.65 # đây là số thực, đơn vị là mét
Bạn có thể thực hiện các phép toán với số thực, giống như với số nguyên.
Bạn mở cửa sổ lập trình trực tiếp hoặc viết mã vào một tập tin, để thực hiện các phép toán sau:
- Cộng: 1.5 + 2.3 = 3.8
- Trừ: 5.0 - 1.2 = 3.8
- Nhân: 2.5 * 4 = 10.0
- Chia: 10.0 / 2 = 5.0
Kiểm tra kiểu dữ liệu
Bạn sử dụng hàm type() để kiểm tra kiểu dữ liệu:
print(type(3.14)) # Kết quả: <class 'float'>
7.2 Bài tập và câu hỏi
(bạn có thể viết mã trong cửa sổ lập trình trực tiếp của Python - chương trình CMD, IDLE Shell hoặc viết mã vào một tập tin riêng)
Bài tập 7a. Tính diện tích của hồ cá (hình tròn), biết bán kính của hồ cá là 5.5m.
Kết quả mong đợi
94.985 # (đơn vị đo là m2)
Bài tập 7b. Điểm 3 môn của bạn Tèo:
- Toán: 8.5
- Văn: 7.0
- Anh: 9.0
Hãy lập trình để tính điểm trung bình của bạn Tèo.
Kết quả mong đợi
8.2
Bài 7c. Viết chương trình tính chu vi hình chữ nhật. Biết rằng chiều dài là 4.5 mét, chiều rộng là 2.3 mét.
Kết quả mong đợi
13.6 # đơn vị đo là m
Bài tập 7d. Viết chương trình tính lãi ngân hàng đơn giản. Số tiền gửi là 10 000 000 VNĐ, lãi suất một năm là 5% (tức 0.05). Tiền lãi = tiền gửi * lãi suất.
Kết quả mong đợi
500 000 # đơn vị VNĐ
Câu hỏi 7.1 Trong Python, lệnh print(type(3.14))xuất ra kết quả gì?
A. PI
B. type double
C. <class 'float'>
D. <class 'int'>
Câu hỏi 7.2 Trong Python, lệnh print(type(2))xuất ra kết quả gì?
A. int
B. type double
C. <class 'float'>
D. <class 'int'>
7. The Float Data Type
7.1 The Float Type
In the previous lesson, you learned about integers (int). Integers are numbers without a fractional part, such as 3, 5, or -7. However, in real life, you will encounter numbers that have fractional parts. For example, the height of a tree is 1.65m, or the mass of a cat is 4.5kg. This is when you need to use the float data type.
What is a float?
A float (or floating-point number) is a data type used to store numbers that have a fractional part (decimal part). A number, in this case, consists of two parts: the integer part and the fractional part.
In Python, we use a dot (.) to separate the integer part and the fractional part.
Examples: 3.14, 0.5, and -23.15 are all float values.
For the float 3.14, we have: 3 as the integer part and 14 as the fractional part.
As you know, Python determines the data type based on the value. For example, if you write 5, Python will interpret 5 as an integer (int). But if you write 5.0, Python will understand this as a float.
Using floats in Python
To declare a float, you write the number in decimal form. For example:
ChieuCao = 1.65 # this is a float, unit is meters
You can perform arithmetic operations with floats, just like with integers.
Open the interactive programming window or write the code in a file to perform the following operations:
Addition:
1.5 + 2.3
# result: 3.8
Subtraction:
5.0 - 1.2
# result: 3.8
Multiplication:
2.5 * 4
# result: 10.0
Division:
10.0 / 2
# result: 5.0
Checking the Data Type
You use the type() function to check the data type:
print(type(3.14))
# Output: <class 'float'>
7.2 Exercises and Questions
(You can write code in Python’s interactive mode—CMD or IDLE Shell—or write code in a separate file.)
Exercise 7a. Calculate the area of a circular fish pond, given that the radius of the pond is 5.5m. (Use pi approx 3.14)
Expected Output:
94.985 # (unit is m2)
Exercise 7b. Calculate the average score for Mr. Teo's three subjects:
- Math: 8.5
- Literature: 7.0
- English: 9.0
Expected Output:
8.2
Exercise 7c. Write a program to calculate the perimeter of a rectangle, given that the length is 4.5 meters and the width is 2.3 meters.
Expected Output:
13.6 # unit is m
Exercise 7d. Write a program to calculate simple bank interest. The deposit amount is 10,000,000 VND, and the annual interest rate is 5% (i.e., 0.05). Formula: Interest = Deposit amount * Interest rate.
Expected Output:
500000.0 # unit VND
Question 7.1 In Python, what is the output of the command print(type(3.14))?
A. PI
B. type double
C. <class 'float'>
D. <class 'int'>
Question 7.2 In Python, what is the output of the command print(type(2))?
A. int
B. type double
C. <class 'float'>
D. <class 'int'>
-----