Lists [ ]

 #Lists

#what is a list - List is ordered and changeable data type, It is used to store

multiple values in a single variable.



list1  = [1,2,3,"rakesh"]
list2 = ["Jabalpur",30.50,"Khalsa"]
print(list1)
print(list2)

#access the elements

print(list1[2])
print(list2[-2]) #Negative Indexing

#slicing - is also called segment

print(list1[1:3])
print(list2[:-2])

if "Jabalpur" in list2:
     print("Yes jabalpur is in the list2")

#Change elements

#using index number

list1[3] = "suresh"
print(list1) #rakesh will be replaced with suresh

list1[4] = "suresh"
print(list1) # Error- Index Out of Range

#Change the element using slicing

list1[0:3] = [101,"MCA"]
print(list1)

#delete the last element
print(list1.pop())
print(list1)

#insert the element at position 2
print(list1.insert(2,"rakesh"))
print(list1)

#remove the element
print(list1.remove("rakesh"))
print(list1)

#append is used to add the element at the end position
print(list1.append("rakesh"))
print(list1)




Comments