How to use lists in Python

Lists


Lists use for store multiple values in a single variable.
we can use square brackets to create a list.
list can conation one or more datatypes.
Lists are indexed, ordered, allow duplicate values and changeable


  • Create a lists

languages=["python","java","C++","C","Php","HTML","C"]
friends=["gorge","mark","steave","peter","eric"]


  • To display the items in list

print(languages)
print(languages[2])
print(languages[1:4])
print(languages[2:])
print(languages[:3])



  • To add new values

languages.append('Ruby')
languages.insert(2,'Bash')


  • To change values

languages[2]="PHP"



  • To remove items by index

languages.pop(3)

  • To remove items by value

languages.remove("python")

  • To delete items by index

del(languages[1])

  • To delete all items in a list

languages.clear()



  • To combine two lists 

languages.extend(friends)



***

0 comments:

Post a Comment