-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_type.py
132 lines (85 loc) · 1.68 KB
/
data_type.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# declare and initialize integer
a = 5
# declare and initialize float
b = 5.4
# declare and initialize boolean
result = True
# check type of variable
type(a)
# get help
# help('modules')
# help('str')
# help('bool')
# type conversion
a = str(a)
print(a)
print(type(a))
a = float(a)
print(a)
print(type(a))
# ---------- list ------------
# declare list
data = [4, 5, 6]
# declare list
data = list()
data.append(4)
data.remove(5)
data.insert(1, 3)
data.sort()
# pop element of index 0
data.pop(0)
# ---------- Dictionary -------------
user = dict()
# OR
user = {}
user['name'] = 'Bob'
user['address'] = 'USA'
user['profession'] = 'Engineer'
print(user)
user = {'name': 'Bob',
'address': 'USA',
'profession': 'Engineer'}
print(user.keys())
print(user.get('name'))
print(user.values())
print(user.items())
for key, value in user.items():
print(F'{key} : {value}')
# nested dictionary
address = {
'latitude': 1234,
'longitude': 23456
}
# add address of user
user['address'] = address
# update latitude value to 500
user['address']['latitude'] = 500
# ------------ Tuple ----------
token = (1)
token = tuple(1, 'Jhon', 3.5)
print(type(token))
token = (1,)
print(type(token))
key, value = [2, 3]
# swap number
a = 5
b = 6
a, b = b, a
print(a, b)
# right side of tuple can be of any type string, list, tuple
name, domain = '[email protected]'.split('@')
# tuple comparision
if (5, 6, 7) > (1, 2, 4):
print(True)
# access tuple's element by index
values = (5, 6, 7)
print(values[1])
# ----------- set -------------
set_a = set()
set_b = set()
set_a.add(1)
set_a.add(2)
set_b.add(3)
set_b.add(4)
set_a.union(set_b)
# can perform all functions of set