Goal¶
understand different data types and their applications
Numeric Data Types¶
# Integer
a = 20
# float
b = 10.23
print(a, b, a+b, type(a), type(b), type(a+b))20 10.23 30.23 <class 'int'> <class 'float'> <class 'float'>
Sequence Types¶
String¶
s = "Hello World"
print(s, type(s))
print(s[0], s[6:11])
print(s.lower(), s.upper(), s.replace("Hello", "Bye"))Hello World <class 'str'>
H World
hello world HELLO WORLD Bye World
List¶
Python Lists are dynamic, we dont need to set size of the List or Array. To access the value in the list by reference[index]. Array index starts from 0 till one less than the Lenght of the Array, Since String is of same type, this appiles to string as well.
List - 1 dimentional¶
we define by variable equals [ values comma seperated], append and remove to add and remove elemets.
# List
# reference type - Dynamic Array
a = []
for i in range(5):
a.append(i*i)
print(a)
## Lenth of List
print('Length of List',len(a))
## Access List Elements
print(a[0], a[1:4])
print('Individual Element Access for first Element',a[0])
## Negative Index
print('Negative Indexing - Access last element',a[-1])
print('Negative Indexing - Access second last element',a[-2])
## Access sub array by unsing index
print('Access sub array',a[1:4])
## Access sub array by unsing negative index
print('Negative Indexing - Access sub array',a[-4:-1])
## Push Element to List
a.append(25)
print('After Append 25 to List',a)
## Remove Element from List
a.remove(4)
print('After Remove 4 from List',a)
[0, 1, 4, 9, 16]
Length of List 5
0 [1, 4, 9]
Individual Element Access for first Element 0
Negative Indexing - Access last element 16
Negative Indexing - Access second last element 9
Access sub array [1, 4, 9]
Negative Indexing - Access sub array [1, 4, 9]
After Append 25 to List [0, 1, 4, 9, 16, 25]
After Remove 4 from List [0, 1, 9, 16, 25]
List - 2d and more¶
we define by variable equals [ (List of values or Lists) comma seperated]
## List 2d
list_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print('2D List:', list_2d)
print('Access first row of 2D List:', list_2d[0])
print('Access element at row 1 and column 2 of 2D List:', list_2d[1][2])
## List 3d
list_3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print('3D List:', list_3d)
print('Access first 2D array of 3D List:', list_3d[0])
print('Access element at 1st 2D array, 2nd row, 1st column of 3D List:', list_3d[0][1][0])
## Range Query in List 2d by selecting first 2 rows and columns frm 1 to 2.
sub_2d = [row[1:3] for row in list_2d[0:2]]
print('Sub 2D List (first 2 rows and columns 1 to 2):', sub_2d)2D List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Access first row of 2D List: [1, 2, 3]
Access element at row 1 and column 2 of 2D List: 6
3D List: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
Access first 2D array of 3D List: [[1, 2], [3, 4]]
Access element at 1st 2D array, 2nd row, 1st column of 3D List: 3
Sub 2D List (first 2 rows and columns 1 to 2): [[2, 3], [5, 6]]
Tuple¶
Non Editable List.
b = ('apple', 'banana', 'cherry')
## Access Tuple Elements
print(b)
print(b[0], b[1:3])
## Range query in Tuple
print('Range query in Tuple',b[0:2])
## Edit Tuple Element - Not Allowed
# b[1] = 'orange' # This will raise an error
print('Tuple after trying to edit (will raise error if uncommented)',b)
('apple', 'banana', 'cherry')
apple ('banana', 'cherry')
Range query in Tuple ('apple', 'banana')
Tuple after trying to edit (will raise error if uncommented) ('apple', 'banana', 'cherry')
Mapping Type ~ Dictionary¶
Dictionary is an unordered collection that stores data in key-value pairs, you put a element by key and you retrive an element by key. the main difference is, when to find an element, in array since we have to iterate all the values for find the index, here we can directly get the value by key in dictionary.
d = { 'a': 1,
'b': 2,
'c': 3
}
## Access Dictionary Elements
print(d)
print('Access element by key a:',d['a'])
print('Access element by key b:',d['b'])
## Add Element to Dictionary
d['d'] = 4
print('After adding new key d:',d)
## Remove Element from Dictionary
del d['b']
print('After removing key b:',d)
{'a': 1, 'b': 2, 'c': 3}
Access element by key a: 1
Access element by key b: 2
After adding new key d: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
After removing key b: {'a': 1, 'c': 3, 'd': 4}
Set Type¶
An unordered collections of unique elements
## Set Type
set_a = {1, 2, 3, 4, 5}
print('Set:', set_a)
## Add Element to Set
set_a.add(6)
print('After adding 6 to Set:', set_a)
## Remove Element from Set
set_a.remove(3)
print('After removing 3 from Set:', set_a)
## Agnostic to duplicates
set_b = {1, 2, 2, 3, 4, 4, 5}
print('Set with duplicates (duplicates ignored):', set_b)
## Set Operations
set_c = {4, 5, 6, 7, 8}
print('Set C:', set_c)
print('Union of Set A and Set C:', set_a.union(set_c))
print('Intersection of Set A and Set C:', set_a.intersection(set_c))
Set: {1, 2, 3, 4, 5}
After adding 6 to Set: {1, 2, 3, 4, 5, 6}
After removing 3 from Set: {1, 2, 4, 5, 6}
Set with duplicates (duplicates ignored): {1, 2, 3, 4, 5}
Set C: {4, 5, 6, 7, 8}
Union of Set A and Set C: {1, 2, 4, 5, 6, 7, 8}
Intersection of Set A and Set C: {4, 5, 6}
Boolean Type¶
represents logical values True or False
x = True
y = False
print('Boolean x:', x, type(x))
print('Boolean y:', y, type(y))
if x and not y:
print('x is True and y is False')
else:
print('Condition not met')
Boolean x: True <class 'bool'>
Boolean y: False <class 'bool'>
x is True and y is False
## ByteArray
byte_arr = bytearray([65, 66, 67, 68])
print('ByteArray:', byte_arr)
print('Access first element of ByteArray:', byte_arr[0])
byte_arr[1] = 90
print('After modifying second element of ByteArray:', byte_arr) ByteArray: bytearray(b'ABCD')
Access first element of ByteArray: 65
After modifying second element of ByteArray: bytearray(b'AZCD')
bytes¶
## bytes
byte_seq = bytes([65, 66, 67, 68])
print('Bytes:', byte_seq)
print('Access first element of Bytes:', byte_seq[0])
# byte_seq[1] = 90 # This will raise an error since bytes are immutable
Bytes: b'ABCD'
Access first element of Bytes: 65
None Type¶
The NoneType represents a null value with the single value None, this will be usefull when we dont want to assign any value to a variable.
## Set None Type
n = None
print('None Type:', n, type(n))