Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24361

Python Morsels: What are lists in Python?

$
0
0

Lists are used to store and manipulate an ordered collection of things.

Lists are ordered collections

This is a list:

>>> colors=["purple","green","blue","yellow"]

We can prove that to ourselves by passing that object to Python's built-in type function:

>>> type(colors)<class 'list'>

Lists are ordered collections of things.

We can create a new list by using square brackets ([]), and inside those square brackets, we put each of the items that we'd like our list to contain, separated by commas:

>>> numbers=[2,1,3,4,7,11]>>> numbers[2, 1, 3, 4, 7, 11]

Lists can contain any type of object. Each item in a list doesn't need to be of the same type, but in practice, they typically are.

So we might refer to this as a list of strings:

>>> colors=["purple","green","blue","yellow"]

While this is a list of numbers:

>>> numbers=[2,1,3,4,7,11]

Containment checking

We can check whether a …

Read the full article: https://www.pythonmorsels.com/what-are-lists/


Viewing all articles
Browse latest Browse all 24361

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>