forked from dscnsec/Python-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_lists.py
52 lines (38 loc) · 1.27 KB
/
4_lists.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
#This is a python script to demonstrate lists
#This is a list
#Lists are used to store multiple items in a single variable
myList = ["apple", "banana", "cherry"]
#add an item to the end of the list
myList.append("orange")
#add an item at a specific index
myList.insert(1, "mango")
#remove an item from the list
myList.remove("banana")
#remove an item at a specific index
myList.pop(1)
#remove the last item from the list
myList.pop()
#remove all items from the list
myList.clear()
#reverse the order of the list
myList.reverse()
#sort the list
myList.sort()
#copy the list
myList2 = myList.copy()
#join two lists
myList3 = myList + myList2
#list slicing
myList4 = myList[1:3]
#List methods
#append() - adds an element at the end of the list
#clear() - removes all the elements from the list
#copy() - returns a copy of the list
#count() - returns the number of elements with the specified value
#extend() - add the elements of a list (or any iterable), to the end of the current list
#index() - returns the index of the first element with the specified value
#insert() - adds an element at the specified position
#pop() - removes the element at the specified position
#remove() - removes the item with the specified value
#reverse() - reverses the order of the list
#sort() - sorts the list