-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistApplications.py
187 lines (173 loc) · 4.32 KB
/
listApplications.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
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 14 16:56:30 2024
@author: SÜLEYMAN BEYYY
"""
# Adding Elements to the List
'''
The append() method is used to add an element to the end of Python lists.
'''
'''
list1 = ["banana", "grape", "cherry"]
list1.append("apple")
print(list1)
'''
'''
The insert() method is used to add an element to a specified index in Python
lists.
'''
'''
list1 = ["banana", "grape", "cherry"]
list1.insert(1,"apple")
print(list1)
'''
# Deleting an Element from the List
'''
There are different methods we can use to delete elements from Python lists.
We can use the remove() method to delete an element from the list.
'''
'''
list1 = ["banana", "grape", "cherry"]
list1.remove('cherry')
print(list1)
'''
'''
The pop() method is used to delete the element at a specified index in Python
lists. If we do not specify an index number, the last element of the list is
deleted.
'''
'''
list1 = ["banana", "grape", "cherry"]
list1.pop(1)
print(list1)
list1.pop()
print(list1)
'''
'''
We can delete the element at any index number with the del() method.
'''
'''
list1 = ["banana", "grape", "cherry"]
del list1[1]
print(list1)
'''
'''
If we do not give an index number to the del() method, it deletes the list as
it is.
'''
'''
list1 = ["banana", "grape", "cherry"]
del list1
print(list1)
'''
'''
In this case, when we want to access the list object, we receive a NameError,
and in the description, we receive an error message stating that the list is
not defined.
'''
'''
We can also delete the list with the clear() method, just as we delete the list
with the del command. However, the difference is that when we delete the object
reference with del and we get a NameError when we want to access the list, we
do not get an error with the clear() method because the reference of the list
continues to be in memory but it becomes empty.
'''
'''
list1 = ["banana", "grape", "cherry"]
list1.clear()
print(list1)
'''
'''
As you can see, the value [ ] returned to us by print(list) is the empty list
definition. It still holds space in memory and we can continue to add elements
to this list.
'''
# List Copy
'''
List is treated as a class and reference type in memory, so when we want to
assign a list to another list, the list elements are not copied, instead the
address information of the list in memory is copied.
'''
'''
list1 = ["banana", "grape", "cherry"]
list2 = ["banana", "grape"]
list1 = list2
print(list1)
list2.clear()
print(list1)
'''
'''
Therefore, there are some list methods we need to use when copying lists.
We can use the copy() method to assign the contents of a list to another list.
'''
'''
list1 = ["banana", "grape", "cherry"]
list2 = ["banana", "grape"]
list2 = list1.copy()
print(list2,list1)
list1.append('grape')
print(list2,list1)
'''
# Sorting List Elements
'''
The sort() method is used to sort the list elements.
'''
'''
numbers = [1, 10, 5]
letters = ['a', 'g', 's']
print(numbers)
print(letters)
numbers.sort() -> They are arranged numerically from smallest to largest.
letters.sort() -> Sorted alphabetically from a-z.
print(numbers)
print(letters)
'''
'''
When we want to sort in reverse, we can send the reverse=True parameter.
'''
'''
numbers = [1, 10, 5]
letters = ['a', 'g', 's']
print(numbers)
print(letters)
numbers.sort(reverse=True) -> They are listed in numerical order from largest
to smallest.
letters.sort(reverse=True) -> Sorted alphabetically from z-a.
print(numbers)
print(letters)
'''
'''
We can print list elements in reverse using the reverse() method.
'''
'''
numbers = [1, 10, 5]
letters = ['a', 'g', 's']
numbers.reverse()
letters.reverse()
print(numbers)
print(letters)
'''
# Other List Methods
# Min() and Max() Method
'''
We can get the minimum and maximum value in a list numerically or
alphabetically.
'''
'''
numbers = [1, 10, 5]
letters = ['a', 'g', 's']
print(min(numbers))
print(max(numbers))
print(max(letters))
print(min(letters))
'''
# Count() Method
'''
We use the count() method to get the number of repeating elements in a list.
'''
'''
numbers = [1, 10, 5, 16, 4, 9, 10]
letters = ['a', 'g', 's', 'b', 'y', 'a', 's']
print(numbers.count(10))
print(letters.count('a'))
'''