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.
Dictionaries#
Unordered (insertion-ordered in modern Python) collection of key: value pairs; keys must be unique and immutable. Mutable.
| 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#
Exam tips#
- Difference between list and tuple; counting frequencies with a dictionary.