Arrays in Python
In Python, arrays
are more commonly known as lists
. In lists, elements are separated by a comma (,)
. They can also store elements of different data types. This means that a Python list can store a string, int, or even another list within a single list!
Now let's consider an example. Suppose we have a list of names to store in the list. The list will be initialized by giving it a name (like variables) and specifying all elements in the list separated by a comma.
PYTHON
1name = ["Alice", "Mona", "Bob", "Barbara"]
Suppose you also want to store the corresponding ages of people in the list. You can create a list with elements of two data types (string and int) like this.
PYTHON
1name = ["Alice", 12, "Mona", 23, "Bob", 5, "Barbara", 19]