Tuples and Dictionaries Class 11 Notes and Solutions
Explore 'Tuples and Dictionaries' in Class 11 NCERT Computer Science. Understand tuple operations, mutable dictionaries, and real-world applications with AI-powered tools on English Chatterbox.
Renews monthly. Cancel anytime.
Contents
Notes - Tuples and Dictionaries | Class 11 NCERT | Computer Science
Understanding Tuples and Dictionaries: Class 11 Notes
Python's built-in data structures, tuples and dictionaries, are essential parts of the language, offering unique ways to store and manipulate data. Here is an in-depth guide to understanding these concepts for Class 11 students.
Understanding Tuples
Creating Tuples
A tuple is an ordered sequence of elements of different data types, such as integers, floats, strings, lists, or even tuples. Tuples are enclosed in parentheses and the elements are separated by commas.
### Tuple of integers
tuple1 = (1, 2, 3, 4, 5)
### Tuple of mixed data types
tuple2 = ('Economics', 87, 'Accountancy', 89.6)
If there is only a single element in a tuple, it should be followed by a comma. Otherwise, it will be treated as a different data type.
### Incorrect way of assigning a single element tuple
tuple3 = (20)
print(type(tuple3)) # Output: <class 'int'>
### Correct way
tuple3 = (20,)
print(type(tuple3)) # Output: <class 'tuple'>
Accessing Elements in a Tuple
Elements of a tuple can be accessed using indexing and slicing.
tuple1 = (2, 4, 6, 8, 10, 12)
print(tuple1[0]) # Output: 2
print(tuple1[-1]) # Output: 12
Immutable Nature of Tuples
Tuples are immutable, meaning their elements cannot be changed after creation. Attempting to modify a tuple will result in a TypeError
.
tuple1 = (2, 4, 6)
tuple1[1] = 10 # Raises an error
Tuple Operations
Concatenation: We can join tuples using the concatenation operator (
+
).
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print(tuple1 + tuple2) # Output: (1, 2, 3, 4, 5, 6)
Repetition: Use the repetition operator (
*
) to repeat elements.
tuple1 = ('Hello', 'World')
print(tuple1 * 2) # Output: ('Hello', 'World', 'Hello', 'World')
Slicing: Slicing can be used to obtain a range of elements.
tuple1 = (10, 20, 30, 40, 50)
print(tuple1[1:3]) # Output: (20, 30)
Common Tuple Methods and Functions
len(): Returns the number of elements in the tuple.
tuple(): Converts a sequence into a tuple.
count(): Returns the count of a specified element.
index(): Returns the index of the first occurrence of an element.
min(): Returns the smallest element.
max(): Returns the largest element.
sum(): Returns the sum of all elements in a tuple (if they are numbers).
tuple1 = (10, 20, 30, 20)
print(len(tuple1)) # Output: 4
print(tuple1.count(20)) # Output: 2
Tuple Assignment
Tuple assignment allows multiple variables to be assigned values from a tuple simultaneously.
(a, b) = (10, 20)
print(a) # Output: 10
print(b) # Output: 20
Nested Tuples
A tuple inside another tuple is called a nested tuple.
nested_tuple = (1, 2, (3, 4))
print(nested_tuple[2][1]) # Output: 4
Practical Examples Using Tuples
Tuples are efficient and can be used in various scenarios like function returns, swapping values, and more.
Understanding Dictionaries
A dictionary in Python is a collection of key-value pairs where each key is unique.
Creating Dictionaries
Dictionaries are created with curly braces, containing key-value pairs separated by colons.
dict1 = {'Mohan': 95, 'Ram': 89}
print(dict1)
Accessing Items in a Dictionary
Items in a dictionary can be accessed using keys.
dict1 = {'Mohan': 95, 'Ram': 89}
print(dict1['Ram']) # Output: 89
Mutable Nature of Dictionaries
Dictionaries are mutable, allowing modification of values and addition or removal of items.
dict1['Ram'] = 90
dict1['Sita'] = 85
print(dict1)
Dictionary Operations
Membership: Check if a key exists.
print('Ram' in dict1) # Output: True
Traversing a Dictionary
You can traverse a dictionary using a for
loop.
for key, value in dict1.items():
print(key, value)
Common Dictionary Methods and Functions
len(): Number of items in the dictionary.
keys(): Returns a list of all keys.
values(): Returns a list of all values.
items(): Returns a list of key-value pairs.
get(): Returns the value for a specified key.
update(): Adds key-value pairs from another dictionary.
del: Deletes a specified item.
print(dict1.keys())
print(dict1.values())
Dictionary Manipulation
Dictionaries support various manipulations like adding, updating, and deleting elements.
dict1.update({'Ravi': 88})
del dict1['Ram']
print(dict1)
Practical Examples Using Dictionaries
Dictionaries are useful in cases where data needs to be labelled with unique keys, such as student records, configurations, and more.
Summary and Key Takeaways
Tuples are immutable sequences, primarily used for storing heterogeneous data and ensuring data integrity.
Dictionaries are mutable mappings that associate unique keys with values, making them excellent for fast lookups and dynamic data storage.
Additional Resources
This guide provides an extensive overview of tuples and dictionaries, enabling Class 11 students to grasp these fundamental concepts with ease.
Create a Free Account
Sign up to unlock this article and get access to more study resources.
Advanced Study Techniques
This article covers advanced memory techniques, optimal study environments, and subject-specific strategies that top students use...
Create a free account to:
10,000+ students have already joined
Extra Questions - Tuples and Dictionaries | NCERT | Computer Science | Class 11
NCERT Solutions - Tuples and Dictionaries | NCERT | Computer Science | Class 11
Consider the following tuples, tuple1 and tuple2:
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
Find the output of the following statements:
i. print(tuple1.index(45))
ii. print(tuple1.count(45))
iii. print(tuple1 + tuple2)
iv. print(len(tuple2))
v. print(max(tuple1))
vi print(min(tuple1))
vii. print(sum(tuple2))
viii. print(sorted(tuple1)) print(tuple1)
Consider the following tuples, tuple1 and tuple2:
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
Find the output of the following statements:
i. print(tuple1.index(45))
ii. print(tuple1.count(45))
iii. print(tuple1 + tuple2)
iv. print(len(tuple2))
v. print(max(tuple1))
vi print(min(tuple1))
vii. print(sum(tuple2))
viii. print(sorted(tuple1)) print(tuple1)
Let's evaluate each statement one by one for the given tuples:
tuple1 = (23, 1, 45, 67, 45, 9, 55, 45)
tuple2 = (100, 200)
Statement i:
print(tuple1.index(45))
This statement finds the index of the first occurrence of the value
45
intuple1
.Output:
2
Statement ii:
print(tuple1.count(45))
This statement counts the number of times the value
45
appears intuple1
.Output:
3
Statement iii:
print(tuple1 + tuple2)
This statement concatenates
tuple1
andtuple2
.Output:
(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
Statement iv:
print(len(tuple2))
This statement prints the length of
tuple2
.Output:
2
Statement v:
print(max(tuple1))
This statement prints the maximum value in
tuple1
.Output:
67
Statement vi:
print(min(tuple1))
This statement prints the minimum value in
tuple1
.Output:
1
Statement vii:
print(sum(tuple2))
This statement prints the sum of values in
tuple2
.Output:
300
Statement viii:
print(sorted(tuple1)) print(tuple1)
sorted(tuple1)
returns a new list of all items intuple1
in ascending order.tuple1
remains unchanged because tuples are immutable.Output:
[1, 9, 23, 45, 45, 45, 55, 67] (23, 1, 45, 67, 45, 9, 55, 45)
Summary
i.
print(tuple1.index(45))
→2
ii.
print(tuple1.count(45))
→3
iii.
print(tuple1 + tuple2)
→(23, 1, 45, 67, 45, 9, 55, 45, 100, 200)
iv.
print(len(tuple2))
→2
v.
print(max(tuple1))
→67
vi.
print(min(tuple1))
→1
vii.
print(sum(tuple2))
→300
viii.
print(sorted(tuple1)) print(tuple1)
→[1, 9, 23, 45, 45, 45, 55, 67] (23, 1, 45, 67, 45, 9, 55, 45)
Follow-up Questions:
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Write a program to read email IDs of number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email IDs. Print all three tuples at the end of the program. [Hint: You may use the function split()]
Write a program to read email IDs of number of students and store them in a tuple. Create two new tuples, one to store only the usernames from the email IDs and second to store domain names from the email IDs. Print all three tuples at the end of the program. [Hint: You may use the function split()]
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Create a Free Account
Sign up to continue learning
Hi there! What can I help you learn today?
Click here to learn something new with Chatterbox AI Tutor.