forked from shubhansu-kr/INT213-Python-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_ListMethods.py
71 lines (53 loc) · 1.13 KB
/
12_ListMethods.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
# List methods
nums = [1,2,3,1,2,4]
# count(parameter) : Gives the frequency of element
print(nums.count(3))
# sort - By default: ascending order
nums.sort()
print(nums)
# sort - parameter: reverse = true for descending order
nums.sort(reverse=True)
print(nums)
# sorted - makes a copy of list and doesn't modify the orignal list
# sorted is a function: not a method
sortedNums = sorted(nums)
print(sortedNums)
# clear:
sortedNums.clear()
print (sortedNums)
# copy:
sortedNums = nums.copy()
print (sortedNums)
sortedNums.clear()
sortedNums = nums
print (sortedNums)
# max, min, len - functions
print (max(nums))
print (min(nums))
print (len(nums))
# Enumerate Functions: Used to print elements along with index
enum = list(enumerate(nums))
print (enum)
print(type(enum[0]))
# i is the index and j is the value
for i, j in enum:
print(i, j)
# Operators
list1 = [2,4,5,3,6]
list2 = [4,2,6,5]
# Concatination
list2 = list1 + list2
print(list2)
# replication
list2 = list2 * 3
print (list2)
# in
if 3 in list2:
print ('Yes')
else:
print('No')
# not in
if 3 not in list2:
print ('Yes')
else:
print ('No')