-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes
More file actions
133 lines (69 loc) · 2.49 KB
/
notes
File metadata and controls
133 lines (69 loc) · 2.49 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
// = int val in division
/ = float val in division
# aama c ni jem define n hoy aapde capital ma lakhi to
e constant j ganay ,
EX:
PAI = 3.14
imoprt math karine
pai.math kari to e direct pai ni val j aape
remove() Method
Removes the first occurrence of a specific value from the list.
Raises a ValueError if the value is not found.
arr = [10, 20, 30, 40, 30]
arr.remove(30) # Removes the first 30 from the list
print(arr) # Output: [10, 20, 40, 30]
2. pop() Method
Removes an element at a specified index.
If no index is provided, it removes the last
element by default.
Returns the removed element.
arr = [10, 20, 30, 40]
removed = arr.pop(2) # Removes the element at index 2 (30)
print(arr) # Output: [10, 20, 40]
print(removed) # Output: 30
arr = [10, 20, 30, 40]
arr.pop() # Removes the last element (40)
print(arr) # Output: [10, 20, 30]
3. del Statement
Deletes an element at a specified index or slices of a list.
arr = [10, 20, 30, 40]
del arr[1] # Removes the element at index 1 (20)
print(arr) # Output: [10, 30, 40]
arr = [10, 20, 30, 40, 50]
del arr[1:3] # Removes elements at indices 1 and 2 (20, 30)
print(arr) # Output: [10, 40, 50]
Using List Comprehension
Creates a new list by excluding specific elements without modifying the original list.
arr = [10, 20, 30, 40, 30]
arr = [x for x in arr if x != 30] # Removes all occurrences of 30
print(arr) # Output: [10, 20, 40]
5. clear() Method
Removes all elements from the list, leaving it empty.
arr = [10, 20, 30, 40]
arr.clear()
print(arr) # Output: []
seen = set() is used to create an empty set in Python.
A set is a built-in data structure that holds unique
elements and does not allow duplicates.
It is particularly useful for keeping track
of items that have already been encountered or processed.
What Does seen = set() Do?
Initializes an Empty Set:
set() creates an empty set,
which can later store elements.
For example:
seen = set()
Stores Unique Items Only:
Sets automatically remove duplicates.
If you add the same element multiple times,
it will appear only once.
Efficient Membership Check:
You can check if an element is already in
the set using the in keyword,
which is faster in a set compared to a list.
end=" " in print():
By using end=" ", the elements of
a row are printed on the same
line with a space separating them,
instead of the default newline.
print(line.strip()) # Strip removes trailing newline or spaces