Menu
Coddy logo textTech

Declaring a List

Part of the Fundamentals section of Coddy's Python journey — lesson 53 of 77.

A list is a collection of items, and it can contain values of different types, such as numbers, strings, or even other lists. Lists are created using square brackets [], and the items inside the list are separated with commas.

Here is an example of how to create a list:

my_list = [1, 2, "three", True]

To check the length of the list, we can use the len() function:

length = len(my_list)

The variable length will hold 4 because there are 4 elements in the list.

challenge icon

Challenge

Easy

Create a list called shopping_list that contains the following items: bread, eggs, milk, and butter.

Cheat sheet

Lists in Python:

  • Created using square brackets []
  • Items separated by commas
  • Can contain different data types
my_list = [1, 2, "three", True]

Get list length:

length = len(my_list)

Try it yourself

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals