Viết chương trình Python để tạo một Tuple với các kiểu dữ liệu khác nhau

Ở bài viết này, chúng ta sẽ cùng đi tìm hiểu về Tuple trong Python, cách sử dụng chúng như thế nào, và sự khác biệt giữa Tuple và List là gì? Tất cả những nội dung trong bài đọc sẽ đều có ví dụ minh hoạ cụ thể, hi vọng các bạn đọc có thể dễ dàng theo dõi cũng như tiếp thu được những kiến thức mà mình mong muốn!

  • Đọc thêm: Tự học lập trình Python trong 6 tháng
  • Đọc thêm: Tài liệu lập trình Python tiếng Việt tốt nhất hiện nay

Mục lục

  • 1. Tuple là gì trong Python?
  • 2.  Một số ưu điểm của Tuple so với List
  • 3. Tạo một Tuple
  • 4. Truy cập các phần tử Tuple
    • Index (chỉ mục) của Tuple
    • Index (chỉ mục) âm
    • Slicing (Cắt lát)
  • 5. Thay đổi phần tử Tuple
  • 6. Xóa phần tử Tuple
  • 7. Phương thức dùng với Tuple trong Python
  • 8. Kiểm tra phần tử trong Tuple
  • 9. Lặp qua các phần tử của Tuple trong Python

1. Tuple là gì trong Python?

Tuple trong Python là một kiểu dữ liệu dùng để lưu trữ các đối tượng có thứ tự và bất biến. Cách lưu trữ của nó cũng khá giống với kiểu dữ liệu List. Tuy nhiên, sự khác biệt giữa List và Tuple, đó là List có thể thay đổi (mutability) trong khi Tuple là bất biến (immutability).

2.  Một số ưu điểm của Tuple so với List

Vì Tuple khá giống với List, nên chúng thường được sử dụng trong các case tương tự nhau. Tuy nhiên, có một số lợi thế nhất định mà một Tuple cung cấp tốt hơn so với một List. Dưới đây là một số ưu điểm chính:

  • Chúng ta thường sử dụng tuple cho các kiểu dữ liệu không đồng nhất và sử dụng list cho các kiểu dữ liệu đồng nhất.
  • Vì các tuple là bất biến, nên việc lặp qua các phần tử của tuple nhanh hơn so với list.
  • Tuple chứa các phần tử bất biến có thể được sử dụng làm key cho dictionary. Trong khi đó, list lại không thể làm được điều này.
  • Nếu bạn có dữ liệu không thay đổi, việc triển khai nó dưới dạng tuple sẽ đảm bảo rằng nó sẽ được chống ghi (write-protected).

3. Tạo một Tuple

Tuple thường được sử dụng cho các dữ liệu không cho phép sửa đổi. Chúng được tạo ra bằng cách đặt tất cả các items/elements bên trong dấu ngoặc đơn

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
2 và được phân tách bằng dấu phẩy
my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
3 .

# Different types of tuples

# Empty tuple
my_tuple = ()
print(my_tuple)

# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple)

# tuple with mixed data types
my_tuple = (1, "Hello", 3.4)
print(my_tuple)

# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)

Output:

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
2

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
5

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
6

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
7

Một Tuple cũng có thể được tạo mà không cần sử dụng dấu ngoặc đơn, được gọi là tuple packing.

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog

Output:

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
8

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
9

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
0

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
1

Để viết một tuple chứa một giá trị duy nhất, bạn bắt buộc phải thêm một dấu phẩy đằng sau giá trị đó:

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 

Output:

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
2

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
3

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
3

4. Truy cập các phần tử Tuple

Có nhiều cách khác nhau để chúng ta có thể truy cập các phần tử của một tuple.

Index (chỉ mục) của Tuple

Sử dụng toán tử index

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
5 để truy cập vào một phần tử của tuple. Index bắt đầu từ 0, nên một tuple có 6 phần tử sẽ có index từ 0 đến 5. Truy cập vào phần tử có index khác index của tuple sẽ làm phát sinh lỗi
my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
6. Index phải là một số nguyên, không thể sử dụng float, hay kiểu dữ liệu khác, sẽ tạo lỗi
my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
7.

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4

Output:

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
8

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
9

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
0

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
1

Index (chỉ mục) âm

Python cho phép lập chỉ mục âm cho các chuỗi. Index -1 là phần tử cuối cùng, -2 là phần tử thứ 2 từ cuối cùng lên,…. Nói đơn giản là index âm dùng khi bạn đếm phần tử của chuỗi ngược từ cuối lên đầu.

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])

Output:

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
9

my_tuple = ("hello")
print(type(my_tuple))  # 

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple))  # 

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple))  # 
8

Slicing (Cắt lát)

Chúng ta có thể truy cập một loạt các phần tử trong một tuple trong Python bằng cách sử dụng dấu hai chấm

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
4 của toán tử cắt (Slicing).

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])

Output:

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
5

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
6

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
7

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
8

5. Thay đổi phần tử Tuple

Không giống như List, Tuple không thể thay đổi. Điều này có nghĩa là các phần tử của một tuple không thể thay đổi một khi đã được gán. Nhưng, nếu bản thân phần tử đó là một kiểu dữ liệu có thể thay đổi (như list chẳng hạn) thì các phần tử lồng nhau có thể được thay đổi. Chúng ta cũng có thể gán giá trị khác cho tuple (gọi là gán lại – reassignment).

# Changing tuple values
my_tuple = (4, 2, 3, [6, 5])


# TypeError: 'tuple' object does not support item assignment
# my_tuple[1] = 9

# However, item of mutable element can be changed
my_tuple[3][0] = 9    # Output: (4, 2, 3, [9, 5])
print(my_tuple)

# Tuples can be reassigned
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)

Output:

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
9

# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0])   # 'p' 
print(my_tuple[5])   # 't'

# IndexError: list index out of range
# print(my_tuple[6])

# Index must be an integer
# TypeError: list indices must be integers, not float
# my_tuple[2.0]

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3])       # 's'
print(n_tuple[1][1])       # 4
8

Ngoài ra, chúng ta có thể sử dụng toán tử

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
1 để kết hợp hai tuple, hoặc cũng có thể lặp lại các phần tử trong một tuple với một số lần nhất định bằng cách sử dụng toán tử
# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
2. Cả hai phép toán
# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
1 và
# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
2 đều dẫn đến một tuple mới.

# Concatenation
# Output: (1, 2, 3, 4, 5, 6)
print((1, 2, 3) + (4, 5, 6))

# Repeat
# Output: ('Repeat', 'Repeat', 'Repeat')
print(("Repeat",) * 3)

Output:

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
5

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
6

6. Xóa phần tử Tuple

Như đã thảo luận ở trên, chúng ta không thể thay đổi các phần tử trong một tuple. Điều này có nghĩa là, bạn không thể xóa hoặc loại bỏ các giá trị ra khỏi một tuple mà chỉ có thể xóa cả tuple đi mà thôi.

Để xóa một hay nhiều tuple thì chúng ta sử dụng hàm del. 

# Deleting tuples
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

# can't delete items
# TypeError: 'tuple' object doesn't support item deletion
# del my_tuple[3]

# Can delete an entire tuple
del my_tuple

# NameError: name 'my_tuple' is not defined
print(my_tuple)

Output:

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
7

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
8

# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])

# Output: 'p'
print(my_tuple[-6])
9

7. Phương thức dùng với Tuple trong Python

Rõ ràng, phương thức thêm phần tử và xóa phần tử không thể sử dụng với tuple. Tuy nhiên,  có 2 phương thức sau bạn có thể ghi nhớ để sử dụng:

  • count(x): Đếm số phần tử x trong tuple
  • index(x): Trả về giá trị index của phần tử x đầu tiên mà nó gặp trong tuple

my_tuple = ('a', 'p', 'p', 'l', 'e',)

print(my_tuple.count('p'))  # Output: 2
print(my_tuple.index('l'))  # Output: 3

Output

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
0

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
9

8. Kiểm tra phần tử trong Tuple

Bạn có thể kiểm tra xem một phần tử đã tồn tại trong tuple hay chưa với từ khóa

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
2.

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
0

Output:

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
3

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
4

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
3

9. Lặp qua các phần tử của Tuple trong Python

Sử dụng vòng lặp

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
6 để lặp qua các phần tử trong tuple:

my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a)      # 3
print(b)      # 4.6
print(c)      # dog
1

Output:

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
7

# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')

# elements 2nd to 4th
# Output: ('r', 'o', 'g')
print(my_tuple[1:4])

# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

# elements 8th to end
# Output: ('i', 'z')
print(my_tuple[7:])

# elements beginning to end
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[:])
8


Với nội dung bài viết này, bạn đã phần nào nắm bắt được những nội dung quan trọng về tuple trong Python rồi chứ? Hãy cùng chờ đón những bài viết tiếp theo của Got It về các kiến thức khác trong lập trình nói riêng cũng như công nghệ nói chung nhé!