Tuples and Dictionaries
Tuples and Dictionaries — Study Notes
NCERT-aligned · 17 notes · 3 shown free
10.1 INTRODUCTION TO TUPLES
Explanation10.1 INTRODUCTION TO TUPLES
A tuple is an ordered sequence of elements that can be of different data types such as integer, float, string, list, or even another tuple. Tuples are enclosed in parentheses (round brackets) and elements are separated by commas. Similar to lists and strings, elements of a tuple can be accessed using index values starting from 0. Tuples are particularly useful when you want to store a collection of heterogeneous data items. For example, a tuple can contain a student's name (string), roll number (integer), and marks (float) all together. Unlike lists, tuples are immutable, meaning once created, their elements cannot be changed. This immutability makes tuples faster and safer to use when the data should not be modified. If a tuple contains only a single element, it must be followed by a comma; otherwise, Python treats it as the data type of the element itself, not as a tuple. For example, (20) is treated as an integer, but (20,) is treated as a tuple with one element. Also, a sequence of comma-separated elements without parentheses is treated as a tuple by default. Tuples can also contain mutable elements like lists. Even though the tuple itself is immutable, the mutable elements inside it can be modified. This feature allows for flexible data structures combining immutability and mutability. Examples: - tuple1 = (1, 2, 3, 4, 5) # tuple of integers - tuple2 = ('Economics', 87, 'Accountancy', 89.6) # tuple of mixed data types - tuple3 = (10, 20, 30, [40, 50]) # tuple containing a list - tuple4 = (1, 2, 3, 4, 5, (10, 20)) # nested tuple Tuples are preferred over lists when the data is not supposed to change throughout the program, ensuring data integrity and faster access.
- Tuple is an ordered sequence of elements enclosed in parentheses.
- Elements can be of different data types including integers, floats, strings, lists, or tuples.
- Indexing starts at 0 and elements can be accessed using indices.
- Tuples are immutable; their elements cannot be changed after creation.
- Single element tuples require a trailing comma to be recognized as tuples.
- A sequence without parentheses but with commas is treated as a tuple by default.
- 📌 Tuple: An ordered, immutable sequence of elements.
- 📌 Immutable: Cannot be changed after creation.
- 📌 Indexing: Accessing elements by position starting at 0.
10.1.1 Accessing Elements in a Tuple
Explanation10.1.1 Accessing Elements in a Tuple
Elements in a tuple can be accessed using indexing and slicing, similar to lists and strings. Indexing starts at 0 for the first element and can be positive or negative. Positive indices count from the start (0, 1, 2, ...), while negative indices count from the end (-1, -2, -3, ...). Accessing elements using indices allows retrieval of individual elements. For example, tuple1[0] returns the first element, and tuple1[-1] returns the last element. If an index is out of range, Python raises an IndexError. Slicing allows accessing a range of elements by specifying start and end indices: tuple1[start:end]. The slice includes elements from the start index up to but not including the end index. Omitting start or end defaults to the beginning or end of the tuple respectively. Slicing also supports a step parameter to skip elements. Examples: - tuple1 = (2, 4, 6, 8, 10, 12) - tuple1[0] returns 2 - tuple1[3] returns 8 - tuple1[-1] returns 12 - tuple1[2:7] returns (30, 40, 50, 60, 70) - tuple1[0:len(tuple1):2] returns every second element starting from index 0 - tuple1[::-1] returns the tuple in reverse order Attempting to access an index that does not exist results in an IndexError, ensuring safe access.
- Indexing starts at 0 and can be positive or negative.
- Positive indices count from the start; negative indices count from the end.
- Slicing extracts a range of elements using start:end syntax.
- Omitting start or end in slicing defaults to beginning or end of the tuple.
- Step parameter in slicing allows skipping elements.
- Accessing out-of-range indices raises IndexError.
- 📌 Indexing: Accessing elements by their position.
- 📌 Slicing: Extracting a subset of elements from a sequence.
- 📌 Step: The interval between elements in slicing.
10.1.2 Tuple is Immutable
Explanation10.1.2 Tuple is Immutable
Tuples are immutable data types in Python, meaning that once a tuple is created, its elements cannot be changed, added, or removed. Attempting to assign a new value to an element of a tuple results in a TypeError. Example: - tuple1 = (1, 2, 3, 4, 5)
All 11 Chapters in Computer Science
Computer Science · Class 11