KEMBAR78
Data Structures | PDF | Data Management | Algorithms And Data Structures
0% found this document useful (0 votes)
10 views5 pages

Data Structures

The document provides an introduction to basic data structures in Python, including lists, sets, tuples, and dictionaries. It demonstrates how to create, modify, and access elements in these data structures with code examples and their outputs. Key operations such as adding, removing, and combining elements are illustrated for each data structure.

Uploaded by

pratyushsagar31
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)
10 views5 pages

Data Structures

The document provides an introduction to basic data structures in Python, including lists, sets, tuples, and dictionaries. It demonstrates how to create, modify, and access elements in these data structures with code examples and their outputs. Key operations such as adding, removing, and combining elements are illustrated for each data structure.

Uploaded by

pratyushsagar31
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
You are on page 1/ 5

# Name = Raghav Anant Nimje

# PRN = 1262253802
# Div = 23, Batch = 1
# Branch = Electrical and Computer Engineering
# Subject = Python Programming

#AIM: Introduction to Basic Data Structures in Python.

#1. Lists

#Creating Lists
List = [1,2,3,4]
print(List)

#Adding elements to the back of a list


List.append('x')
print(List)

#Adding elements from one list to the back of another list


list = [5,6,7,8]
List.exdend(list)
print(List)

#Adding an element at any index in a list


List.insert(0,0)
print(List)

#Removing elements from lists


List.remove('x')#You pass the element to be removed

#Popping elements from lists


print(List.pop(0))#index is passed
print(List)

Outputs:
[1, 2, 3, 4]
[1, 2, 3, 4, 'x']
[1, 2, 3, 4, 'x', 5, 6, 7, 8]
[0, 1, 2, 3, 4, 'x', 5, 6, 7, 8]
0
[1, 2, 3, 4, 5, 6, 7, 8]

#2. Sets
#Creating a set
Set = {1,2,3,4}
print(Set)

#Adding to sets
Set.add(5)
print(Set)

#Combining two sets


set = {2,4,6,8}
Union_set = Set.union(set)
print(Union_set)

#Common elements of a set


Intersection_set = Set.intersection(set)
print(Intersection_set)

#Set difference
Difference_set = Set.difference(set)
print(Difference_set)
Outputs:
{1, 2, 3, 4}
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6, 8}
{2, 4}
{1, 3, 5}

#3. Tuples
#Creating a Tuple
Tuple = (1,2,3,4,5)
print(Tuple)

#Indexes in a Tuple
print(Tuple[0])
print(Tuple[1:3])
print(Tuple[2:])

#Concatenating Tuples
tuple = (5,6,7,8)
Concatenated_Tuple = Tuple + tuple
print(Concatenated_Tuple)

Outputs:
(1, 2, 3, 4, 5)
1
(2, 3)
(3, 4, 5)
(1, 2, 3, 4, 5, 5, 6, 7, 8)

#4. Dictionaries
#Creating a Dictionary
Dict = {"Raghav":1,"Pratyush":2,"Chinmay":3,"Chaitanya":4}
print(Dict)

#Adding an item to the dictionary


Dict["Rishikesh"] = 5
print(Dict)

#Modifiying and existing key:value pair


Dict['Raghav'] = 0
print(Dict)

#Accessing values through keys


print(Dict['Chaitanya'])
print(Dict["Raghav"])

#Length of the dictionary


print(len(Dict))

#Keys in a dictionary
print(Dict.keys())

#Values in a dictionary
print(Dict.values())

#Key:value pairs in a dictionary


print(Dict.items())

Outputs:
{'Raghav': 1, 'Pratyush': 2, 'Chinmay': 3, 'Chaitanya': 4}
{'Raghav': 1, 'Pratyush': 2, 'Chinmay': 3, 'Chaitanya': 4, 'Rishikesh': 5}
{'Raghav': 0, 'Pratyush': 2, 'Chinmay': 3, 'Chaitanya': 4, 'Rishikesh': 5}
4
0
5
dict_keys(['Raghav', 'Pratyush', 'Chinmay', 'Chaitanya', 'Rishikesh'])
dict_values([0, 2, 3, 4, 5])

dict_items([('Raghav', 0), ('Pratyush', 2), ('Chinmay', 3), ('Chaitanya',


4), ('Rishikesh', 5)])

You might also like