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

Các kiểu dữ liệu chỉ định các kích thước và giá trị khác nhau có thể được lưu trữ trong biến. Ví dụ: Python lưu trữ số, chuỗi và danh sách giá trị bằng các loại dữ liệu khác nhau

Mục lục

  • kiểu dữ liệu str
    • Ví dụ
  • kiểu dữ liệu int
    • Ví dụ
    • Ví dụ
  • Kiểu dữ liệu nổi
    • Ví dụ
  • Kiểu dữ liệu phức tạp
    • Ví dụ
  • Liệt kê kiểu dữ liệu
    • Tạo và thao tác danh sách ví dụ
  • Kiểu dữ liệu Tuple
    • Ví dụ tạo và thao tác Tuple
  • Kiểu dữ liệu chính tả
    • Ví dụ về tạo và thao tác từ điển
  • Đặt loại dữ liệu
    • Ví dụ Tạo và thao tác tập hợp
    • băng giá
  • Kiểu dữ liệu bool
  • kiểu dữ liệu byte
    • mảng phụ
  • Loại dữ liệu phạm vi
  • bộ nhớ xem

Python là một ngôn ngữ được gõ động; . Bất kỳ giá trị nào chúng ta gán cho biến dựa trên kiểu dữ liệu đó sẽ được gán tự động. Ví dụ,

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
5 ở đây Python sẽ lưu biến name dưới dạng kiểu dữ liệu
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
6

Bất kể giá trị nào được lưu trữ trong một biến [đối tượng], một biến có thể là bất kỳ loại nào như int, float, str, list, set, tuple, dict, bool, v.v.

Ngoài ra, Giải quyết

  • Biến Python và kiểu dữ liệu Trắc nghiệm
  • Bài tập Python cơ bản cho người mới bắt đầu

Chủ yếu có bốn loại kiểu dữ liệu cơ bản/nguyên thủy có sẵn trong Python

  • số. int, float và phức hợp
  • Sự nối tiếp. Chuỗi, danh sách và bộ dữ liệu
  • Bộ
  • Từ điển [dict]

Để kiểm tra loại dữ liệu của biến, hãy sử dụng hàm tích hợp

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
7  và
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
8

  • Hàm
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    7 trả về kiểu dữ liệu của biến
  • Hàm isinstance[] kiểm tra xem một đối tượng có thuộc về một lớp cụ thể hay không

Trong bài viết này, chúng ta sẽ tìm hiểu chi tiết các kiểu dữ liệu Python sau

Kiểu dữ liệu Mô tảVí dụ
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
0Để lưu trữ các giá trị số nguyên
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
1
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
2Để lưu trữ các giá trị thập phân
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
3
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
4Để lưu trữ các số phức [phần thực và phần ảo]
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
5strĐể lưu trữ dữ liệu dạng văn bản/chuỗi
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
6boolĐể lưu trữ các giá trị boolean
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
7listĐể lưu trữ một chuỗi dữ liệu có thể thay đổi
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
8tupleĐể lưu trữ chuỗi dữ liệu bất biến
# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
9dictĐể lưu trữ khóa. cặp giá trị
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
50setĐể lưu trữ các giá trị không có thứ tự và không được lập chỉ mục
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
51frozensetĐể lưu trữ phiên bản không thay đổi của tập hợp
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
52rangeĐể tạo một chuỗi số
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
53byteĐể lưu trữ các giá trị byte
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
54Kiểu dữ liệu Python

kiểu dữ liệu str

Trong Python, Chuỗi là một dãy các ký tự được đặt trong dấu nháy đơn hoặc nháy kép. Các ký tự này có thể là bất kỳ thứ gì như chữ cái, số hoặc ký hiệu đặc biệt được đặt trong dấu ngoặc kép. Ví dụ,

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
55 là một chuỗi

Loại chuỗi trong Python được biểu diễn bằng lớp 

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
6

Để làm việc với dữ liệu văn bản hoặc ký tự trong Python, chúng tôi sử dụng Chuỗi. Khi một chuỗi được tạo, chúng ta có thể thực hiện nhiều thao tác trên chuỗi đó, chẳng hạn như tìm kiếm bên trong chuỗi, tạo chuỗi con từ chuỗi đó và tách chuỗi đó

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
2

Ghi chú. Chuỗi là bất biến, tôi. e. , nó không thể thay đổi sau khi được xác định. Bạn cần tạo một bản sao của nó nếu bạn muốn sửa đổi nó. Hành vi không thể thay đổi này được gọi là tính bất biến

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
3

kiểu dữ liệu int

Python sử dụng kiểu dữ liệu int để biểu thị toàn bộ giá trị số nguyên. Ví dụ, chúng ta có thể sử dụng kiểu dữ liệu int để lưu trữ số điểm danh của một học sinh. Kiểu Integer trong Python được biểu diễn bằng lớp

# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
0

Bạn có thể lưu trữ các số nguyên dương và âm có độ dài bất kỳ, chẳng hạn như 235, -758, 235689741

Chúng ta có thể tạo một biến số nguyên bằng hai cách

  1. Gán trực tiếp một giá trị nguyên cho một biến
  2. Sử dụng lớp
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    58

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'

Bạn cũng có thể lưu trữ các giá trị số nguyên khác với cơ số 10, chẳng hạn như

  • Nhị phân [cơ số 2]
  • Bát phân [cơ số 8]
  • Số thập lục phân [cơ số 16]

Ví dụ

# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'

Kiểu dữ liệu nổi

Để biểu diễn giá trị dấu phẩy động hoặc giá trị thập phân, chúng ta có thể sử dụng kiểu dữ liệu float. Ví dụ chúng ta muốn lưu trữ lương có thể sử dụng kiểu float

Kiểu float trong Python được biểu diễn bằng lớp

# decimal int 16 with base 8
# Prefix with zero + letter o
octal_num = 0o20
print[octal_num]  # 16
print[type[octal_num]]  # class 'int'

# decimal int 16 with base 16
# Prefix with zero + letter x
hexadecimal_num = 0x10  # decimal equivalent of 21
print[hexadecimal_num]  # 16
print[type[hexadecimal_num]]  # class 'int'

# decimal int 16 with base 2
# Prefix with zero + letter b
binary_num = 0b10000  # decimal equivalent of 6
print[binary_num]  # 16
print[type[binary_num]]  # class 'int'
2

Chúng ta có thể tạo một biến float bằng hai cách

  1. Gán trực tiếp giá trị float cho một biến
  2. Sử dụng lớp
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    90

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
5

Các giá trị dấu phẩy động có thể được biểu diễn bằng dạng hàm mũ, còn được gọi là ký hiệu khoa học. Lợi ích của việc sử dụng dạng hàm mũ để biểu diễn các giá trị dấu phẩy động là chúng ta có thể biểu diễn các giá trị lớn bằng cách sử dụng ít bộ nhớ hơn

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
9

Kiểu dữ liệu phức tạp

Số phức là một số có phần thực và phần ảo được biểu diễn dưới dạng

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
91 trong đó
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
92 và
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
93 chứa các số nguyên hoặc giá trị dấu phẩy động

Loại phức tạp thường được sử dụng trong các ứng dụng khoa học và ứng dụng kỹ thuật điện. Nếu chúng ta muốn khai báo một giá trị phức tạp, thì chúng ta có thể sử dụng biểu mẫu

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
91. Xem ví dụ sau

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
6

Phần thực của số phức được biểu diễn bằng một giá trị nguyên. Giá trị số nguyên có thể ở dạng thập phân, float, nhị phân hoặc thập lục phân. Nhưng phần ảo chỉ được biểu diễn bằng dạng thập phân. Nếu chúng ta đang cố biểu diễn một phần ảo dưới dạng nhị phân, hex hoặc bát phân, chúng ta sẽ gặp lỗi

Liệt kê kiểu dữ liệu

Danh sách Python là tập hợp có thứ tự [còn được gọi là chuỗi ] của các phần tử. Các phần tử danh sách có thể được truy cập, lặp lại và xóa theo thứ tự chúng được chèn vào thời điểm tạo

Chúng tôi sử dụng kiểu dữ liệu danh sách để biểu diễn các nhóm phần tử dưới dạng một thực thể duy nhất. Ví dụ. Nếu chúng ta muốn lưu tên tất cả các sinh viên, chúng ta có thể sử dụng kiểu ________ 295

  1. Danh sách này có thể chứa dữ liệu thuộc mọi loại dữ liệu, chẳng hạn như 
    # decimal int 16 with base 8
    # Prefix with zero + letter o
    octal_num = 0o20
    print[octal_num]  # 16
    print[type[octal_num]]  # class 'int'
    
    # decimal int 16 with base 16
    # Prefix with zero + letter x
    hexadecimal_num = 0x10  # decimal equivalent of 21
    print[hexadecimal_num]  # 16
    print[type[hexadecimal_num]]  # class 'int'
    
    # decimal int 16 with base 2
    # Prefix with zero + letter b
    binary_num = 0b10000  # decimal equivalent of 6
    print[binary_num]  # 16
    print[type[binary_num]]  # class 'int'
    0,
    # decimal int 16 with base 8
    # Prefix with zero + letter o
    octal_num = 0o20
    print[octal_num]  # 16
    print[type[octal_num]]  # class 'int'
    
    # decimal int 16 with base 16
    # Prefix with zero + letter x
    hexadecimal_num = 0x10  # decimal equivalent of 21
    print[hexadecimal_num]  # 16
    print[type[hexadecimal_num]]  # class 'int'
    
    # decimal int 16 with base 2
    # Prefix with zero + letter b
    binary_num = 0b10000  # decimal equivalent of 6
    print[binary_num]  # 16
    print[type[binary_num]]  # class 'int'
    2,
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    98
  2. Các yếu tố trùng lặp được cho phép trong danh sách
  3. Danh sách có thể thay đổi, nghĩa là chúng ta có thể sửa đổi giá trị của các phần tử danh sách

Chúng ta có thể tạo một danh sách bằng hai cách

  1. Bằng cách đặt các phần tử trong dấu ngoặc vuông
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    99
  2. Sử dụng lớp
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    60

Tạo và thao tác danh sách ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
3

Kiểu dữ liệu Tuple

Tuples là bộ sưu tập có thứ tự các phần tử không thể thay đổi.

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
61 giống với
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
95, ngoại trừ bộ dữ liệu là bất biến có nghĩa là chúng tôi không thể sửa đổi bộ dữ liệu sau khi được tạo

Nói cách khác, chúng ta có thể nói một bộ là phiên bản chỉ đọc của danh sách

Ví dụ. Nếu bạn muốn lưu số thứ tự của học sinh mà bạn không thay đổi, bạn có thể sử dụng kiểu dữ liệu ________ 361

Ghi chú. Tuple duy trì thứ tự chèn và cũng cho phép chúng tôi lưu trữ các phần tử trùng lặp

Chúng ta có thể tạo một tuple bằng hai cách

  1. Bằng cách đặt các phần tử trong dấu ngoặc đơn []
  2. Sử dụng lớp
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    64

Ví dụ tạo và thao tác Tuple

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
8

Tuple là bất biến

Một tuple là bất biến có nghĩa là một khi chúng ta tạo một tuple, chúng ta không thể sửa đổi nó

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
9

Kiểu dữ liệu chính tả

Trong Python, từ điển là tập hợp các giá trị duy nhất không có thứ tự được lưu trữ trong các cặp [Khóa-Giá trị]. Sử dụng kiểu dữ liệu từ điển để lưu trữ dữ liệu dưới dạng cặp khóa-giá trị

Loại từ điển được biểu diễn bằng lớp

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
65. Ví dụ: Nếu bạn muốn lưu trữ tên và số thứ tự của tất cả các sinh viên, thì bạn có thể sử dụng loại
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
65

Trong một từ điển, các khóa trùng lặp không được phép, nhưng giá trị có thể được trùng lặp. Nếu chúng tôi cố gắng chèn một giá trị bằng một khóa trùng lặp, giá trị cũ sẽ được thay thế bằng giá trị mới

Từ điển có một số đặc điểm được liệt kê dưới đây

  1. không đồng nhất [i. e. Các phần tử ,
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    6,
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    95,
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    61] được phép dùng cho cả khóa và giá trị trong từ điển. Nhưng một đối tượng có thể là một khóa trong từ điển nếu nó có thể băm được
  2. Từ điển có thể thay đổi, nghĩa là chúng ta có thể sửa đổi các mục của nó
  3. Từ điển không có thứ tự nên chúng tôi không thể thực hiện lập chỉ mục và cắt

Chúng ta có thể tạo một từ điển bằng hai cách

  1. Bằng cách đặt khóa và giá trị trong dấu ngoặc nhọn
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    30
  2. Sử dụng lớp
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    31

Ví dụ về tạo và thao tác từ điển

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
30

Đặt loại dữ liệu

Trong Python, một tập hợp là một tập hợp các mục dữ liệu không có thứ tự và duy nhất. Nói cách khác, Python Set là một tập hợp các phần tử [Hoặc đối tượng] không chứa phần tử trùng lặp

Trong Python, kiểu dữ liệu Set được sử dụng để biểu thị một nhóm các phần tử duy nhất dưới dạng một thực thể duy nhất. Ví dụ muốn lưu mã số sinh viên ta có thể sử dụng kiểu dữ liệu set

Loại dữ liệu Set trong Python được biểu diễn bằng lớp

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
32

Chúng ta có thể tạo Set bằng hai cách

  1. Bằng cách đặt các giá trị trong dấu ngoặc nhọn
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    30
  2. Sử dụng lớp
    # store int value
    roll_no = 33
    # display roll no
    print["Roll number is:", roll_no]
    # output 33
    print[type[roll_no]]  
    # output class 'int'
    
    # store integer using int[] class
    id = int[25]
    print[id]  # 25
    print[type[id]]  # class 'int'
    34

Kiểu dữ liệu tập hợp có các đặc điểm sau

  1. Nó có thể thay đổi có nghĩa là chúng ta có thể thay đổi các mục đã đặt
  2. Các yếu tố trùng lặp không được phép
  3. Cho phép các phần tử không đồng nhất [giá trị của tất cả các loại dữ liệu]
  4. Thứ tự chèn của các phần tử không được giữ nguyên, vì vậy chúng tôi không thể thực hiện lập chỉ mục trên Tập hợp

Ví dụ Tạo và thao tác tập hợp

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
31

băng giá

Loại dữ liệu 

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
35 được sử dụng để tạo Tập hợp bất biến. Sau khi chúng tôi tạo một 
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
35, thì chúng tôi không thể thực hiện bất kỳ thay đổi nào đối với nó

Sử dụng lớp _______437 để tạo 

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
35

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
32

Ghi chú. Nếu chúng tôi cố gắng thực hiện các thao tác như thêm, xóa trên 

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
35, bạn sẽ gặp lỗi

Kiểu dữ liệu bool

Trong Python, để biểu diễn các giá trị boolean [

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
80 và
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
81], chúng ta sử dụng kiểu dữ liệu
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
82. Các giá trị Boolean được sử dụng để đánh giá giá trị của biểu thức. Ví dụ: khi chúng ta so sánh hai giá trị, biểu thức được đánh giá và Python trả về giá trị boolean
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
80 hoặc
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
81

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
33

kiểu dữ liệu byte

Kiểu dữ liệu

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
85 đại diện cho một nhóm số byte giống như một mảng. Chúng tôi sử dụng hàm tạo
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
86 để tạo loại byte, loại này cũng trả về một đối tượng byte. Byte là bất biến [Không thể thay đổi]

Sử dụng kiểu dữ liệu byte nếu chúng ta muốn xử lý dữ liệu nhị phân như hình ảnh, video và tệp âm thanh

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
34

Tính theo byte, các giá trị được phép là 0 đến 256. Nếu chúng tôi đang cố gắng sử dụng bất kỳ giá trị nào khác, thì chúng tôi sẽ nhận được một

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
87

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
35

mảng phụ

Kiểu dữ liệu

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
88 giống như kiểu
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
85 ngoại trừ
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
88 có thể thay đổi [chúng tôi có thể sửa đổi các phần tử của nó]. Hàm tạo
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
91 trả về một đối tượng
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
88

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
36

Loại dữ liệu phạm vi

Trong Python, Hàm tích hợp 

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
93 dùng để tạo một chuỗi số từ số bắt đầu cho đến số kết thúc. Ví dụ: Nếu chúng ta muốn đại diện cho số cuộn từ 1 đến 20, chúng ta có thể sử dụng loại
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
93. Theo mặc định, nó trả về một đối tượng iterator mà chúng ta có thể lặp lại bằng cách sử dụng
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
95

Ví dụ

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
37

bộ nhớ xem

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
96 được sử dụng để tạo chế độ xem dữ liệu bên trong của một đối tượng mà không cần sao chép nó. Vì vậy, với sự trợ giúp của
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
96, chúng ta có thể xem cùng một dữ liệu ở một chế độ xem khác

Để làm điều này, chúng ta cần một giao thức đệm. Giao thức bộ đệm cung cấp một cách để truy cập dữ liệu bên trong của một đối tượng. Dữ liệu nội bộ này là một mảng bộ nhớ hoặc bộ đệm

Trong Python

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
85 và
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
88 là các đối tượng tích hợp hỗ trợ giao thức bộ đệm. Vì vậy, chúng tôi có thể tạo
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
96 trên các đối tượng đó

Cú pháp để tạo một

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
96

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
38

ví dụ 1. Tạo

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
96 của một đối tượng

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
39

đầu ra

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
0

Trong ví dụ trên, chúng tôi tạo

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
96 của
# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
304 và in đối tượng ở định dạng xem bộ nhớ thay vì đối tượng ban đầu. Nếu chúng ta muốn một đối tượng ở dạng ban đầu, chúng ta cần thu thập đối tượng trong một vùng chứa [như danh sách hoặc bộ dữ liệu]. Chúng ta cũng có thể lặp qua từng phần tử của mảng byte đã cho

ví dụ 2. Tạo và lưu trữ trong vùng chứa

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
1

đầu ra

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
2

Chúng tôi cũng có thể thực hiện lập chỉ mục và cắt trên

# store int value
roll_no = 33
# display roll no
print["Roll number is:", roll_no]
# output 33
print[type[roll_no]]  
# output class 'int'

# store integer using int[] class
id = int[25]
print[id]  # 25
print[type[id]]  # class 'int'
96 giống như đối tượng danh sách và tuple. Vì vậy, nó sẽ trả về giá trị thứ tự cho các ký tự tại chỉ mục đã cho

Ví dụ về kiểu dữ liệu trong Python là gì?

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

Kiểu dữ liệu có nghĩa là gì?

Kiểu dữ liệu, trong lập trình, là phân loại xác định loại giá trị mà một biến có và loại phép toán, quan hệ hoặc logic nào có thể được áp dụng cho nó mà không gây ra . .

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

Python có 5 kiểu dữ liệu tiêu chuẩn. .
Số
Chuỗi
Tuple
Từ điển

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

Python có 4 cấu trúc dữ liệu tích hợp có thể được sử dụng để chứa một tập hợp các đối tượng, đó là danh sách, bộ dữ liệu, bộ và từ điển. Chúng có thể được phân biệt thành loại có thể thay đổi, không thay đổi, loại thiết lập và ánh xạ tương ứng

Chủ Đề