How to store a lot of data in one variable is to use a List. List is the most basic data structure in python programming language. Successive elements will be assigned a position number or commonly referred to as an index with the first index being 0 and so on.
Python has 6 built-in sequence types and the most commonly used are lists and tuples. Lists are commonly used in indexing, slicing, adding, multiplying and checking operations.
Creating a list is very easy we can make it like this:
list1 = [‘python’, ‘flutter’, 2018, 2020]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print (“list1[0]: “, list1[0])
print (“list2[1:5]: “, list2[1:5])
After you execute the code above, the result will be like this:
list1[0]: python list2[1:5]: [2, 3, 4, 5]
We can also update one or more values in the list by :
list = [‘python’, ‘flutter’, 2018, 2020]
print (“Nilai ada pada index 2 : “, list[2])
list[2] = 2021
print (“Nilai baru ada pada index 2 : “, list[2])
Dan selanjutnya jika kita ingin menghapus item yang sudah kita ketahui elemenya maka bisa menggunakan del dan gunakan metode remove() jika kamu tidak mengetahui persis dimana item yang akan dihapus.
list = [‘python’, ‘flutter’, 2018, 2020]
print (list)
del list[2]
print (“Setelah dihapus nilai pada index 2 : “, list)
List responds to all common sequence operations commonly used in strings