0% found this document useful (0 votes)
3 views9 pages

Built-In Data Structures in Python CC Odi

The document provides an overview of Python's built-in data structures, which include Lists, Tuples, Sets, and Dictionaries. Each data structure is described with its characteristics, methods, and examples of usage. The document emphasizes the importance of these structures in efficiently storing and organizing data in Python programs.

Uploaded by

dk.kmr23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Built-In Data Structures in Python CC Odi

The document provides an overview of Python's built-in data structures, which include Lists, Tuples, Sets, and Dictionaries. Each data structure is described with its characteristics, methods, and examples of usage. The document emphasizes the importance of these structures in efficiently storing and organizing data in Python programs.

Uploaded by

dk.kmr23
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Built-in

Data Structures
in
Python
The backbone of every Python program that stores
and organizes data efficiently

By Shashank Rayarao
Data Structures

Data structures are ways to store, organize, and manage data


efficiently.
Python has 4 built-in data structures:
[Link]
[Link]
[Link]
[Link]
List
• Lists are used to store multiple values .
1. Ordered
2. Mutable (can be changed)
3. Allows duplicates
Ex:
[‘Apple’ , ’Banana’ , ’watermelon]
[54,25,48,36]
[10, 20, 30, "Python", True]

Creating Lists:
You can create lists using square brackets [ ] or the list() constructor.
Ex:
numbers = [1, 2, 3, 4, 5]
fruits = list(("apple", "banana", "cherry"))

Characteristics of List

• Mutable – means we can change the data after it is created


Ex:
[Apple’ , ’Banana’ , ’watermelon] # if we like to add ‘ Carrot ‘ we can

• Ordered – Same order follows whenever list is created

• Lists allows duplicate values


Ex:
[2,1,2,1,2,1]

• Allows indexing and slicing


Ex:
List1 = [10,20,30,40]
List1[2] # 30
List1[1:3] #[20,30]
List Methods
Method Description
.append() Single value at the end of the list

.extend() To add list of value at the end

.insert() Index based adding the values

. remove() Value Based Removal

.pop() Index Based removal

.clear() Removes all the data and returns empty list

.index() Returns to index number of a value

.sort()
Sort list

Ex : L1 = [1,8,7,2,21,15]

➢ L1. append(55) # L1 = [1,8,7,2,21,15,55]

➢ L1. extend([23,45,67]) # L1 = [1,8,7,2,21,15,55,23,45,67]

➢ L1 .insert(3,100) # L1 = [1,8,7,100,2,21,15,55,23,45,67]

➢ L1 .remove(7) # L1 = [1,8,100,2,21,15,55,23,45,67]

➢ L1. pop() # L1 = [1,8,100,2,21,15,55,23,45]

➢ L1. pop(2) # L1 = [1,8,2,21,15,55,23,45]

➢ [Link]() #[]

➢ L1. index(15) #5

➢ [Link]() # [1,2,7,8,15,21]
Tuple
A Tuple is used to store multiple values.
• Ordered
• Immutable (can not be changed)
• Allows duplicate elements
• Defined using ( )
(10, 20, 30, "Python“)
numbers = (1, 2, 3, 4, 5)

Pro’s:
1. Faster than lists (optimized for read-only data)
2. Data protection — can’t be accidentally changed
3. Ideal for fixed collections (like coordinates or settings)

Indexing and slicing

numbers = (10, 20, 30, 40,10)


print(numbers[0]) # 10
print(numbers[-1]) # 40
print(numbers[1:3]) # (20, 30)

Tuple Methods

Method Description Example

count() Counts the occurrence of a value [Link](10)

index() Finds the index of the value [Link](20)


Set
A Set is used to store multiple values.
• Unordered
• Mutable (can be changed)
• Doesn’t allow duplicates
• Defined using { }
• f = {"apple", "banana", "cherry“}

Set Methods
Ex : set1 = {2,3,4,5,6,7}
➢ [Link](34) #To add single value to set
{2,3,4,5,6,7,34}
➢ [Link]({10,14,15,17}) #To add multiple Values
{2,3,4,5,6,7,34,10,14,15,17}
➢ [Link]() #It removes values randomly
{2,3,4,5,6,7,34,10,14,15}
➢ [Link](15) #Value Based Removal
{2,3,4,5,6,7,34,10,14}

Set Operations
Python sets support mathematical operations

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print(A | B) # Union {1, 2, 3, 4, 5, 6}


print(A & B) # Intersection {3, 4}
print(A - B) # Difference {1, 2}
print(A ^ B) # Symmetric Difference {1, 2, 5, 6}
Dictionaries
Dictionary is used to store multiple values in the form of key and values
Syntax:
dictionary_name = {
key1: value1,
key2: value2,
key3: value3,
...
}

•Keys → must be unique


•Values → can be duplicates
•Mutable

Ex:
student = {"name": "John", "age": 21, "course": "Python"}
print(student)

• Dictionaries help you store data in a structured format.


Perfect for:
JSON data
API responses
User profiles
Configurations
Key-based lookups
• Super fast for searching based on keys
Dictionary Methods
Method Description
values(): Returns all values

keys(): Returns all keys

get(): Returns the values of Specific keys

items() To get the key- value pair

update() Updates dictionary


pop() Key Based removal

• s = {"name": “Rohan", "age": 21}


• [Link]()
• [ “Rohan", 21]
• [Link]()
• ['name', 'age',]

• [Link]("age")
• 21
• [Link]()
• [('name', 'Rohan'), ('age', 21)]
• [Link]({"course": "Python"})
• {'name': 'Rohan', 'age': 21, 'course': 'Python'}
• [Link]("age")
• print(s)
• {'name': 'Rohan', 'course': 'Python'}

You might also like