The most basic data structure of Python is the sequence (list/tuple). Each element in a sequence is assigned a number-its position or index. The first index is 0, the second index is 1, and so on.
Python has 6 built-in types of sequences, but the most common are lists and tuples, which we'll see in this tutorial.
There are some things that can be done using all sequence types. These operations include indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in functions to find the length of a sequence and search for its largest and smallest elements.
Python list:
Lists are the most common types of data, and in Python you can write a comma-delimited list of square brackets (items). The list of items that are in a list is not necessarily the same data type.
Creating a list is as simple as putting squere brackets between different comma-separated values. For example:
List1 = [' Physics ', ' Chemistry ', 1997, 2000];list2 = [1, 2, 3, 4, 5];list3 = ["A", "B", "C", "D"];
such as the string index, the list index starts at 0, and lists the available slices, joins, and so on.
To access the values in the list:
To access the values in the list, use the square brackets along the slices to index or index the values that correspond to the available indexes. The following is a simple example:
#!/usr/bin/pythonlist1 = [' Physics ', ' Chemistry ', 1997, 2000];list2 = [1, 2, 3, 4, 5, 6, 7];p rint "list1[0]:", list1[0]p Rint "List2[1:5]:", List2[1:5]
When the above code is executed, the following results are produced:
LIST1[0]: Physicslist2[1:5]: [2, 3, 4, 5]
Update list:
You can update a single or multiple elements of a list by giving the slice assignment operator the left side, and you can add elements to the list by using the Append () method. The following is a simple example:
#!/usr/bin/pythonlist = [' Physics ', ' Chemistry ', 1997,];p rint "Value available at index 2:" Print list[2];list[2] = 2001;print "New value available at index 2:" Print list[2];
Note: the append () method is discussed in the following sections.
When the above code is executed, the following results are produced:
Value available at index 2:1997new value available at index 2:2001
To delete an element in a list:
To delete the elements of a list, you can use the DEL statement, if you know which elements to delete, or if you don't know how to use the Remove () method. The following is a simple example:
#!/usr/bin/pythonlist1 = [' Physics ', ' Chemistry ', 1997,];p rint list1;del list1[2];p rint "after deleting value at Inde X 2: "Print list1;
When executing the above code, it produces the following result:
[' Physics ', ' Chemistry ', 1997, 2000] After deleting value at index 2: [' Physics ', ' chemistry ', 2000]
Note: The Remove () method is discussed in a later section.
Basic list operations:
Lists the + and * operators like strings; concatenation and repeating strings are the same here, and the difference is that the result is a new list instead of a string.
In fact, the list responds to all of the general sequence of operations we use in strings.
indexes, slices, and matrices:
Because of list sequences, indexes and slices work in a similar way to their string operations.
Assume the following input:
L = [' spam ', ' spam ', ' spam! ']