Loại giá trị trong Python là gì?

Nếu bạn là người mới bắt đầu sử dụng Python, việc làm quen với tất cả các loại dữ liệu khác nhau của nó lúc đầu có thể gây nhầm lẫn. Rốt cuộc, có rất nhiều trong số chúng có sẵn cho bạn bằng ngôn ngữ

Trong bài viết này, tôi sẽ chỉ cho bạn cách lấy kiểu của các cấu trúc dữ liệu khác nhau trong Python bằng cách gán chúng cho một biến, sau đó in kiểu ra bàn điều khiển bằng hàm print[]

Cách in Loại biến trong Python

Để lấy kiểu của một biến trong Python, bạn có thể sử dụng hàm type[] có sẵn

Cú pháp cơ bản trông như thế này

type[variableName]

Trong Python, mọi thứ đều là đối tượng. Vì vậy, khi bạn sử dụng hàm type[] để in loại giá trị được lưu trữ trong một biến ra bàn điều khiển, nó sẽ trả về loại lớp của đối tượng

Chẳng hạn, nếu loại là một chuỗi và bạn sử dụng type[] trên đó, kết quả là bạn sẽ nhận được

name = "freeCodeCamp"

score = 99.99

lessons =  ["RWD", "JavaScript", "Databases", "Python"]

person = {
    "firstName": "John",
    "lastName": "Doe",
    "age": 28
}

langs = ["Python", "JavaScript", "Golang"]

basics = {"HTML", "CSS", "JavaScript"}

2

Để cho bạn thấy hoạt động của hàm type[], tôi sẽ khai báo một số biến và gán cho chúng các kiểu dữ liệu khác nhau trong Python

name = "freeCodeCamp"

score = 99.99

lessons =  ["RWD", "JavaScript", "Databases", "Python"]

person = {
    "firstName": "John",
    "lastName": "Doe",
    "age": 28
}

langs = ["Python", "JavaScript", "Golang"]

basics = {"HTML", "CSS", "JavaScript"}

Sau đó, tôi sẽ in các loại ra bàn điều khiển bằng cách quấn print[] xung quanh một số chuỗi và hàm type[]

print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]

Đây là kết quả đầu ra

# Outputs:
# The variable, name is of type:  
# The variable, score is of type:   
# The variable, lessons is of type:  
# The variable, person is of type:   
# The variable, langs is of type:   
# The variable, basics is of type:    

Suy nghĩ cuối cùng

Hàm type[] là một hàm tích hợp có giá trị của Python mà bạn có thể lấy kiểu dữ liệu của một biến

Nếu bạn là người mới bắt đầu, bạn nên tránh rắc rối với việc nhồi nhét các loại dữ liệu bằng cách sử dụng hàm type[] để in loại biến ra bảng điều khiển. Điều này sẽ giúp bạn tiết kiệm thời gian

Bạn cũng có thể sử dụng hàm type[] để gỡ lỗi vì trong Python, các biến không được khai báo với kiểu dữ liệu. Vậy là hàm type[] đã được tích hợp sẵn trong ngôn ngữ để bạn kiểm tra kiểu dữ liệu của các biến

Cảm ơn bạn đã đọc

QUẢNG CÁO

QUẢNG CÁO

QUẢNG CÁO

Kolade Chris

Nhà phát triển web và nhà văn kỹ thuật tập trung vào các công nghệ giao diện người dùng

Nếu bạn đọc đến đây, hãy tweet cho tác giả để cho họ thấy bạn quan tâm. Tweet một lời cảm ơn

Học cách viết mã miễn phí. Chương trình giảng dạy mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người có được việc làm với tư cách là nhà phát triển. Bắt đầu

Các kiểu dữ liệu tích hợp

Trong lập trình, kiểu dữ liệu là một khái niệm quan trọng

Các biến có thể lưu trữ dữ liệu thuộc các loại khác nhau và các loại khác nhau có thể làm những việc khác nhau

Python có các loại dữ liệu sau được tích hợp sẵn theo mặc định, trong các danh mục này

Dạng văn bản.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
0Các kiểu số.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
1,
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
2,
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
3Các loại trình tự.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
4,
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
5,
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
6 Loại bản đồ.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
7Thiết lập các loại.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
8,
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
9Kiểu Boolean.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
10Các loại nhị phân.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
11,
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
12,
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
13Không có loại.
print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
14

Lấy kiểu dữ liệu

Bạn có thể lấy kiểu dữ liệu của bất kỳ đối tượng nào bằng cách sử dụng hàm

print["The variable, name is of type:", type[name]]
print["The variable, score is of type:", type[score]]
print["The variable, lessons is of type:", type[lessons]]
print["The variable, person is of type:", type[person]]
print["The variable, langs is of type:", type[langs]]
print["The variable, basics is of type:", type[basics]]
15

Đặt loại dữ liệu

Trong Python, kiểu dữ liệu được đặt khi bạn gán giá trị cho một biến

Đặt loại dữ liệu cụ thể

Nếu bạn muốn chỉ định kiểu dữ liệu, bạn có thể sử dụng các hàm tạo sau

Kiểm tra bản thân với các bài tập

Bài tập

Ví dụ mã sau sẽ in kiểu dữ liệu của x, kiểu dữ liệu đó sẽ là gì?


Cung cấp câu trả lời "

bắt đầu bài tập

Để hiểu các giá trị và kiểu Python, hãy sử dụng các giá trị 1, 2 và “Xin chào, Thế giới. ” làm ví dụ

Các giá trị này [1, 2 và “Xin chào, Thế giới. ”] thuộc các loại khác nhau trong lập trình Python. 1, 2 là số nguyên và “Xin chào, Thế giới. ” là một chuỗi, được gọi như vậy vì nó chứa một “chuỗi” các chữ cái. Một lập trình viên Python [và trình thông dịch] có thể xác định các chuỗi đơn giản vì chúng được đặt trong dấu ngoặc kép hoặc dấu ngoặc đơn

Nếu với tư cách là một lập trình viên Python, nếu bạn không chắc giá trị thuộc loại nào, thì trình thông dịch Python có thể cho bạn biết

>>> type['Hello, World!'] #returns 
>>> type[17] #returns

Không có gì đáng ngạc nhiên, các chuỗi ["Xin chào, Thế giới. ”] thuộc kiểu str, số nguyên[1, 2] thuộc kiểu int. Ít rõ ràng hơn, các số có dấu thập phân thuộc về một loại gọi là float, đơn giản vì các loại số này được biểu diễn theo định dạng gọi là dấu phẩy động

>>> type[3.2] #returns 

Vậy còn các giá trị như “723” và “34 thì sao?. 32”?

>>> type['17'] #returns 
>>> type['3.2'] #returns

Chúng là những sợi dây

Bất cứ khi nào bạn nhập một số nguyên lớn, đôi khi bạn có thể muốn sử dụng dấu phẩy để phân tách các chữ số giữa các nhóm ba chữ số, chẳng hạn như trong 45.000.000. Đây không phải là một giá trị số nguyên hợp lệ trong Python, nhưng nó hợp lệ

>>> print[45,000,000] #returns 45 0 0
45 0 0

Chà, có vẻ như đó không phải là điều chúng ta mong đợi chút nào. Python diễn giải 45.000.000 dưới dạng một chuỗi số nguyên được phân tách bằng dấu phẩy, mà nó in ra với khoảng cách giữa

Đây là ví dụ đầu tiên chúng tôi thấy về một lỗi ngữ nghĩa. mã chạy trơn tru mà không tạo ra thông báo lỗi, nhưng nó cũng không làm điều “đúng”

Ở đó bạn có nó. Để tìm hiểu thêm về lập trình Python, hãy xem những cuốn sách được liệt kê bên dưới

  1. Lập trình Python cho người mới bắt đầu
  2. Lập trình Python

Nếu bạn thích câu chuyện này, đừng ngần ngại theo dõi tôi và vỗ tay cho nhiều câu chuyện như thế này

mã hóa hạnh phúc

Thêm nội dung bằng tiếng Anh. io. Đăng ký nhận bản tin hàng tuần miễn phí của chúng tôi. Nhận quyền truy cập độc quyền để viết các cơ hội và lời khuyên trong cộng đồng của chúng tôi Discord

Giá trị trong kiểu dữ liệu là gì?

Một loại dữ liệu là một loại giá trị nếu nó giữ dữ liệu trong cấp phát bộ nhớ của chính nó . Các loại giá trị bao gồm những điều sau đây. Tất cả các loại dữ liệu số. Boolean , Char và Ngày.

Có bao nhiêu loại giá trị trong Python?

Trong ngôn ngữ lập trình như Python, chủ yếu có 4 kiểu dữ liệu. Chuỗi – Đó là tập hợp các ký tự Unicode [chữ cái, số và ký hiệu] mà chúng ta thấy trên bàn phím. Số - Các loại dữ liệu này lưu trữ các giá trị số như số nguyên, số dấu phẩy động và số phức.

4 kiểu dữ liệu trong Python là gì?

Kiểu dữ liệu Python .
Kiểu dữ liệu số Python. Kiểu dữ liệu số Python được sử dụng để chứa các giá trị số như;.
Kiểu dữ liệu chuỗi Python. Xâu là một dãy các ký tự. .
Kiểu dữ liệu danh sách Python. Danh sách là một kiểu dữ liệu linh hoạt độc quyền trong Python. .
Python Tuple. .
Từ điển Python

3 loại biến trong Python là gì?

Các loại biến Python .
Số
Chuỗi
Tuple

Chủ Đề