-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.py
211 lines (178 loc) · 3.45 KB
/
List.py
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
# List Functions
# Seq(Converts any sequence to lists)
s='Hello world'
p=1,2,3,4
print(list(s))
print(list(p))
# Extends sequence to the list
s="Hello World"
l=[1,2,3,4]
l.extend(s)
print(l)
# Indexing
pets=["dog","cat","ferrat","piegon"]
print(pets[::])
print(pets[::-1])
print(pets[0:4:2])
# Append(adds at the end of list)
pets.append("rabbit")
print(pets)
# Insert(inserting element at a paritcular index)
(pets.insert(0,"puppy"))
print(pets)
# Extend(inserts a whole list at the end of the existing list)
(pets.extend(["cat","dog"]))
print(pets)
# Count(count the specified element)
# 1
print(pets.count("dog"))
# 2
grades=['B','B','F','C','B','A','A','D','C','D','A','A','B','D']
l2=[grades.count('A'),grades.count('B'),grades.count('C'),grades.count('D'),grades.count('E'),grades.count('F')]
print(l2)
# Remove(remove the specified element from the list)
(pets.remove("cat"))
print(pets)
# Pop(removes the last item of list)
(pets.pop())
print(pets)
(pets.pop(2))
print(pets)
# Reverse(reverses the list)
(pets.reverse())
print(pets)
# Index
print(pets[2])
# Clear(Clear the whole list but the structure will be there)
(pets.clear())
print(pets)
# Delete(delete list+structure)
del pets
# SWAPPING OR SORTING
# 1
l1=[6,3,25,1,90,9,7,100,95]
for i in range(len(l1)-1):
for j in range(len(l1)-1):
if l1[j]>l1[j+1]:
t=l1[j+1]
l1[j+1]=l1[j]
l1[j]=t
print(l1)
# OR
l1=[6,3,25,1,90,9,7,100,95]
l1.sort()
print(l1)
# 2
g=[9,7,7,10,3,9,6,6,2]
for i in range(len(g)-1):
for j in range(len(g)-1):
if g[j]>g[j+1]:
temp=g[j+1]
g[j+1]=g[j]
g[j]=temp
print(g)
# NESTING OF A LIST:
# MATRIX FROM USER INPUT:
x=[]
for i in range(3):
print("Enter Elements for",i,"Row:")
y=[]
for j in range(3):
y.append(int(input()))
x.append(y)
print()
for i in x:
print(i)
# MATRIX ADDITION:
A=[[1,2,3],
[4,5,6],
[7,8,9]]
B=[[2,4,6],
[1,3,5],
[7,8,9]]
R=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(A)):
for j in range(len(A[0])):
R[i][j] = A[i][j]+B[i][j]
# print(R)
print()
for i in R:
print(i)
# Matrix Multiplication
A=[[1,2,3],
[4,5,6],
[7,8,9]]
B=[[2,4,6],
[1,3,5],
[7,8,9]]
R=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
R[i][j] += A[i][k] * B[k][j]
print()
for i in R:
print(i)
print("\n")
# Practice
# 1
l=[]
n=int(input("Enter no of integers you want to input:"))
for i in range(1,n+1):
print("Enter Integer",i,":")
x=int(input())
l.append(x)
print(l)
print("")
#a and b(middle element and its index)
if len(l)%2==0:
import math
m= math.ceil(len(l)/2)
print(l[m])
print(l.index(l[m]))
else:
m=int(len(l)/2)
print(l[m])
print(l.index(l[m]))
print("")
# c
print("Sorting Of List In Descending Order")
l.sort(reverse=True)
print(l)
print("")
# d (removes 1st item and puts it at the end)
x=l.pop(0)
print(l)
y=l.append(x)
print(l)
print("")
# 2
l=[]
for i in range(5):
print("Enter floating point number",i,"")
x=float(input())
l.append(x)
print(l)
print()
sum=sum(l)
print("sum=",sum)
print()
avg=sum/len(l)
print("avg=",avg)
print()
print("max=",max(l))
print()
print("min=",min(l))
#
print("Sorting Of List In Descending Order")
for i in range(len(l)-1):
for j in range(len(l)-1):
if l[j+1]>l[j]:
temp=l[j]
l[j]=l[j+1]
l[j+1]=temp
print(l)