[go: up one dir, main page]

0% found this document useful (0 votes)
42 views3 pages

# Tuples in Python (Brief Explanation)

Tuples in Python are ordered and immutable sequences that can store heterogeneous data types and allow duplicates. They are useful for fixed data, as dictionary keys, and for returning multiple values from functions, being faster and more memory-efficient than lists. Key operations include indexing, slicing, and common methods like concatenation and counting occurrences.

Uploaded by

MUKUL CHAUHAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

# Tuples in Python (Brief Explanation)

Tuples in Python are ordered and immutable sequences that can store heterogeneous data types and allow duplicates. They are useful for fixed data, as dictionary keys, and for returning multiple values from functions, being faster and more memory-efficient than lists. Key operations include indexing, slicing, and common methods like concatenation and counting occurrences.

Uploaded by

MUKUL CHAUHAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

# **Tuples in Python (Brief Explanation)**

### **What is a Tuple?**


A **tuple** is an **ordered, immutable** sequence of elements. Once created, its elements
**cannot be changed, added, or removed**.

### **Key Features:**


✔ **Ordered** – Elements have a fixed position (indexed).
✔ **Immutable** – Cannot be modified after creation.
✔ **Heterogeneous** – Can store different data types.
✔ **Allows duplicates** – Can have repeated values.
✔ **Faster than lists** – More memory-efficient for fixed data.

---

### **1. Creating a Tuple**


```python
# Empty tuple
empty_tuple = ()
empty_tuple = tuple()

# Single-element tuple (requires a trailing comma)


single = (5,) # Correct
not_a_tuple = (5) # Just an integer

# Multiple elements
my_tuple = (1, "hello", 3.14, True)
mixed = (10, [1, 2], {"key": "value"}) # Can contain mutable objects
```

---

### **2. Accessing Elements**


```python
t = ("a", "b", "c", "d")

# Indexing (0-based)
print(t[0]) # "a"
print(t[-1]) # "d" (last element)

# Slicing (returns new tuple)


print(t[1:3]) # ("b", "c")
print(t[::-1]) # ("d", "c", "b", "a") (reverse)
```
---

### **3. Immutability (Cannot Modify)**


```python


t = (1, 2, 3)
t[0] = 99 # TypeError: 'tuple' does not support item assignment

# But if a tuple contains a list (mutable), the list can be modified


t = (1, [2, 3], 4)
t[1][0] = 99 # Valid → (1, [99, 3], 4)
```

---

### **4. Common Operations**


```python
# Concatenation
t1 = (1, 2)
t2 = (3, 4)
combined = t1 + t2 # (1, 2, 3, 4)

# Repetition
repeated = t1 * 3 # (1, 2, 1, 2, 1, 2)

# Membership check
print(2 in t1) # True

# Length
print(len(t1)) # 2

# Count occurrences
t = (1, 2, 2, 3)
print(t.count(2)) # 2

# Find index of element


print(t.index(3)) # 3
```

---


### **5. When to Use Tuples?**


**Fixed data** (e.g., coordinates, database records).
**Dictionary keys** (lists can't be keys, but tuples can).
✅ **Returning multiple values from functions** (faster than lists).
✅ **Protecting data from accidental changes**.
```python
# Example: Returning multiple values
def get_user():
return ("Alice", 25, "USA")

name, age, country = get_user() # Tuple unpacking


```

---

### **6. Tuple vs. List**


| Feature | Tuple (`()`) | List (`[]`) |
|---------|-------------|-------------|
| **Mutability** | Immutable | Mutable |
| **Speed** | Faster | Slower |
| **Memory** | More efficient | Less efficient |
| **Use Case** | Fixed data | Dynamic data |

---

### **Summary**
- Tuples are **ordered, immutable** sequences.
- Useful for **fixed data, dictionary keys, and multiple returns**.
- Faster and more memory-efficient than lists.
- Can contain **any data type**, including mutable objects.

Would you like a deeper dive into any specific aspect? 😊

You might also like