🚀 Learn Python Tuples with Real Practical Coding! In this post, we will explore tuple creation, accessing, unpacking, concatenation, repetition, nested tuples, and function returns with full coding examples. Let's start!
📌 1. Creating Tuples in Python
Tuples are immutable ordered collections of elements. They can store different types of data.
Create a tuple `numbers` with integers
numbers = (90, 80, 70, 60, 50, 40)
print(f"tuple numbers = {numbers}")
Create a tuple `fruits` with strings
fruits = ("Apple", "Banana", "Orange", "Grape")
print(f"tuple fruits = {fruits}")
Create a `mixed_tuple` with multiple data types
mixed_tuple = (10, 3.14, "Cambodia", True, 555)
print(f"tuple mixed_tuple = {mixed_tuple}")
📌 2. Accessing Elements in a Tuple
Since tuples are indexed, you can retrieve elements using their index positions.
Access elements using index
print(f"fruits[0] = {fruits[0]}") # Output: Apple
print(f"fruits[2] = {fruits[2]}") # Output: Orange
print(f"fruits[-1] = {fruits[-1]}") # Output: Grape (Negative index)
📌 3. Unpacking Tuples into Variables
Tuples allow you to unpack elements into separate variables.
Create a new tuple `my_tuple`
my_tuple = ("Khmer", 999, True)
Unpack items into variables
a, b, c = my_tuple
Print unpacked values
print(f"Original `my_tuple` items: {my_tuple}")
print(f"variable a is: {a}")
print(f"variable b is: {b}")
print(f"variable c is: {c}")
📌 4. Creating a Single-Element Tuple
To define a single-item tuple, add a trailing comma.
Creating a single-item tuple
single_tuple = (777,)
print(f"single_tuple is: {single_tuple}")
📌 5. Concatenating Tuples
Tuples can be combined using the + operator.
Create two tuples
tuple_01 = (111, 222, 333, 444, 555)
tuple_02 = ("AAA", "BBB", "CCC", "DDD", "EEE")
Print original tuples
print(f"tuple_01 is: {tuple_01}")
print(f"tuple_02 is: {tuple_02}")
Concatenate both tuples
concatenated_tuple = tuple_01 + tuple_02
print(f"concatenated_tuple is: {concatenated_tuple}")
📌 6. Repeating Tuples
Tuples can be repeated using the * operator.
Repeat tuple_02 three times
tuple_03 = tuple_02 * 3
print(f"Repetition of tuple_03 is: {tuple_03}")
📌 7. Nested Tuples (Tuple inside a Tuple)
Tuples can contain other tuples.
Create a nested tuple
nested_tuple = (888, "Hello", (1, "Angkor Wat"), "Education for all!")
Print the entire tuple
print(f"Items within nested_tuple: {nested_tuple}")
Accessing a tuple inside another tuple
print(f"Nested index nested_tuple[2]: {nested_tuple[2]}")
📌 8. Returning Multiple Values from a Function Using Tuples
A function can return multiple values as a tuple.
Function returning multiple values as a tuple
def got_tuple_value():
x = 666
y = "Welcome to Cambodia!"
return x, y # Returning values as a tuple
Store function return values in a tuple
result = got_tuple_value()
print(f"result is: {result}")
🚀 Summary
In this practical session, we covered:
✔ Creating tuples
✔ Accessing elements
✔ Unpacking values
✔ Creating single-item tuples
✔ Concatenating tuples
✔ Repeating tuples
✔ Using nested tuples
✔ Returning multiple values from functions
📌 Next Topic: Exploring Sets in Python and Their Unique Properties!
🔔 Found this helpful? LIKE 👍, COMMENT 💬, and SUBSCRIBE 🔔 for more Python tutorials! 🚀
Информация по комментариям в разработке