⚡0 XP🔥0

Ch 9 · Tuples and Dictionaries | टपल और डिक्शनरी

Lesson 9 of 11 ⏱ 3 min read ❓ 5-question quiz ▶ Runs Python

Tuples - creation, operations, immutability, built-in functions; dictionaries - key:value pairs, accessing, adding, updating, traversing and methods, with programs.

Tuples#

Ordered, immutable sequences: T = (1, 2, 3); a single-element tuple needs a comma: (5,). Support indexing, slicing, +, *, in, len, min, max, sum, count, index. Tuple packing/unpacking: a, b = b, a swaps values.

▶ Try it YourselfPython
Ctrl + Enter to run

Dictionaries#

Unordered (insertion-ordered in modern Python) collection of key: value pairs; keys must be unique and immutable. Mutable.

▶ Try it YourselfPython
Ctrl + Enter to run
Method Does
keys(), values(), items() views
get(k, default) safe access
update(d2) merge
pop(k), popitem(), clear() remove
fromkeys(), copy(), setdefault() utilities
हिंदी में समझें

टपल अपरिवर्तनशील होते हैं, जबकि डिक्शनरी कुंजी:मान (key:value) जोड़ों का परिवर्तनशील संग्रह है। डिक्शनरी की कुंजियाँ अद्वितीय और अपरिवर्तनशील होनी चाहिए। get() से न मिलने वाली कुंजी पर भी त्रुटि नहीं आती।

Program - character frequency#

▶ Try it YourselfPython
Ctrl + Enter to run

Exam tips#

  • Difference between list and tuple; counting frequencies with a dictionary.

Quick Quiz

Q1.

A single-element tuple is written as

Q2.

Dictionary keys must be

Q3.

marks.get('X', 0) when 'X' is missing returns

Q4.

Tuples are

Q5.

Method returning key–value pairs is