Dec 31, 2022

What is List datatype in Python

 A list is a collection of items that are ordered and changeable. Lists are written with square brackets, and the items are separated by commas. Lists are one of the most commonly used data types in Python and are very versatile.

Here is an example of how to create a list in Python:

fruits = ['apple', 'banana', 'orange', 'mango']

You can access the items in a list by referring to the index number of the item. The index numbers start at 0, so the first item in the list has an index of 0, the second item has an index of 1, and so on.

Here is an example of how to access an item in a list:

print(fruits[0]) # prints 'apple'

You can also use negative index numbers to access items in the list, starting from the end. The last item in the list has an index of -1, the second to last item has an index of -2, and so on.

Here is an example of how to access the last item in a list using a negative index:

print(fruits[-1]) # prints 'mango'

You can modify an item in a list by assigning a new value to it.

Here is an example of how to change the second item in a list:

fruits[1] = 'strawberry'

You can also add new items to a list by using the append() method. This will add the new item to the end of the list.

Here is an example of how to add a new item to a list:

fruits.append('kiwi')

You can remove an item from a list using the remove() method. This will remove the first occurrence of the item from the list.

Here is an example of how to remove an item from a list:

fruits.remove('banana')

There are many other methods and operations that you can perform on lists in Python. For a complete reference, you can refer to the official Python documentation.

I hope this helps! Let me know if you have any questions.