Feb 01
Feb 01
>>> l
[0, 2, 4, 6, 8, 10]
>>> # Method #1
>>> # iterate a for loop with range as req start = 0, stop =11, step =2
>>> # value ret by range is APPENDED to an empty list
>>> # Method # 2
>>> ## list_2a = [ <loop variable> || <for loop>]
>>> # <for loop> -> for i in range(0, 11, 2) ## Colons are omitted
>>> # <loop variable> -> i
>>> p
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> "123456789"
'123456789'
>>> # create a list of digits and each ele in list should be of type int
>>> q = []
>>> s = "123456789"
>>> s
'123456789'
>>> list(s)
['1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> q
[]
>>> for i in s:
i_int = int(i) # convert each char in str to int
q.append(i_int) # append the converted integer to the list
>>> q
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> # Verify whether each element of list q is int
>>> for j in q:
print(type(j))
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
>>> for j in q:
print(isinstance(j, int))
True
True
True
True
True
True
True
True
True
>>> q.append('21') # a value of type str is appended to demonstrate
usage of isinstance function
>>> q
[1, 2, 3, 4, 5, 6, 7, 8, 9, '21']
>>> for j in q:
print(isinstance(j, int))
True
True
True
True
True
True
True
True
True
False
>>> # because the last ele of q is of type str the isinstance returned False at the end
>>> q.pop() # revert q to it's original value
'21'
>>> q
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> w
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> w_t_c
[True, True, True, True, True, True, True, True, True]
>>> w = []
>>> w_t_c = []
>>> counter = 0
>>> for i in s:
w.append(int(i))
w_t_c.append((isinstance(w[counter], int), w[counter]))
counter = counter + 1
>>> w
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> w_t_c
[(True, 1), (True, 2), (True, 3), (True, 4), (True, 5), (True, 6),
(True, 7), (True, 8), (True, 9)]