0
0
Pythonprogramming~5 mins

List creation and representation in Python

Choose your learning style9 modes available
Introduction

Lists help you keep many items together in one place. You can easily add, remove, or look at items in a list.

When you want to store a group of friends' names.
When you need to keep track of daily tasks.
When you want to save multiple numbers for math calculations.
When you want to collect answers from a quiz.
When you want to remember items in a shopping cart.
Syntax
Python
my_list = [item1, item2, item3]

# Example:
numbers = [1, 2, 3, 4]
words = ['apple', 'banana', 'cherry']

Lists are written inside square brackets [].

Items inside a list can be of any type: numbers, words, or even mixed.

Examples
This creates an empty list with no items.
Python
empty_list = []
print(empty_list)
A list with just one item still needs the square brackets.
Python
single_item_list = [42]
print(single_item_list)
Lists can hold different types of items together.
Python
mixed_list = [1, 'hello', 3.14, True]
print(mixed_list)
Lists can contain other lists inside them.
Python
nested_list = [[1, 2], [3, 4]]
print(nested_list)
Sample Program

This program shows how to create a list, add an item, and print items from it.

Python
def main():
    # Create a list of fruits
    fruits = ['apple', 'banana', 'cherry']
    print('Original list:', fruits)

    # Add a new fruit
    fruits.append('date')
    print('After adding date:', fruits)

    # Show the first fruit
    print('First fruit:', fruits[0])

    # Show the whole list
    print('Complete list:', fruits)

if __name__ == '__main__':
    main()
OutputSuccess
Important Notes

Creating a list is very fast and uses space proportional to the number of items.

Lists keep the order of items, so the first item you add stays first unless you change it.

Common mistake: forgetting the square brackets and trying to create a list like my_list = 1, 2, 3 which creates a tuple, not a list.

Summary

Lists store multiple items in one variable using square brackets.

You can create empty lists, single-item lists, or lists with many items.

Lists keep the order of items and can hold different types together.