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
EasyCreate 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input11Lists Basics
Declaring a ListAccessing List ElementsModifying ListsList MethodsRecap - Product ListRecap - Reversed ListTuple3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2