Kundali IQ

Home › Free notes › Class 11 Computer Science › Tuples and Dictionaries

Tuples and Dictionaries – Class 11 Computer Science notes, important questions and MCQs

NCERT Class 11 Computer Science, chapter 10. Notes, key terms, important questions with answers and MCQs.

हिंदी में पढ़ें

Chapter overview

A tuple is an ordered, immutable sequence of elements enclosed in parentheses. A dictionary is a mutable collection of key-value pairs enclosed in curly braces, in which keys are unique and immutable. Tuples are useful for fixed data and for returning multiple values, while dictionaries allow fast lookup of values by key. Both support useful operations, methods and built-in functions.

Tuples

  • Creating: T = (10, 20, 30); a single-element tuple needs a comma, such as (5,), because (5) is just an integer; tuples support indexing, slicing, concatenation, repetition and membership.
  • Tuples are immutable, so T[0] = 1 raises a TypeError; however, a mutable element inside a tuple, such as a list, can be changed.
  • Tuple methods and functions: count(), index(), len(), min(), max(), sum(), sorted() and tuple(); tuple assignment allows swapping, as in a, b = b, a.

Dictionaries

  • Creating: D = {'roll': 1, 'name': 'Asha'}; D['name'] gives 'Asha'; D['age'] = 16 adds a new pair, and assigning to an existing key updates its value.
  • Keys must be unique and immutable (numbers, strings, tuples); values can be of any type; membership with in checks keys, not values.
  • Methods: keys(), values(), items(), get() (returns None or a default value if the key is absent), update(), pop(), popitem(), clear(), copy(), fromkeys(), setdefault(); del D[key] deletes a pair; dictionaries are used for counting frequencies, as in counting characters in a string.

Key terms

Tuple
an ordered, immutable sequence in parentheses
Dictionary
a mutable collection of unique keys mapped to values
Key
the unique, immutable identifier in a dictionary pair
get()
a dictionary method that returns a value without raising an error for a missing key
Tuple assignment
assigning several values at once, as in a, b = 1, 2

Important questions with answers

Distinguish between a list and a tuple.2 marks

A list is mutable, enclosed in square brackets, and has methods like append() and remove(). A tuple is immutable, enclosed in parentheses, uses less memory, and is suitable for fixed data such as days of the week; tuples can also be dictionary keys, while lists cannot.

Explain the methods keys(), values() and items() with an example.3 marks

For D = {'a': 1, 'b': 2}, D.keys() returns the keys dict_keys(['a', 'b']); D.values() returns the values dict_values([1, 2]); D.items() returns key-value pairs as tuples dict_items([('a', 1), ('b', 2)]), which are useful for looping with for k, v in D.items().

Write a program to count the frequency of each character in a string using a dictionary, and explain it.5 marks

The program uses a dictionary to store each character as a key and its count as the value. s = input('Enter a string: '); freq = {}; for ch in s: if ch in freq: freq[ch] = freq[ch] + 1, else: freq[ch] = 1. Then for k, v in freq.items(): print(k, ':', v). Explanation: an empty dictionary freq is created. The for loop traverses the string character by character. The membership operator in checks whether the character already exists as a key. If it does, its count is increased by 1; otherwise, a new key is added with the value 1. The same logic can be written more briefly using freq[ch] = freq.get(ch, 0) + 1, where get() returns 0 if the key is missing. Finally, items() is used to display each character with its frequency. For the input 'banana', the output is b : 1, a : 3 and n : 2. Dictionaries are ideal for this task because they provide fast access to values by key, and keys are unique, so each character appears only once in the result.

Quick revision

  • Creating and accessing tuples; immutability.
  • Tuple operations, methods and assignment.
  • Creating, accessing and modifying dictionaries.
  • Dictionary methods; counting frequencies.

Exam tips

  • Remember: (5) is an int, (5,) is a tuple.
  • Use D.get(key) to avoid KeyError.

Practice MCQs

  1. Tuples in Python are enclosed in

    1. square brackets []
    2. angle brackets
    3. braces {}
    4. parentheses ()
    Show answer

    Answer: (d) parentheses (). Example: (1, 2, 3).

  2. A dictionary stores data as

    1. key-value pairs
    2. index-value pairs only
    3. only keys
    4. only values
    Show answer

    Answer: (a) key-value pairs. Example: {'a': 1}.

  3. Tuples are

    1. mutable
    2. only numeric
    3. unordered
    4. immutable
    Show answer

    Answer: (d) immutable. Their elements cannot be reassigned.

  4. Which of these creates a tuple with one element

    1. (5,)
    2. (5)
    3. [5]
    4. {5}
    Show answer

    Answer: (a) (5,). The comma makes it a tuple.

  5. For D = {'a': 1, 'b': 2}, D.get('c') returns

    1. 0
    2. None
    3. an error
    4. 'c'
    Show answer

    Answer: (b) None. get() does not raise KeyError.

  6. For D = {'x': 10}, the statement D['x'] = 20

    1. adds a second key 'x'
    2. updates the value of 'x' to 20
    3. raises an error
    4. deletes 'x'
    Show answer

    Answer: (b) updates the value of 'x' to 20. Keys are unique.

  7. Which cannot be a dictionary key

    1. 10
    2. 'name'
    3. (1, 2)
    4. [1, 2]
    Show answer

    Answer: (d) [1, 2]. Lists are mutable and unhashable.

  8. After a, b = 3, 7 and then a, b = b, a, the values are

    1. a = 3, b = 7
    2. a = 7, b = 7
    3. a = 7, b = 3
    4. an error
    Show answer

    Answer: (c) a = 7, b = 3. Tuple assignment swaps the values.

All chapters of Class 11 Computer Science

Written by Kundali IQ from standard, well-established facts to help with revision. This is not an official NCERT publication; always read your textbook too.