List of Values
Learn and practice manipulating lists of values in Python.
What is a list?
In Python, a list is a comma-separated collection of values enclosed in square brackets and stored as a single variable. For example, ["apple","mango","banana"]
is a list of strings. It can be stored as a variable just like any other data type.
The following program demonstrates how to create a list of string values:
listOfFruits = ["apple","mango","banana"] # Creating a list of stringsprint(listOfFruits)
In the code above:
- We use a variable,
listOfFruits
, to assign the comma-separated values enclosed in square brackets. - We call the variable,
listOfFruits
, a list.
The following program demonstrates the various types of lists in Python:
list0 = []print("Empty list: ")print(list0)list1 = [100]print("\nSingle-valued list: ")print(list1)list2 = [7, 7, 4, 4, 3, 3, 3, 6, 5]print("\nList having duplicate Numbers: ")print(list2)list3 = [50, 'Learn', 10, 'To', 2.5, -1, 'Code']print("\nList with the use of mixed values: ")print(list3)
Hint: The
\n
written as part of the string in the
There are a few new things we have to understand in the code above.
-
list0
,list1
,list2
,list3
,list4
, andlist5
are the variables storing list values. -
A list that has no values is an empty or a blank list (
list0
...