As opposed to data types such as int, bool, float, str, a list is a compound data type where you can group values together. In Python, it would be inconvenient to create a new python variable for each data point you collected. What you can do instead is, store all this information in a Python list. You can build such a list with square brackets.
Building a List
Suppose you asked your two sisters and parents for their height, in meters. You can build the list as follows:
Of course, all this data can be referenced to with a variable. Simply, put the variable name and the equal sign in front, like here.
fam = [1.73, 1.68, 1.71, 1.89]
fam
A list is a way to give a single name to a collection of values. These values or elements can have any data type; int, float, etc., and also more advanced Python types, even other lists.
Storing Variables in Lists
It is also possible for a list to contain different types within it as well. Suppose you wanted to add the names of your parents and sisters to the list so you know which heights belong to whom. You can throw in some strings without issue.
fam = ["liz", 1.73, "emma", 1.68, "mom", 1.71, "dad", 1.89]
fam
['liz', 1.73, 'emma', 1.68, 'mom', 1.71, 'dad', 1.89]
As opposed to data types such as
int,bool,float,str, a list is a compound data type where you can group values together. In Python, it would be inconvenient to create a new python variable for each data point you collected. What you can do instead is, store all this information in a Python list. You can build such a list with square brackets.Building a List
Suppose you asked your two sisters and parents for their height, in meters. You can build the list as follows:
Of course, all this data can be referenced to with a variable. Simply, put the variable name and the equal sign in front, like here.
A list is a way to give a single name to a collection of values. These values or elements can have any data type; int, float, etc., and also more advanced Python types, even other lists.
Storing Variables in Lists
It is also possible for a list to contain different types within it as well. Suppose you wanted to add the names of your parents and sisters to the list so you know which heights belong to whom. You can throw in some strings without issue.