Tuple
Introduction / Definition
A tuple is a fixed-size ordered collection of values. Its fields can have different types, which makes it useful for representing a small structured record. Tuples are commonly used for grouped results and related attributes. Their fixed structure makes them convenient for destructuring and pattern-based access.
Syntax
(value1, value2)
(name, age)
Detailed Explanation
Tuple can be understood by identifying its purpose, the rules that govern it, and the way it affects program behavior. The concept should be separated from its particular syntax so that learners can recognize the same idea across different programming languages. A useful way to study it is to start with a small example, trace what happens step by step, and then consider the benefits and limitations. This approach builds both conceptual understanding and practical programming skill.
Example
person = ('Asha', 24)
name, age = person
print(name)
print(age)
Output
Asha
24
Important Points / Notes
- Understand the purpose of tuple.
- Separate the concept from its particular syntax.
- Trace the example step by step.
- Consider both benefits and limitations.
- Use the concept only when it improves program clarity or correctness.
Mini Practice Question
Create a tuple containing a student's name, age, and grade.