Python is an object-oriented programming language and is mostly implicitly run, Python does not have a compiler and it works on the base of an interpreter that debugs each command line by line.
A list in Python is used to store variables and values much like an array in any other programming language. Since Python does not have anything such as an array, it utilises the 4 available storage data types to store multiple data/variable/values in a single container. Some of the most popularly used data types are Lists and Tuples.
A tuple is essentially a list without the ability of being mutable, it is non-mutable meaning it cannot be changed or altered later on in the code, only the outer functions can be applied on a tuple whereas a list can be manipulated by the program itself.
List in Python
It is quite a simple process to declare a list, since Python is an implicit language we do not need to use the list constructor to declare a list, variable and values in square brackets depict a list, here is how you can create one:
list1 = ["item1", "item2", "item3"]
print(list1) //This will print all the values in the list one after another, in the exact same order you entered them in
Lists are ordered, meaning whatever you enter in the list through the code will be inserted at the end of the list (the last value). It works more like an array but unless defined it will enter values at the end. Lists can also contain multiple different data types and multiple functions like len can be performed on the list.
list2 = ["mycar", "5555", "True", "False]
print(list2) //This will print the list2 that contains multiple different data types of string, integer and boolean.
list3 = ["mycar", "5555", "True", "False]
print(len(list2)) //The len function will be applied and the number of elements available in the list will be printed.
Apart from just having many functions that can be performed on a list, it can also be explicitly declared with the list() keyword. Defining it explicitly will make use of round brackets instead of the implicit square.
list4 = list(("item1", "item2", "item3", "item4")) // Notice how the list is declared in double brackets, this determines the value inside the list() constructor.
print(list4)
Tuples in Python
In a very similar manner to lists, tuples are also implicitly detected by the interpreter and do not require any explicit declaration unless needed. Tuples work in a very similar way to list with the only exception being that it cannot be changed once defined, only acted upon. Here is how you can declare a tuple implicitly
tuple1 = ("item1", "item2", "item3") // Notice how the tuple is captured in round brackets and not square ones, these characteristics stand out from lists when implicitly declared.
print(tuple1)
A list can contain a single item without any special identification but a tuple cannot, a tuple needs a comma separator after the single entry to have it declared implicitly as a tuple:
tuple2 = ("item1" , ) // If the comma separator is removed, it will no longer be declared as a tuple.
print(tuple2)
Similar to lists, tuples can also contain multiple different data-types with the only difference being that it cannot be changed later-on in the code:
tuple3 = ("name", "1234", “True”) // each of the different elements in the tuple belongs to a different data type, yet they can be stored together in the tuple.
print(tuple3)
Tuples can also contain duplicates in them without any special need of separator or identifier, since python is an implicitly run language, it can detect duplicates.
tuple5 = ("name1", 1234, "name1", 1234) // Both initial elements are repeated again in the tuple but it will not present any error but treat them in separate variable spaces.
print(tuple5)
You can also declare tuples explicitly using the tuple() constructor, although python detects tuples implicitly using the round brackets but declaring them explicitly removes the need of specifying special brackets for each data type.
tuple6 = tuple(("item1", "item2")) // One set of brackets belong to the actual variable and the external set of brackets belong to the tuple() constructor which allows it to be declared explicitly.
print(tuple6)