Python offers two primary sequence data types: lists and tuples. While lists are mutable—allowing you to modify their data by deleting items or appending new ones—tuples are immutable, meaning their data remains constant once created.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Remember: Unlike lists, tuples cannot be modified after creation. This property makes them useful in scenarios where a constant set of values is required.
Creating and Using Tuples
A tuple is defined using parentheses or simply comma-separated values. Below is an example of creating and printing a tuple:append, nor can you assign a new value to an individual element:
Tuple Characteristics
- Immutable Structure: Once a tuple is created, its contents cannot be changed.
- Versatile Storage: Tuples can store elements of different data types including integers, strings, variables, and even other tuples.
Single-Element Tuples
Creating a tuple with a single element requires a trailing comma to distinguish it from a regular parenthesized expression:Comparing Lists and Tuples
While lists offer flexibility through mutability, tuples provide a reliable structure for fixed data. A quick comparison:| Data Type | Mutability | Creation Syntax | Use Case |
|---|---|---|---|
| List | Mutable | Square brackets [] | Data that may change over time |
| Tuple | Immutable | Parentheses () | Fixed data that should not change |