Saturday, January 8, 2022

LIst in Python Concept Study

list
In [1]:
l=[]
In [2]:
l=[2,'abc',3.0]
l
Out[2]:
[2, 'abc', 3.0]
In [4]:
l[0]=3
In [5]:
l
Out[5]:
[3, 'abc', 3.0]
In [6]:
l[1]
Out[6]:
'abc'
In [1]:
l2=[3,5,[9,8.0],7]
l2[2]
len(l2)
Out[1]:
4
In [3]:
l2[2][0]
Out[3]:
9
In [9]:
l2[3]
Out[9]:
7
In [5]:
l2[3]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-a11279955f9d> in <module>
----> 1 l2[3][0]

TypeError: 'int' object is not subscriptable
In [19]:
l3=list('i am indian')
l3
l4=str(l3)
len(l3)
print(len(l4))
55
In [12]:
eval('5'+'8')
Out[12]:
58
In [13]:
eval('8+9')
Out[13]:
17
In [14]:
eval('3*10')
Out[14]:
30
In [12]:
v=eval(input('enter value or expression'))
print(v,type(v))
enter value or expression[1,2,3]
[1, 2, 3] <class 'list'>
In [20]:
vow="ae iou"
vowel=list(vow)
print(vow,vowel)
ae iou ['a', 'e', ' ', 'i', 'o', 'u']
In [21]:
s=str(vowel)
print(s)
print(len(s),len(vow),len(vowel))
['a', 'e', ' ', 'i', 'o', 'u']
30 6 6
In [29]:
l1,l2=[1,2,3],[1,2,3]
l1==l2
l1!=l2
Out[29]:
False
In [32]:
 
Out[32]:
False
In [32]:
l1=l1+l2
In [33]:
l1
Out[33]:
[1, 2, 3, 1, 2, 3]
In [34]:
l1+3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-16f51a34bda7> in <module>
----> 1 l1+3

TypeError: can only concatenate list (not "int") to list
In [35]:
l1*2
Out[35]:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
In [1]:
eval('5+8')
Out[1]:
13
In [2]:
eval('3*10')
Out[2]:
30
In [3]:
eval('3'*'10')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-9f68aa82b557> in <module>
----> 1 eval('3'*'10')

TypeError: can't multiply sequence by non-int of type 'str'
In [ ]:
v=eval(input('enter value'))
print(v,type(v))
In [ ]:
l1=(4,5,6)
l2=(6,7,8,)
In [ ]:
print(l1>l2)
print(l1<l2)
In [2]:
lst=[10,20,30,40,32,34,40,80,90,23]
se=lst[3:-3]
se
Out[2]:
[40, 32, 34, 40]
In [3]:
se[1]
Out[3]:
32
In [4]:
lst[-12:7]
Out[4]:
[10, 20, 30, 40, 32, 34, 40]
In [5]:
lst[-12:10]
Out[5]:
[10, 20, 30, 40, 32, 34, 40, 80, 90, 23]
In [6]:
lst[-24:24]
Out[6]:
[10, 20, 30, 40, 32, 34, 40, 80, 90, 23]
In [7]:
lst[2:10:3]
Out[7]:
[30, 34, 90]
In [8]:
lst
Out[8]:
[10, 20, 30, 40, 32, 34, 40, 80, 90, 23]
In [9]:
lst[1:3]=[12,34,56]
In [19]:
len(lst)
Out[19]:
12
In [11]:
lst[0:1]=[22,33,44,55,66]
In [14]:
len(lst)
Out[14]:
15
In [13]:
len(lst)
Out[13]:
15
In [17]:
lst[0:5]=[5,5]
In [18]:
lst
Out[18]:
[5, 5, 12, 34, 56, 40, 32, 34, 40, 80, 90, 23]
In [21]:
fst=['i','you','we','they']
fst[0:2]=[45]
In [22]:
fst
Out[22]:
[45, 'we', 'they']
In [23]:
L=[1,2,3]
L[2:]='604'
L
Out[23]:
[1, 2, '6', '0', '4']
In [24]:
L1[2:]=604
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-24-ac453cc80c52> in <module>
----> 1 L1[2:]=604

NameError: name 'L1' is not defined
In [26]:
L1=['a','b','c']
L1[10:20]='xyz'
In [27]:
L1
Out[27]:
['a', 'b', 'c', 'x', 'y', 'z']
In [29]:
L1+='16'
In [31]:
L1*=2
In [32]:
L1
Out[32]:
['a',
 'b',
 'c',
 'x',
 'y',
 'z',
 '1',
 '6',
 'a',
 'b',
 'c',
 'x',
 'y',
 'z',
 '1',
 '6']
In [34]:
L1=['11','22','33','44','55']
L1
Out[34]:
['11', '22', '33', '44', '55']
In [35]:
L1.append(16)
In [36]:
del L1[0]
In [37]:
L1
Out[37]:
['22', '33', '44', '55', 16]
In [38]:
del L1[0:1]
In [39]:
L1
Out[39]:
['33', '44', '55', 16]
In [40]:
del L1[0:2]
In [41]:
L1
Out[41]:
['55', 16]
In [42]:
L1+=[11,12,15,40]
In [1]:
L1
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-5163c2dce851> in <module>
----> 1 L1

NameError: name 'L1' is not defined
In [45]:
L1.pop(-1)
Out[45]:
40
In [46]:
L1
Out[46]:
['55', 16, 11, 15]
In [47]:
L1
Out[47]:
['55', 16, 11, 15]
In [48]:
L2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-48-e2d37c5ab432> in <module>
----> 1 L2

NameError: name 'L2' is not defined
In [49]:
#list copy
c=[1,2,3]
d=c
Out[49]:
[1, 2, 3]
In [55]:
c[1]=9
print(c)
print(d)
#reflect change in both
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-55-d96fed13007c> in <module>
----> 1 c[1]=9
      2 print(c)
      3 print(d)
      4 #reflect change in both

NameError: name 'c' is not defined
In [53]:
#seperate copy list
a=[10,20,30]
b=list(a)
b[1]=40
print(a)
print(b)
[10, 20, 30]
[10, 40, 30]
In [4]:
#copied list reference original List
c=[1,2,3]
d=c
d[1]=9
print(c)
print(d)
[1, 9, 3]
[1, 9, 3]
In [58]:
#index function in List
L1=[13,18,20,18,28,20,35]
print(L1.index(18))
print(L1.index(20))
1
2
In [62]:
#append in List
L2=[]
L2.append(90)
L2.append('ninty')
L3=L2.append('ten')
print(L2)
print(L3)
[90, 'ninty', 'ten']
None
In [2]:
l1=[1,2,3]
l1+=[5,6]
In [3]:
l1
Out[3]:
[1, 2, 3, 5, 6]
In [2]:
t1=[1,2,3]
t2=[4,5,6]
t1.extend(t2)
t1+=t2
t1
Out[2]:
[1, 2, 3, 4, 5, 6, 4, 5, 6]
In [4]:
t1=[3,4,5]
t1.insert(1,9)
t1
Out[4]:
[3, 9, 4, 5]
In [20]:
#index value if more than max index
t1.insert(7,5)
t1
Out[20]:
[5, 5, 4, 4, 3, 3, 2, 5, 5, 2, 1]
In [7]:
#index value if less than min index
t1.insert(-8,88)
t1
Out[7]:
[88, 3, 9, 4, 5, 5]
In [10]:
#it will remove all element from list
t1.clear()
t1
Out[10]:
[]
In [23]:
#count number of item freuency in list
t1=[1,2,2,3,3,3,4,4,5,5]
t1.count(3)
Out[23]:
3
In [12]:
t1.count(2)
Out[12]:
2
In [13]:
t1.count(3)
Out[13]:
2
In [14]:
t1.reverse()
t1
Out[14]:
[5, 5, 4, 4, 3, 3, 2, 2, 1]
In [15]:
t1.sort()
In [16]:
t1
Out[16]:
[1, 2, 2, 3, 3, 4, 4, 5, 5]
In [17]:
t1.sort(reverse=True)
In [18]:
t1
Out[18]:
[5, 5, 4, 4, 3, 3, 2, 2, 1]
In [21]:
t1=[1,2,3]
t1.insert(7,5)
t1
Out[21]:
[1, 2, 3, 5]
In [24]:
l=['abc','zxzx','nnnn','aaa']
l.sort()
l
Out[24]:
['aaa', 'abc', 'nnnn', 'zxzx']
In [27]:
l.sort(reverse=False)
l
Out[27]:
['aaa', 'abc', 'nnnn', 'zxzx']
In [28]:
l=['A','B','aaa']
l.sort()
l
Out[28]:
['A', 'B', 'aaa']
In [29]:
ord(1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-77e5aeef42fb> in <module>
----> 1 ord(1)

TypeError: ord() expected string of length 1, but int found
In [30]:
chr(1)
Out[30]:
'\x01'
In [31]:
chr(97)
Out[31]:
'a'
In [32]:
ord('A')
Out[32]:
65
In [33]:
ord('1')
Out[33]:
49
In [34]:
ord('a')
Out[34]:
97
In [ ]:
 

No comments:

Post a Comment