Lists are used to store multiple items in a single variable.To separate two items, you use a comma ( , ) . List uses the square brackets [ ]. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
This program creates a list called "a" which contains the elements 1, 2, 3, 4, and 5. The first print(a) statement prints the entire list. The second print(type(a)) statement prints the type of the variable, which in this case is a list. The next line, a[0] = 100, modifies the first element of the list to be 100. The next print(a) statement prints the modified list with the first element as 100.
The next lines, print("Slicing"), print(a[1]), print(a[-1]), print(a[0:3]), print(a[2:]), and print(a[:3]) are all examples of "slicing", which allows you to extract specific elements from a list by specifying the starting and ending indices.
# List in Python """ Sequence Type Mutable a[5] a={1,2,3,4,5} a[0] """ a = [1, 2, 3, 4, 5] print(a) print(type(a)) a[0] = 100 print(a) print("Slicing") print(a[1]) print(a[-1]) print(a[0:3]) print(a[2:]) print(a[:3]) print("-----------------------------")
This program creates a list called "a" which contains a variety of elements including integers, booleans, strings, and even a nested list. The first print(a) statement prints the entire list. The second print(type(a)) statement prints the type of the variable, which in this case is a list.
The next lines, print(a[0], " type is ", type(a[0])), print(a[1], " type is ", type(a[1])), print(a[2], " type is ", type(a[2])), print(a[3], " type is ", type(a[3])), print(a[4], " type is ", type(a[4])) are used to print the element and it's type from the list.
The last line print(a[4][1]) prints the second element of the nested list which is 2. Here, the first set of brackets a[4] refer to the nested list and the second set of brackets [1] refer to the index of the element within that nested list.
a = [1, True, 'Ram', 2.5, [1, 2, 3, 4]] print(a) print(type(a)) print(a[0], " type is ", type(a[0])) print(a[1], " type is ", type(a[1])) print(a[2], " type is ", type(a[2])) print(a[3], " type is ", type(a[3])) print(a[4], " type is ", type(a[4])) print(a[4][1]) print("-----------------------------")
This program creates a list called "a" which contains four elements: 10, 25, 35, and 45. The first print(a) statement prints the entire list.
a = [10, 25, 35, 45] print(a) a.clear() print(a) a = [10, 25, 35, 45] b = a.copy() print(b) a = [10, 25, 35, 45, 25, 4, 25] print(a.count(25)) print(a.index(25)) print(len(a)) print(max(a)) print(min(a)) print(a) a.pop(0) # remove Element using index print(a) a = [10, 25, 35, 45, 25, 4, 25] a.remove(25) # Values print(a) print("-----------------------------")
This program creates a list called "names" which initially contains one element "Ram". The first print(names) statement prints the entire list. The next few lines, names.append("Sam"), names.append("Ravi"), names.append("Kumar") are used to add elements "Sam", "Ravi" and "Kumar" to the list "names" using the append() method. The second print(names) statement prints the updated list with the added elements.
The next line, name2 = ["Sara", "Anitha"] creates a new list called name2. The next line, names.extend(name2) is used to add the elements of name2 to the list names using the extend() method. The next print(names) statement prints the updated list with the added elements of name2. The next line, names.insert(0,"Suriya") is used to insert an element "Suriya" at index 0 of the list "names" using the insert() method. The last print(names) statement prints the updated list with the newly inserted element
names = ["Ram"] print(names) names.append("Sam") names.append("Ravi") names.append("Kumar") print(names) name2 = ["Sara", "Anitha"] names.extend(name2) print(names) names.insert(0,"Suriya") print(names) print("-----------------------------")
This program demonstrates various built-in list functions and methods.
print(list(range(5))) print(list("Tutorjoes")) a=[10,50,100,25,85] print(a) a.sort() print(a) a.sort(reverse=True) print(a) a=["Orange","Apple","Zebra"] a.sort() print(a) a.sort(reverse=True) print(a) a=["Orange","Apple","Zebra"] a.sort(key=len) print(a)To download raw file Click Here
[1, 2, 3, 4, 5][100, 2, 3, 4, 5] Slicing 2 5 [100, 2, 3] [3, 4, 5] [100, 2, 3] ----------------------------- [1, True, 'Ram', 2.5, [1, 2, 3, 4]] 1 type is True type is Ram type is 2.5 type is [1, 2, 3, 4] type is 2 ----------------------------- [10, 25, 35, 45] [] [10, 25, 35, 45] 3 1 7 45 4 [10, 25, 35, 45, 25, 4, 25] [25, 35, 45, 25, 4, 25] [10, 35, 45, 25, 4, 25] ----------------------------- ['Ram'] ['Ram', 'Sam', 'Ravi', 'Kumar'] ['Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha'] ['Suriya', 'Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha'] ----------------------------- [0, 1, 2, 3, 4] ['T', 'u', 't', 'o', 'r', 'j', 'o', 'e', 's'] [10, 50, 100, 25, 85] [10, 25, 50, 85, 100] [100, 85, 50, 25, 10] ['Apple', 'Orange', 'Zebra'] ['Zebra', 'Orange', 'Apple'] ['Apple', 'Zebra', 'Orange']
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions
Lists are used to store multiple items in a single variable.To separate two items, you use a comma ( , ) . List uses the square brackets [ ]. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
This program demonstrates various built-in functions and methods that can be used with lists in Python.
# List in Python """ Sequence Type Mutable a[5] a={1,2,3,4,5} a[0] """ a = [1, 2, 3, 4, 5] print(a) print(type(a)) a[0] = 100 print(a) print("Slicing") print(a[1]) print(a[-1]) print(a[0:3]) print(a[2:]) print(a[:3]) print("-----------------------------") a = [1, True, 'Ram', 2.5, [1, 2, 3, 4]] print(a) print(type(a)) print(a[0], " type is ", type(a[0])) print(a[1], " type is ", type(a[1])) print(a[2], " type is ", type(a[2])) print(a[3], " type is ", type(a[3])) print(a[4], " type is ", type(a[4])) print(a[4][1]) print("-----------------------------") a = [10, 25, 35, 45] print(a) a.clear() print(a) a = [10, 25, 35, 45] b = a.copy() print(b) a = [10, 25, 35, 45, 25, 4, 25] print(a.count(25)) print(a.index(25)) print(len(a)) print(max(a)) print(min(a)) print(a) a.pop(0) # remove Element using index print(a) a = [10, 25, 35, 45, 25, 4, 25] a.remove(25) # Values print(a) print("-----------------------------") names = ["Ram"] print(names) names.append("Sam") names.append("Ravi") names.append("Kumar") print(names) name2 = ["Sara", "Anitha"] names.extend(name2) print(names) names.insert(0,"Suriya") print(names) print("-----------------------------") print(list(range(5))) print(list("Tutorjoes")) a=[10,50,100,25,85] print(a) a.sort() print(a) a.sort(reverse=True) print(a) a=["Orange","Apple","Zebra"] a.sort() print(a) a.sort(reverse=True) print(a) a=["Orange","Apple","Zebra"] a.sort(key=len) print(a)To download raw file Click Here
[1, 2, 3, 4, 5][100, 2, 3, 4, 5] Slicing 2 5 [100, 2, 3] [3, 4, 5] [100, 2, 3] ----------------------------- [1, True, 'Ram', 2.5, [1, 2, 3, 4]] 1 type is True type is Ram type is 2.5 type is [1, 2, 3, 4] type is 2 ----------------------------- [10, 25, 35, 45] [] [10, 25, 35, 45] 3 1 7 45 4 [10, 25, 35, 45, 25, 4, 25] [25, 35, 45, 25, 4, 25] [10, 35, 45, 25, 4, 25] ----------------------------- ['Ram'] ['Ram', 'Sam', 'Ravi', 'Kumar'] ['Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha'] ['Suriya', 'Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha'] ----------------------------- [0, 1, 2, 3, 4] ['T', 'u', 't', 'o', 'r', 'j', 'o', 'e', 's'] [10, 50, 100, 25, 85] [10, 25, 50, 85, 100] [100, 85, 50, 25, 10] ['Apple', 'Orange', 'Zebra'] ['Zebra', 'Orange', 'Apple'] ['Apple', 'Zebra', 'Orange']
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions