-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path1.BasicOfList.py
More file actions
173 lines (111 loc) · 3.72 KB
/
1.BasicOfList.py
File metadata and controls
173 lines (111 loc) · 3.72 KB
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
'''When you create a list in Python, the interpreter creates an array-like data
structure in memory to hold your data, with your data items stacked from
the bottom up'''
'''To define a List'''
country=['india','america','japan','china']
'''
postion element
0 india
1 america
2 japan
3 china
'''
'''To printing a list'''
print(country)
'''To Get Length of List'''
print(len(country))
'''Prining list element by using indexing'''
print(country[0])
'''printing last element from list'''
print(country[-1])
'''Assigning new element in list ,replacing'''
country[1]='Nepal'
print(country)
'''Capitlize the first letter'''
print(country[1].capitalize())
'''To lowercase conversion'''
print(country[1].lower())
'''To swapecase'''
print(country[1].swapcase())
'''Apend And Insert Into List'''
'''appending element to last to list'''
country.append('america')
print(country)
'''How to Add element in list at any postion use insert() method'''
#listname.insert(postion,'element')
country.insert(1,'UK')
print(country)
#['india', 'UK', 'Nepal', 'japan', 'china', 'america']
#If you want to add elemnt at last position and you tried to add element at -1 postion
#country.insert(-1,'oman') now in our last updated list america is at -1 position if you run
#such query then america will shift to right and oman addedd at location of the america
country.insert(-1,'oman')
print(country)
#['india', 'UK', 'Nepal', 'japan', 'china', 'oman', 'america']
#if you give position out of range of list then the element is added at last postion
country.insert(4543,'Bhutan')
# print(country[4543]) this will give an error
print(country)
#['india', 'UK', 'Nepal', 'japan', 'china', 'oman', 'america', 'Bhutan']
#if you used negative number which is out of range then that item will place at zero postion
country.insert(-34,'SA')
print(country)
#['SA', 'india', 'UK', 'Nepal', 'japan', 'china', 'oman', 'america', 'Bhutan']
#deleting element from list
#----------------------------------------
#if you removed item using del statment you cant use them for further process
#1.deleting element using indexing
#del listname[index]
del country[1]
print(country)
#['SA', 'UK', 'Nepal', 'japan', 'china', 'oman', 'america', 'Bhutan']
#2.You can use pop method to remove element from list and use it whne you need it
country.pop() # this line remove the last elment from the given list
print(country)
y=country.pop() # this line save them removed element
print(country)
print(y)
#removing element from list at specific posiotion
country.pop(3)
print(country)
#3.Removing element using remove method
#using remove method you can easily remove element using list item
country.remove('SA')
print(country)
#If you give a such item name which is not present in the list it will give error
#country.remove('jkdsncfsd') jkdsncfsd element is not present in list
#How to findout position or index of given string
index=country.index('Nepal')
print(index)
#print(country.index('nepal')) this data searches in case sensetive manner
#How to reverse elemnt from given list
country.reverse()
print(country)
#Sort the list alphabetically
country.sort()
print(country)
#['Nepal', 'UK', 'china', 'oman'] Capital aplbhabets first
#sort the list into reverse order
country.sort(reverse=True)
print(country)
#['oman', 'china', 'UK', 'Nepal']
#Sorted Function
#after sorting we cant go back on original list so we use sorting function
#if you want to sort in alphabetical order
print(sorted(country))
#For ReverseSorting
r=(sorted(country))
r.reverse()
print(r)
#List in List
x=[['abc','bnk'],'cds','dss']
print(x[0][1])
#bnk
print(x[1][1])
#d
#Converting any data to list data type
y=('Ravishankar')
mylist=(list(y))
#['R', 'a', 'v', 'i', 's', 'h', 'a', 'n', 'k', 'a', 'r']
print(mylist[1])
#a