Python Data Types: List, Tuple, Dictionary
1. LIST
Definition: A list in Python is an ordered, mutable (changeable) collection of elements. It can store
different data types and elements are enclosed in square brackets [ ].
Syntax:
list_name = [element1, element2, element3, ...]
Example Output
fruits = ['apple', 'banana', 'cherry']
print(fruits) ['apple', 'banana', 'cherry']
numbers = [1, 2, 3, 4]
print(numbers[2]) 3
mixed = [1, 'apple', 3.5]
print(mixed) [1, 'apple', 3.5]
fruits = ['apple', 'banana']
fruits.append('orange')
print(fruits) ['apple', 'banana', 'orange']
nums = [10, 20, 30]
nums[1] = 25
print(nums) [10, 25, 30]
2. TUPLE
Definition: A tuple is an ordered, immutable (unchangeable) collection of elements. It can store
different data types and elements are enclosed in parentheses ( ).
Syntax:
tuple_name = (element1, element2, element3, ...)
Example Output
colors = ('red', 'green', 'blue')
print(colors) ('red', 'green', 'blue')
nums = (1, 2, 3)
print(nums[0]) 1
mixed = (1, 'apple', 3.5)
print(mixed) (1, 'apple', 3.5)
tup = (10, 20, 30, 40)
print(len(tup)) 4
single = (5,)
print(type(single)) <class 'tuple'>
3. DICTIONARY
Definition: A dictionary is an unordered, mutable collection of key-value pairs. Keys must be unique
and immutable, values can be of any type. Dictionaries are enclosed in curly braces { }.
Syntax:
dict_name = {key1: value1, key2: value2, ...}
Example Output
student = {'name': 'John', 'age': 21}
print(student) {'name': 'John', 'age': 21}
student = {'name': 'John', 'age': 21}
print(student['name']) John
student = {'name': 'John', 'age': 21}
student['age'] = 22
print(student) {'name': 'John', 'age': 22}
data = {'a': 1, 'b': 2}
print(data.keys()) dict_keys(['a', 'b'])
data = {'a': 1, 'b': 2}
print(data.values()) dict_values([1, 2])