Table of contents | |
Introduction | |
Lists | |
Tuples | |
Sets | |
Dictionaries | |
Sample Problems |
Python is a versatile programming language that provides several data structures to store and manipulate data efficiently. In this article, we will explore four important data structures: lists, tuples, sets, and dictionaries. Each of these data structures has its unique properties and applications. Understanding their characteristics and use cases will empower you to write more efficient and organized Python code.
Lists are ordered collections that can store elements of different data types. They are mutable, meaning you can modify their contents. Here's an example of creating a list:
fruits = ['apple', 'banana', 'orange']
You can access individual elements of a list using their index. Python uses zero-based indexing, which means the first element has an index of 0. Consider the following code:
print(fruits[0]) # Output: 'apple'
Lists allow you to modify their elements by assigning new values to specific indices. For example:
fruits[1] = 'grape'
print(fruits) # Output: ['apple', 'grape', 'orange']
Tuples are similar to lists but are immutable, meaning their elements cannot be changed once assigned. They are often used to store related pieces of data. Here's an example of creating a tuple:
coordinates = (10, 20)
You can access tuple elements in the same way as lists, using zero-based indexing. For example:
print(coordinates[0]) # Output: 10
Since tuples are immutable, you cannot directly modify their elements. However, you can create a new tuple with modified values. Consider the following code:
new_coordinates = (coordinates[0], 30)
print(new_coordinates) # Output: (10, 30)
Sets are unordered collections of unique elements. They are useful when you want to store distinct values and perform set operations like union, intersection, and difference. Here's an example of creating a set:
fruits = {'apple', 'banana', 'orange'}
You can add elements to a set using the 'add()' method. Similarly, you can remove elements using the 'remove()' or 'discard()' methods. Consider the following code:
fruits.add('grape')
fruits.remove('banana')
print(fruits) # Output: {'apple', 'orange', 'grape'}
Sets support various operations such as union ('|'), intersection ('&'), and difference ('-'). For example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 | set2) # Output: {1, 2, 3, 4, 5}
print(set1 & set2) # Output: {3}
print(set1 - set2) # Output: {1, 2}
Dictionaries store key-value pairs and provide fast access to values based on their keys. They are often used to represent real-world entities and their attributes. Here's an example of creating a dictionary:
student = {'name': 'John', 'age': 20, 'grade': 'A'}
You can access dictionary values using their corresponding keys. For example:
print(student['name']) # Output: 'John'
Dictionaries are mutable, so you can modify the values associated with specific keys. For example:
student['age'] = 21
print(student) # Output: {'name': 'John', 'age': 21, 'grade': 'A'}
Problems 1: Create a list that contains the numbers from 1 to 5 and print the square of each number.
numbers = [1, 2, 3, 4, 5]
squared_numbers = [num ** 2 for num in numbers]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]
Problems 2: Given two tuples t1 = (1, 2, 3) and t2 = (4, 5, 6), concatenate them into a single tuple.
t1 = (1, 2, 3)
t2 = (4, 5, 6)
concatenated_tuple = t1 + t2
print(concatenated_tuple) # Output: (1, 2, 3, 4, 5, 6)
Understanding the applications of lists, tuples, sets, and dictionaries in Python is crucial for writing efficient and organized code. Lists are used for storing ordered collections of elements, tuples for immutable related data, sets for distinct values and set operations, and dictionaries for key-value pairs. By leveraging these data structures, you can tackle a wide range of programming problems effectively.
49 videos|38 docs|18 tests
|
|
Explore Courses for EmSAT Achieve exam
|