Skip to content

Commit 3b1663b

Browse files
committed
real life operations with data types
1 parent 85ecc6b commit 3b1663b

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

Dictionaries.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#store data in key-value pair , key immutable unique, value can be mutable duplicates
2+
3+
#Create
4+
dc = {
5+
1:'ankit',
6+
2:'rahul',
7+
3:'aditi',
8+
4:'anjali'
9+
}
10+
#Read
11+
print(dc[1]) # get value of key
12+
print(dc.keys()) #get all keys
13+
dc.values() # get all values
14+
15+
se ={}
16+
lis = [2,3,4,4,5,4,3,7,8]
17+
18+
fe={}
19+
20+
for x in range(len(lis)):
21+
fe[100+x]= lis[x]
22+
23+
print(fe)
24+
for k , v in fe.items():
25+
print(k,'---',v)
26+
for x in lis:
27+
se[x]=se.get(x,0)+1
28+
29+
30+
31+
print(se)

list.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#Create a list
2+
list = [1,2,4,5,7,8,9]
3+
#Read a list
4+
print(list[2])
5+
6+
print(list[-2])
7+
8+
#update a list
9+
list[0]=3;
10+
11+
# add
12+
list.append('value')
13+
14+
15+
list.insert(2,'Here') # 2 is index and value placed
16+
17+
print(list)
18+
a = list.copy() # copy a list
19+
print(a)
20+
21+
print(list.pop()) # Remove a value from top
22+
list.clear() # Clear a list.
23+
print(list)
24+
#a.remove(11) Remove a value
25+
26+
27+
#del(a); # Completely Delete a list
28+
a.remove('Here')
29+
a.remove('value')
30+
print(len(a))
31+
a.sort() # sort a list.
32+
c = sorted(a) #Create a another list which is sorted.
33+
print(c)
34+
# if any list has mix datatype elements
35+
b = [1,2,3,'m','j',0.2,0.7,8,7]
36+
37+
num = [x for x in b if isinstance(x, (int, float))]
38+
stri = [x for x in b if isinstance(x,str)]
39+
40+
final = num+stri
41+
print(final)
42+
43+
final.reverse()
44+
print(final)
45+
46+
47+

0 commit comments

Comments
 (0)