List:
- The list data type is used to store the collection of an item in an ordered sequence.
- These items are mutable, which means changeable as possible.
- It is denoted by square bracket [].
- Its index value is started with 0 and ends with list_length-1.
Syntax:
Declaring list:
Variable_name=[]
Example:
List=[]
Declaring value into the list:
Variable_name=[item1,item2,...item n]
Example:
a=[1000,’Kurshitha’,’female’,450]
Example program:
a=[5,1,8,3]
print("Fist value : ",a[0])
print("Last value : ",a[len(a)-1])
print("Another method to print last value : ",a[-1])
print("List of elements : ",a)
Output:
Print list in looping statement:
a=[5,1,8,3]
for i in a:
print(i)
Output:
Insert or append an item into the list:
- To insert an element in the list is used append() function.
pin=[1234,1111,1211,1457]
p=int(input("Enter an item to add in list : "))
pin.append(p)
print("Your list of items are: ")
for i in pin:
print(i)
Output:
No comments:
Post a Comment