-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist python notes.txt
259 lines (169 loc) · 3.84 KB
/
list python notes.txt
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
delete list element - used to delete emelemt of list or delete variables
del a - delete complete list
or
del a[2] - list element
or
del a[2,5] range in list
list opreators
1] list repeater - repetation of list --multiply content of list
syntax - list*no of times repeation
example
a=[11,22,33]
print (a*3)
>>> [11,22,33,11,22,33,11,22,33]
2] list concationation used to merge /concate 2 or more list
+ symbol is used to concate 2 list
syntax list1+list2
example
a=[11,22]
b=[33,44,55]
print(a+b)
>>> [11,22,33,44,55]
3] display list elements one by one
we use for loop here
syntax
for value in list:
print(value)
example
a=[11,22,33,44]
for i in a:
print(i)
>>>
11
22
33
44
for loop syntax
for variable in range(start_index, end_index)
example
for a in range(1,5)
print(a)
>>>
1
2
3
4
5
or
for variable in listname
print(variable)
a=[11,22,33,44]
for i in a:
print(i)
>>>
11
22
33
44
s=["java","php","cpp","dsa","python"]
for i in s:
print(i)
>>>
java
php
cpp
dsa
python
Q) accept no number in list and print
n=int(input("ENter number"))
a=[]
for i in range(0,n):
num=int(input("ENter number"))
a.append(num)
print(a)
print("Even numbers")
for i in range(0,n):
if a[i]%2==0:
print(a[i])
# append --add only one element at the end of list
a=[11,22,33,44]
b=[10,20,30]
a.append(b)
print(a)
output >>> [11,22,33,44,[10,20,30]]
# extend -- add more than one element at the end of list
a=[11,22,33,44]
b=[10,20,30]
a.extend(b)
print(a)
output >>> [11,22,33,44,10,20,30]
# insert - this function insert element at given potion in the list
syntax -- a.insert(index,no_to insert)
example
a=[11,22,33,44]
a.insert(2,99)
print(a)
output >>> [11,22,99,33,44]
# remove - remove the first occurred element of the element is remove
if there are same value more than one times
del is used to remove element by insex
del a[2]
remove is used to remove element by value
a=[11,22,33,44,55,33]
a.remove(33)
print(a)
>>>[11,22,44,55,33] ---only first element is removed in case there
are multiple nos\values
# pop()
it is used tu remove and retrive last element
syntax val=a.pop()
example
a=[11,22,33,44]
val=a.pop()
print("a=",a)
print("Deleted /pop element ",val)
>>> a= [11,22,33]
>>> deleted elemment=44
--by default pop function remove last element
--if we pass index to pop function we can retrive any index element
syntax val=a.pop(index)
example
a=[11,22,33,44]
val=a.pop(2)
print(a)
print("Deleted /pop element ",val)
>>> a= [11,22,44]
>>> deleted elemment=33
#revere() -- it is uued to reverse the list element
syntax a.reverse()
a=[11,22,33,44]
a.reverse()
print(a)
>>> [44,33,22,11]
# len() --it is used to print length of list
a=[11,22,33,44,33,55,33]
print("Length of list=",len(a))
>>> Length of list = 7
# count --used to count how many times the no is in the list
a=[11,22,33,44,33,55,33]
print("Count Value=",count(33))
>>> Count value = 3
max and min -- used to display max and min element
a=[11,22,33,44,7,33,55,33]
print("minimum number=",min(a))
print("maximun number=",max(a))
# sort -- used to sort element in accending order
syntax a.sort()
a=[66,44,11,9,66,77,44]
a.sort()
print(a)
>>>[9,11,44,44,66,66,77]
# sort reverse-- used to sort element in descending order
syntax a.sort(reverse=True)
a=[66,44,11,9,66,77,44]
a.sort(reverse=True)
print(a)
>>>[77,66,66,44,44,11,9]
# clear()-- used to empty / clear the list
a=[11,22,33]
a.clear()
print(a)
>>>[]
# copy() -- return shallow copy of the list
-- copy one list in another
a=[11,22,33]
b=a.copy()
print(a)
print(b)
>>>> [11,22,33] --a list
>>>> [11,22,33] --b list