⬅ Previous Topic
Primitive Data TypesNext Topic ⮕
Type Systems - Static vs Dynamic Typing⬅ Previous Topic
Primitive Data TypesNext Topic ⮕
Type Systems - Static vs Dynamic TypingComplex data types are used to store multiple values in a single variable. Unlike primitive data types (like integer, float, or boolean), which hold a single value, complex data types can hold collections of values and offer built-in ways to organize, access, and manipulate them efficiently.
An array (or list) is a collection of elements stored in a sequence. Each item can be accessed by its index.
// Define a list of numbers
numbers = [10, 20, 30, 40]
// Access elements
print(numbers[0]) // First element
print(numbers[3]) // Last element
// Modify elements
numbers[1] = 25
// Add a new element at the end
append(numbers, 50)
// Loop through the list
for num in numbers:
print(num)
Output:
10 40 10 25 30 40 50
What happens if you try to access an index that doesn’t exist?
You get an error or "out of bounds" exception depending on the programming environment. Always ensure the index is valid (between 0 and length - 1).
A set is a collection of unique, unordered elements. Useful for checking membership and removing duplicates.
// Define a set
unique_numbers = set(10, 20, 20, 30, 10)
// Add elements
add(unique_numbers, 40)
// Check membership
if 20 in unique_numbers:
print("20 is present")
// Loop through set
for item in unique_numbers:
print(item)
Output:
20 is present 10 20 30 40
Why might you use a set instead of a list?
If you only care about unique items and fast membership checking, sets are more efficient than lists.
Dictionaries (or maps) store data as key-value pairs. Each key is unique, and you use keys to access corresponding values.
// Create a dictionary to store student scores
scores = {
"Alice": 90,
"Bob": 85,
"Charlie": 92
}
// Access a value by key
print(scores["Alice"])
// Add a new key-value pair
scores["David"] = 88
// Modify a value
scores["Bob"] = 87
// Loop through dictionary
for key in scores:
print(key, scores[key])
Output:
90 Alice 90 Bob 87 Charlie 92 David 88
What is a good real-world use case for a dictionary?
Storing student grades, user profiles (name, email, age), or configuration settings where you retrieve data using descriptive keys.
Type | Ordered? | Allows Duplicates? | Access Method | Common Use |
---|---|---|---|---|
List / Array | Yes | Yes | By index | Storing sequence of items |
Set | No | No | Membership test | Unique items, filtering |
Dictionary / Map | No | Keys: No, Values: Yes | By key | Key-value storage |
// Practice Task:
// Create a dictionary with city names as keys and population as values.
// Print all cities with a population over 1 million.
cities = {
"Metropolis": 1500000,
"Smallville": 450000,
"Star City": 1200000
}
for city in cities:
if cities[city] > 1000000:
print(city)
Output:
Metropolis Star City
⬅ Previous Topic
Primitive Data TypesNext Topic ⮕
Type Systems - Static vs Dynamic TypingYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.