# 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)])