-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.datatypes.py
More file actions
114 lines (90 loc) · 2.21 KB
/
2.datatypes.py
File metadata and controls
114 lines (90 loc) · 2.21 KB
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
# Python Numbers
print('>>>> Python Numbers >>>>')
a = 5
print(a, "is of type", type(a))
a = 2.0
print(a, "is of type", type(a))
a = 1 + 2j
print(a, "is complex number?", isinstance(1 + 2j, complex))
# Python String
print('\n')
print('>>>> Python String >>>>')
s = "This is a string"
print(s)
s = "This is a string 1+3j, 323645 3.56"
print(s)
s = '''A multiline
string'''
print(s)
# String slicing
print("s[0:3] = ", s[0:3])
print("s[5:] = ", s[5:])
print("s[0:8] = ", s[0:8])
print("s[:8] = ", s[:8])
# String is immutable
# s[1] = 'n'
# TypeError: 'str' object does not support item assignment
# Python List
print('\n')
print('>>>> Python List >>>>')
a = [1, 2.2, 'python', 1 + 3j, 'python']
print(a)
# List slicing
b = [5, 10, 15, 20, 25, 30, 35, 40]
print("b[2] = ", b[2])
print("b[0:3] = ", b[0:3])
print("b[5:] = ", b[5:])
# Lists are mutable, meaning, the value of elements of a list can be altered.
a = [1, 2, 3]
a[2] = 4
print(a)
# Python Tuple
print('\n')
print('>>>> Python Tuple >>>>')
t = (5, 'program', 1 + 3j, 1.6)
print("t = ", t)
# Tuple slicing
print("t[1] = ", t[1])
print("t[0:3] = ", t[0:3])
# Tuples are immutable
# t[0] = 10
# TypeError: 'tuple' object does not support item assignment
# Python Set
print('\n')
print('>>>> Python Set >>>>')
a = {5, 2, 3, 1, 4}
print("a = ", a)
print(type(a))
a = {5, 2, 3, 1, 4, 2, 2, 2, 4, 5}
print("a = ", a)
print(type(a))
a = {5, 2, 3, 1, 4, 2, 2, 2, 4, 5, 'A', 'B', 'C'}
print("a = ", a)
print(type(a))
a = {5, 2, "string", 3 + 4j, 3.4}
print("a = ", a)
print(type(a))
a = {1, 2, 3}
# print("a[1] = ", a[1])
# TypeError: 'set' object does not support indexing
# Set slicing
# print("a[1] = ", a[1])
# TypeError: 'set' object does not support indexing
# Python Dictionary
print('\n')
print('>>>> Python Dictionary >>>>')
d = {1: 'value', 'key': 2}
print(type(d))
print("d[1] = ", d[1])
print("d['key'] = ", d['key'])
d = {'key1': 'value1', 'key2': 'value2'}
print(type(d))
print("d['key1] = ", d['key1'])
print("d['key2'] = ", d['key2'])
d['key2'] = 'value3'
print("d : ", d)
# Dictionary slicing
d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4', 55: 66, 77: 88}
print("d : ", d)
# print("d[0:3] = ", d[0:3])
# TypeError: unhashable type: 'slice'