Computer Science · Class 11 Optional
Ch 10Tuples and Dictionaries — Class 11 Computer Science, concept-first.
A tuple is an ordered sequence that can hold elements of different data types — integers, floats, strings, lists, and even other tuples. The elements are written inside parentheses (round brackets) and separated by commas.
Key concepts
Hover a concept to preview it and jump to its most relevant Q&A.
Tuple Manipulation
Think of a tuple as a sealed envelope. You can put things inside it — numbers, strings, even other envelopes — but once you seal it, you cannot change what's inside.
Most relevant Q&A
- Example 10.1: Create tuples of different kinds and display them — tuple1, a tuple of integers; tuple2, a tuple of mixed data types; tuple3,…Preview
- Program 10-1: This is a program to create a nested tuple to store roll number, name and marks of studentsPreview
- Program 10-4: Write a program to input n numbers from the user. Store these numbers in a tuple. Print the maximum and minimum number from th…Preview
- Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames…Free
- Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is present…Free
Chapter contents
The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.
Introduction to Tuples
A tuple is an ordered sequence that can hold elements of different data types — integers, floats, strings, lists, and even other tuples.
Accessing Elements in a Tuple
Elements of a tuple are accessed exactly the way elements of a list or characters of a string are accessed — through indexing (and, as we will see later in the chapter, slicing).
Tuple is Immutable
A tuple is an immutable data type. Immutability means that once a tuple has been created, its elements cannot be changed.
Tuple Operations
Tuples support the same family of sequence operations you have already used with strings and lists. This section groups them into four operations, each covered in its own sub-section:
Concatenation
Concatenation joins tuples together using the + operator. Applying + to two tuples produces a longer tuple containing all the elements of the first followed by all the elements of the second.
Repetition
The repetition operation is written with the symbol and repeats the elements of a tuple a given number of times.
Membership
The membership operators test whether a value occurs among a tuple's elements.
Slicing
Slicing extracts a contiguous part of a tuple, exactly as it does for strings and lists. The general form is tuple[start : stop : step] — the slice begins at index start and runs up to but not includi…
Tuple Methods and Built-in Functions
Python ships with several ready-made functions and methods for working with tuples. Table 10.1 of the textbook collects the commonly used ones; each is explained below with its behaviour and a worked…
Tuple Assignment
Tuple assignment (also called unpacking) is a very useful Python feature: a tuple of variables on the left side of the assignment operator receives, position by position, the corresponding values from…
Nested Tuples
2 QA tuple placed inside another tuple is called a nested tuple. Nesting is the natural way to store a table of records: each inner tuple is one record (one row), and the outer tuple holds all the record…
+−Worked Examples1 question
Tuple Handling
3 QThis section puts tuples to work in three complete programs, each showing a practical tuple idiom: swapping values with tuple assignment, returning multiple values from a function, and growing a tuple…
+−Programs1 question
+−Programs1 question
Introduction to Dictionaries
The dictionary data type belongs to the mapping category of Python types. A dictionary is a mapping between a set of keys and a set of values — instead of numbering its contents by position the way a…
Creating a Dictionary
To create a dictionary, write its items separated by commas and enclosed in curly braces . Each item is a key-value pair with the key and value separated by a colon (:).
Accessing Items in a Dictionary
Sequences — strings, lists and tuples — are accessed by indexing: you ask for the item at a numbered position. Dictionaries work differently.
Dictionaries are Mutable
In sharp contrast to tuples, dictionaries are mutable — the contents of a dictionary can be changed after it has been created.
Adding a new item
Adding a new item to a dictionary is a single assignment: put the new key in square brackets on the left and its value on the right.
Modifying an Existing Item
An existing item of a dictionary is modified by simply overwriting the key-value pair: assign a new value to a key that is already present, and the old value is replaced.
Dictionary Operations
Just as tuples support sequence operations, dictionaries come with operations of their own. The operation the book covers here is membership — testing whether a particular key is present in the dictio…
Membership
The membership operator in checks whether a key is present in the dictionary. It returns True if the key exists, else it returns False:
Traversing a Dictionary
Traversing a dictionary means visiting each of its items one by one — and the tool for that is the for loop. The textbook shows two equivalent methods.
Dictionary Methods and Built-in functions
Python provides many built-in functions and methods for working with dictionaries. Table 10.2 of the textbook collects the commonly used ones; each is explained below with a worked example.
Manipulating Dictionaries
4 QHaving learnt how to create a dictionary and the methods that operate on it, this section applies those manipulation methods in four complete programs.
+−Programs1 question
+−Programs1 question
+−Programs1 question
Summary
- Tuples are immutable sequences — once a tuple is created, its elements cannot be changed. - The elements of a tuple are written inside round brackets, separated by commas.
More questions
18 Q+−Show 8 questionsHide questions8 questions
- Q1Consider the following tuples, tuple1 and tuple2: tuple1 = (23,1,45,67,45,9,55,45) tuple2 = (100,200) Find the output of the following state…Free
- Q2Consider the following dictionary stateCapital: stateCapital = {"AndhraPradesh":"Hyderabad", "Bihar":"Patna","Maharashtra":"Mumbai", "Rajast…Free
- Q3“Lists and Tuples are ordered”. Explain.Free
- Q4With the help of an example show how can you return more than one value from a function.Preview
- Q5What advantages do tuples have over lists?Preview
- Q6When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.Preview
- Q7Prove with the help of an example that the variable is rebuilt in case of immutable data types.Preview
- Q8TypeError occurs while statement 2 is running. Give reason. How can it be corrected? >>> tuple1 = (5) #statement 1 >>> len(tuple1) #statemen…Preview
+−Show 5 questionsHide questions5 questions
- Q1Write a program to read email IDs of n number of students and store them in a tuple. Create two new tuples, one to store only the usernames…Free
- Q2Write a program to input names of n students and store them in a tuple. Also, input a name from the user and find if this student is present…Free
- Q3Write a Python program to find the highest 2 values in a dictionary.Preview
- Q4Write a Python program to create a dictionary from a string. Note: Track the count of the letters from the string. Sample string : 'w3resour…Preview
- Q5Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary as the key-value pair. Perform the fol…Preview
+−Show 1 questionHide questions1 question
+−Show 4 questionsHide questions4 questions
- Q1A bank is a financial institution which is involved in borrowing and lending of money. With advancement in technology, online banking, also…Free
- Q2Participating in a quiz can be fun as it provides a competitive element. Some educational institutes use it as a tool to measure knowledge l…Free
- Q3Our heritage monuments are our assets. They are a reflection of our rich and glorious past and an inspiration for our future. UNESCO has ide…Preview
- Q4Every mode of transport utilises a reservation system to ensure its smooth and efficient functioning. If you analyse you would find many thi…Preview