Unlock Python Tuples and Dictionaries: Essential Operations and Tips
This tutorial explains Python tuples and dictionaries, covering their definitions, creation methods, immutable and mutable characteristics, common operations, built‑in functions, and practical code examples that demonstrate indexing, slicing, unpacking, method usage, such as adding, updating, copying, and iterating.
1. Tuple
Python tuples are similar to lists but their elements cannot be modified. Tuples are created with parentheses and elements are separated by commas.
Tuples and lists are very similar.
Like strings, tuples are immutable.
Tuples can store a series of values.
Tuples are often used when a function needs to receive a fixed set of values that should not change.
Examples:
>> t = (1,3,5,'a',(1,))
>>> type(t)
<class 'tuple'>
>>> print(t)
(1, 3, 5, 'a', (1,))Creating an empty tuple: tup1 = () When a tuple contains a single element, a trailing comma is required:
tup1 = (50,)1.1 Tuple Operations
Tuples belong to the sequence type, so they support indexing and slicing.
Tuple values are immutable.
Tuple unpacking:
>> t = (1,2,3)
>>> a,b,c = t
>>> print(t)
(1, 2, 3)
>>> print(a)
1
>>> print(b+c)
5Accessing elements:
>> t = (1,3,5,'a',(1,))
>>> print(t[1])
31.2 Tuple Methods
index() : return the position of a value.
>> t = (1,3,5,6,6,7,7,8)
>>> t.index(3)
1
>>> t.index(8)
7count() : count how many times a value appears.
>> t = (1,3,5,6,6,7,7,8)
>>> t.count(6)
2
>>> t.count(8)
1Common built‑in functions for tuples: len(tuple) – number of elements. max(tuple) – maximum element. min(tuple) – minimum element. tuple(seq) – convert a sequence (e.g., list) to a tuple.
>> tuple1 = (1,2,3)
>>> tuple2 = (2,3,4,5)
>>> len(tuple1)
3
>>> max(tuple1)
3
>>> min(tuple1)
1
>>> lst = [1,3,5,6,7]
>>> tuple(lst)
(1, 3, 5, 6, 7)
>>> lst
[1, 3, 5, 6, 7]2. Dictionary
A dictionary is Python's only mapping type (hash table). It is mutable, but keys must be immutable objects (e.g., strings, numbers, or tuples). Keys are unique; values can be of any type.
Dictionary methods:
keys() values() get() items()Examples:
>> dic = {'a':1,'b':2}
>>> dic
{'a': 1, 'b': 2}
>>> dic.keys()
dict_keys(['a', 'b'])
>>> dic.values()
dict_values([1, 2])
>>> len(dic)
2
>>> dic.get('b')
2
>>> dic['b']
2Modifying a value:
>> dic['a'] = 8
>>> dic
{'a': 8, 'b': 2}Checking key existence:
>> 'b' in dic
True
>>> 'c' in dic
FalseConverting to a list of items:
>> dic.items()
dict_items([('a', 8), ('b', 2)])Copying a dictionary:
>> dic1 = dic.copy()
>>> dic1
{'a': 8, 'b': 2}Removing a key:
>> dic.pop('a')
8
>>> dic
{'b': 2}Updating with another dictionary:
>> dic = {'b':2}
>>> dic1 = {'a':8,'b':2}
>>> dic.update(dic1)
>>> dic
{'b': 2, 'a': 8}Creating dictionaries:
>> dic = {}
>>> dic = dict()
>>> dict(a=1,b=2)
{'a': 1, 'b': 2}
>>> dict([('c',3),('d',4)])
{'c': 3, 'd': 4}
>>> dict.fromkeys('abcd')
{'a': None, 'b': None, 'c': None, 'd': None}Common Python Functions
help()– view help information. dir() – list names in the current scope.
Type conversion: str(), int(), list(), dict(), tuple(). range() – generate a sequence of numbers. enumerate() – iterate with index.
>> for i in range(10):
... print(i)
0
1
2
3
4
5
6
7
8
9 >> for i, v in enumerate([1,3,5]):
... print(i, v)
0 1
1 3
2 5These examples illustrate the basic usage of tuples and dictionaries, essential built‑in functions, and common patterns for data manipulation in Python.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
