Types of data collection in python — part 1 of python intermediate level notes
(1)LIST:
Lists are a collection of data types that are ordered and mutable.
1.Storing variables in a list:
In lists, we first create a variable to store the whole list which is in square brackets[],
in which we store string values, integers. etc and store that list in the variable called mylist.
for example: mylist = [“banana”, “apple”, “cherry”]
2.Empty lists:
Empty lists are that lists which do not contain any string values, integers or any other elements.
for example: mylist2 = list()
Visit this website for complete detail:- https://pranav14blog.blogspot.com/2021/03/what-is-list-in-python.html
3.Accessing an element from the list:
To access an element from the list we can use integers to access a specific element from that list,
In which 1st number can be accessed by the integer 0 and the second element can be accessed by the integer 1 and so on.
We can also use negative integers to access an element from the last element.
4.Checking for an element or for the number of element:
To check for an element you can set a if condition :
for example:
mylist = [“banana”, “apple”, “cherry”]
if “banana” in mylist:
print(“yes”)
else:
print(“no”)
output = yes
To check for number of elements you can use the function ‘len’
for example :
mylist = [“banana”, “apple”, “cherry”]
print(len(mylist))
output = 3
5.How to add,remove,reverse,sort.etc elements in the list:
add:
To add an element to the list you can use ‘append’.To use append first write the name of your list then type append(the element you want to add)
for example :
mylist.append(“lemon”)
To add an element at a spcific position you can use ‘insert’
for example:
mylist.insert(1,