0% found this document useful (0 votes)
18 views5 pages

Tuple Dict Sets Python241217 100627

Tuple, dictionary, and sets programming code using Python.

Uploaded by

sumitkrr78
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)
18 views5 pages

Tuple Dict Sets Python241217 100627

Tuple, dictionary, and sets programming code using Python.

Uploaded by

sumitkrr78
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

cbvzii2lg

December 6, 2024

[1]: # creating a tuple


tuple=(1,2,2,2,1,3,2,3)
print(tuple)

(1, 2, 2, 2, 1, 3, 2, 3)

[3]: # tuple unpacking


tuple=(1,2,4,5)
x,y,z,p=tuple
print(x)
print(y)
print(z)
print(p)

1
2
4
5

[5]: # tuple packing


x,y,z=1,2,4
tuple=x,y,z
print(tuple)

(1, 2, 4)

[15]: # accessing element of a tuple


t=(34,34,345,5,66,6,4,6,54)
print(t[3])

[21]: # cancatination
t1=(34,34,345,5,66,6,4,6,54)
t2=(32,4,54,3,53,5,23,43,433)
result=t1+t2
print(result)

(34, 34, 345, 5, 66, 6, 4, 6, 54, 32, 4, 54, 3, 53, 5, 23, 43, 433)

1
[23]: t1=(34,34,345,5,66,6,4,6,54)
result=t1*2
print(result)

(34, 34, 345, 5, 66, 6, 4, 6, 54, 34, 34, 345, 5, 66, 6, 4, 6, 54)

[25]: # creating a set


set={2,3,2,4,234,25,43,2,2,23,4,2,43}
print(set)#this will not print duplicate value as set is a unique collection of␣
↪elements

{2, 3, 4, 23, 25, 234, 43}

[44]: # insert an element in set


set.add(5000)
print(set)

{2, 3, 4, 5000, 150, 23, 25, 234, 50, 500}

[32]: # deleting an element of a set


set={2,3,2,4,234,25,43,2,2,23,4,2,43}
set.remove(43)
print(set)

{2, 3, 4, 23, 25, 234}

[72]: set1={1,2,1,3,2,12,3,2}
set2={321,1,21,2,3,4,4,5,4}
union_set=set1.union(set2)
print("union set",union_set)

inter_set=set1.intersection(set2)
print("intersection_set",inter_set)

sym_diff=set1.symmetric_difference(set2)
print("symmetric difference ",sym_diff)

union set {1, 2, 3, 321, 4, 5, 12, 21}


intersection_set {1, 2, 3}
symmetric difference {321, 4, 5, 21, 12}

[74]: # find length of set


set1={1,2,1,3,2,12,3,2}
print(len(set1))

2
[78]: dict={
"name":["ajit","aman","akash"],
"city":["sonipat","panipat","delhi"],
"rollnbr":[12,20,40],
"salary":[1000,3000,4000]
}
print(dict)

{'name': ['ajit', 'aman', 'akash'], 'city': ['sonipat', 'panipat', 'delhi'],


'rollnbr': [12, 20, 40], 'salary': [1000, 3000, 4000]}

[82]: import pandas as pd


df=pd.DataFrame(dict)
print(df)

name city rollnbr salary


0 ajit sonipat 12 1000
1 aman panipat 20 3000
2 akash delhi 40 4000

[90]: # accessing the elements


print(dict["name"])

['ajit', 'aman', 'akash']

[92]: dict={
"name":["ajit","aman","akash"],
"city":["sonipat","panipat","delhi"],
"rollnbr":[12,20,40],
"salary":[1000,3000,4000]
}
print(dict)

{'name': ['ajit', 'aman', 'akash'], 'city': ['sonipat', 'panipat', 'delhi'],


'rollnbr': [12, 20, 40], 'salary': [1000, 3000, 4000]}

[94]: # adding a new column in a dictionary


dict["age"]=[20,23,12]
print(dict)

{'name': ['ajit', 'aman', 'akash'], 'city': ['sonipat', 'panipat', 'delhi'],


'rollnbr': [12, 20, 40], 'salary': [1000, 3000, 4000], 'age': [20, 23, 12]}

[96]: dict={
"name":"ajit",
"age":21,
"salary":2000,
}

3
print(dict)

{'name': 'ajit', 'age': 21, 'salary': 2000}

[98]: # update any value


dict["age"]=32
print(dict)

{'name': 'ajit', 'age': 32, 'salary': 2000}

[104]: items=dict.values()
print(items)

dict_values(['ajit', 32, 2000])

[106]: # delete a key


dict.pop("age")
print(dict)

{'name': 'ajit', 'salary': 2000}

[108]: dict

[108]: {'name': 'ajit', 'salary': 2000}

[110]: dict={
"name":"ajit",
"age":21,
"salary":2000,
"area":"sector 22",
"gender":"M",
"roll":20,
}
print(dict)

{'name': 'ajit', 'age': 21, 'salary': 2000, 'area': 'sector 22', 'gender': 'M',
'roll': 20}

[112]: if "city" in dict:


print("city column is avalable")
else:
print("column not found")

column not found

[114]: if "age" in dict:


print("city column is avalable")
else:

4
print("column not found")

city column is avalable

[ ]:

You might also like