Skip to content
← Computer Science

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.

30

Q&A

16

Concepts

Not available

Exam weightage

Start learning — read this chapter →

Key concepts

Hover a concept to preview it and jump to its most relevant Q&A.

Chapter contents

The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.

10.1

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.

10.1.1

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).

10.1.2

Tuple is Immutable

A tuple is an immutable data type. Immutability means that once a tuple has been created, its elements cannot be changed.

10.2

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:

10.2.1

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.

10.2.2

Repetition

The repetition operation is written with the symbol and repeats the elements of a tuple a given number of times.

10.2.3

Membership

The membership operators test whether a value occurs among a tuple's elements.

10.2.4

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…

10.3

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…

10.4

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…

10.5

Nested Tuples

2 Q

A 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…

10.6

Tuple Handling

3 Q

This 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…

10.7

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…

10.7.1

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 (:).

10.7.2

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.

10.8

Dictionaries are Mutable

In sharp contrast to tuples, dictionaries are mutable — the contents of a dictionary can be changed after it has been created.

10.8.1

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.

10.8.2

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.

10.9

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…

10.9.1

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:

10.10

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.

10.11

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.

10.12

Manipulating Dictionaries

4 Q

Having learnt how to create a dictionary and the methods that operate on it, this section applies those manipulation methods in four complete programs.

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 questions8 questions
  1. 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
  2. Q2Consider the following dictionary stateCapital: stateCapital = {"AndhraPradesh":"Hyderabad", "Bihar":"Patna","Maharashtra":"Mumbai", "Rajast…Free
  3. Q3“Lists and Tuples are ordered”. Explain.Free
  4. Q4With the help of an example show how can you return more than one value from a function.Preview
  5. Q5What advantages do tuples have over lists?Preview
  6. Q6When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.Preview
  7. Q7Prove with the help of an example that the variable is rebuilt in case of immutable data types.Preview
  8. Q8TypeError occurs while statement 2 is running. Give reason. How can it be corrected? >>> tuple1 = (5) #statement 1 >>> len(tuple1) #statemen…Preview
+Show 5 questions5 questions
  1. 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
  2. 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
  3. Q3Write a Python program to find the highest 2 values in a dictionary.Preview
  4. 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
  5. 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 question1 question
  1. Q1For the SMIS System given in Chapter 5, let us do the following: Write a program to take in the roll number, name and percentage of marks fo…Preview
+Show 4 questions4 questions
  1. Q1A bank is a financial institution which is involved in borrowing and lending of money. With advancement in technology, online banking, also…Free
  2. 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
  3. 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
  4. Q4Every mode of transport utilises a reservation system to ensure its smooth and efficient functioning. If you analyse you would find many thi…Preview