In this post, I’ll be talking about some basic yet useful functions and techniques.
- Sequential-data-related techniques.
- slicing techniques are quite useful as it allows you to literally slice sequential data into however smaller pieces.
>>>a = [1,2,3,4,5,6,7,8,9,10]
>>>a[0::2]
[1,3,5,7,9]
- reverse
>>>a = [1,2,3,4,5,6,7,8,9,10]>>>a[::-1]
[10,9,8,7,6,5,4,3,2,1]>>>a[::-2]
[10,8,6,4,2]>>>list(reversed(a))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
2. Bit-related functions
- all(), any()
>>>listdata1 = [1, 1, 1] #1 is treated as 'True'
>>>listdata2 = [1, 0, 1] #0 is treated as 'False'
>>>all(listdata1)
True>>>all(listdata2)
False>>>any(listdata1)
True>>>any(listdata2)
True
3. string-related module and functions
- MODULE : string
- ord(), chr()
Simply put, ‘ord’ stands for ‘order’ of things in ASCII CODE. ‘chr’ on the other hands, means the symbol(or character) in the code table.
import string
def use_ord():
#Convert capital letters to the corresponding numbers
for i in string.ascii_uppercase:
print(i,'----->',ord(i))
print()
#Convert small letters to the corresponding numbers
for i in string.ascii_lowercase:
print(i, '----->',ord(i))>>>use_ord()
A -----> 65
B -----> 66
C -----> 67
D -----> 68
E -----> 69
F -----> 70
G -----> 71
H -----> 72
I ---...def use_chr():
for i in range(65, 122):
print(i, "-------->",chr(i))>>>use_chr()
65 --------> A
66 --------> B
67 --------> C
68 --------> D
69 --------> E
70 --------> F
71 --------> G
72 --------> H
73 -----...