LIST DATATYPE


LIST DATATYPE

In python lists, data types are similar to arrays in C. It can contain data of different types. A comma separates the values (,) and it is enclosed within square braces ( [ ] ). We can use the slice operator (:) to access the data on the list. The plus (+) operator and the repetition operator(*) act the same way as in the strings.

Indexed items in the list:

The list items are indexed, and the first item has index [0], the second has [1], and so on. If you try to access a list index that is not available, then the IndexError will occur. Additionally, the index can only be an integer, and if you try accessing some other type, then the TypeError will occur.

The list is Ordered:

It means that there is some defined order that the list items follow and that order cannot be changed. Further, if some new items are added to the list, they add at the end of the list and nowhere in between.

The list is Changeable:

It means that the list items can be changed and manipulated. You can add, delete, sort the list items.

Identifying the length of a list:

To identify the length of a list, you must pass the len() function and fill the brackets with the list name whose length is to be identified.

Data types of list:

Python list items can contain any data type, including integers, floating value, character, strings, Boolean, etc. You can create a list with only one type of data type or a mixture of several data types.

Example:

x = ["Knowledge2life 1", "Knowledge2life 2", "Knowledge2life 3"] print(x)

OUTPUT:

['Knowledge2life 1', 'Knowledge2life 2', 'Knowledge2life 3']