EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Python for EmSAT Achieve  >  Applications of List, Tuple, Set and Dictionary in Python

Applications of List, Tuple, Set and Dictionary in Python | Python for EmSAT Achieve PDF Download

Introduction

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

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']

Accessing List Elements

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'

Modifying List Elements

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

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)

Accessing Tuple Elements

You can access tuple elements in the same way as lists, using zero-based indexing. For example:

print(coordinates[0])  # Output: 10

Modifying Tuples

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

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'}

Adding and Removing Elements

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'}

Set Operations

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

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'}

Accessing Dictionary Values

You can access dictionary values using their corresponding keys. For example:

print(student['name'])  # Output: 'John'

Modifying Dictionary Values

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'}

Sample Problems

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)

Conclusion

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.

The document Applications of List, Tuple, Set and Dictionary in Python | Python for EmSAT Achieve is a part of the EmSAT Achieve Course Python for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
49 videos|38 docs|18 tests

Top Courses for EmSAT Achieve

49 videos|38 docs|18 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

practice quizzes

,

Important questions

,

Tuple

,

Objective type Questions

,

Set and Dictionary in Python | Python for EmSAT Achieve

,

Applications of List

,

Tuple

,

Previous Year Questions with Solutions

,

ppt

,

Viva Questions

,

Set and Dictionary in Python | Python for EmSAT Achieve

,

pdf

,

video lectures

,

shortcuts and tricks

,

Extra Questions

,

Summary

,

Tuple

,

MCQs

,

Applications of List

,

Sample Paper

,

Semester Notes

,

Set and Dictionary in Python | Python for EmSAT Achieve

,

past year papers

,

Applications of List

,

study material

,

Free

,

mock tests for examination

;