Python Lists Explained: Objects, Data Types, and Nested Lists with Examples
A List Can Contain Integer Objects
A list can contain integer objects.
numbers = [10, 20, 30, 40, 50] print(numbers)
Output:
The list contains five integer objects.
A List Can Contain Float Objects
A list can also contain floating-point numbers.
temperatures = [25.5, 26.2, 27.8, 28.1, 29.4] print(temperatures)
Output:
A List Can Contain String Objects
A list can contain strings.
names = ["Ram", "John", "Ali", "Ravi", "David"] print(names)
Output:
Each name is a separate string object in the list.
A List Can Contain Boolean Objects
Lists can also contain Boolean objects.
values = [True, False, True, True, False] print(values)
Output:
The two Boolean values available in Python are:
A List Can Contain Different Types of Objects
One of the useful features of Python lists is that a single list can contain objects of different types.
For example:
data = [ 10, 3.14, "Python", True, 25 ] print(data)
Output:
Here the list contains:
A List Can Even Contain Other Lists
A list can contain another list as one of its objects.
For example:
numbers = [ [10, 20], [30, 40], [50, 60] ] print(numbers)
Output:
Here, the outer list contains three list objects.
For example:
matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(matrix)
Output:
We will study such nested lists separately.
Final Understanding
The most important thing to understand about a Python list is:
For example:
data = [10, 3.14, "Python", True]
The list contains four objects: