forked from karunaket/_Python_
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_methods.py
74 lines (32 loc) · 1.14 KB
/
list_methods.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
#append()
numbers = [1, 2, 3, 4, 5]
#To add a new elemnent at the end of this list, we can use append method
numbers.append(6)
print(numbers)
#insert()
numbers2 = [1, 2, 3, 4, 5]
#To insert a number at the middle or beginning of the list
numbers2.insert(0, -1)
#Here, 0 is the index and -1 is the number to insert...So this will insert -1 before the index 0
#After this the OUTPUT : [-1, 1, 2, 3, 4, 5, 6]
numbers2.insert(2, "Heyy")
#Here, 2 is the index and "Heyy" is the string to insert...So this will insert "Heyy" before the index 2
print(numbers2)
#remove()
nums = [23, 34, 56, 78]
nums.remove(56)
#It removes the no. 56 from the list
print(nums)
#clear()
nums2 = [243, 534, 856, 978]
nums2.clear()
#It removes all the nos. from the list
print(nums2)
#To check whether the give no. is in the list or not:
apple = [1, 2, 3, 4, 5]
print(2 in apple) # 2 is the no. and apple is the name of the list
#len
apple = [1, 2, 3, 4, 5]
print(len(apple)) # It returns the no. of elements in the list named apple
test = "Nakib gajfukka"
print(len(test)) # It returns the no. of elements in the variable named test