Course Content
Introduction to Python
Introduction to Python
Python Lists
A list is a data type that can store multiple items of various types, such as numbers, strings, and tuples.
To create a list, place comma-separated values inside square brackets []
or convert an iterable a string
, set
, or tuple
using list()
. You can create a list with details about the USA (country name, land area, and population):
# Create list US_Info = ["USA", 9629091, 331002651] # Printing list print(US_Info)
Accessing list elements is similar to accessing characters in a string—using indexing and slicing.
To retrieve the land area and population from US_Info
, target the second (index 1
) and third (index 2
) items. Use slicing with a colon :
to specify the start and end positions.
Note
The upper limit of a slice is excluded, so use
[1:3]
to retrieve these two elements.
# Getting area and population US_Info = ["USA", 9629091, 331002651] # Printing list print(US_Info[1:3])
Thanks for your feedback!