Table of contents
Hello People, in my previous blog I talked about Identifiers,Keywords and Literals . In this blog we'll learn about the data-type
known as Lists
.
When we talk about lists, the first thing that comes to our mind is that it refers to a collection of data
in an ordered manner
so that we can access those data by their order of occurance (position number). A list is denoted by a pair of square brackets []
. Here, the ordering starts from 0 (we start counting the first element of the list at zeroth position).
LIST : It is one of the 4 data-types in python used to store multiple data in a single variable.
list1=[1,2,3,4]
print(list1) #prints the entire list.
Output
[1,2,3,4]
How to access list elements?
- List elements can be accessed by their position.
list1=[1,2,3,4]
print(list1[0]) #prints the first element of the list, i.e. , 1.
Output
[1]
- The elements of a
list
can bemodified
at any point of time. and can contain duplicate items.
list1=[1,2,3,4]
list1[0]=4
print(list1) #contains duplicate items at zeroth position and third position, i.e. , 4
Output
[4]
- A single list may contain a variety of literals such as string literal, numeric literal, floating literal, character literal or boolean literal.
list1=[1, "Hello", 1.4, "c", True ]
print(list1)
Output
[1, "Hello", 1.4, "c", True ]
List Length :
- the length of a list can be found out using the len() function.
list1=[1, "Hello", 1.4, "c" ]
print(len(list1))
Output
4
List constructor :
- we can also define list using list() as shown below.
list1=list(1,2,3,4)
print(list1)
Output
[1,2,3,4]
You must know this :
You can check the data-type using type() function.
list1=[] print(type(list1))
Output
<class 'list'>