List of Values
Explore how to create and use lists in Python, accessing values through positive and negative indices. Learn slicing techniques to extract subsets and practice list operations with example programs to build foundational skills in Python list manipulation.
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:
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:
Hint: The
\nwritten 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, andlist5are the variables storing list values. -
A list that has no values is an empty or a blank list (
list0). -
A list can have only one value (
list1). -
The variable
list2holds a list of integers. The ...