List Operations

369 Tesla - Kalviyogi Nagarajan

What Are Lists?

Lists in Python are like super-powered containers that can hold multiple items at once! Think of them as a smart shopping list that can store anything - numbers, text, or even other lists.

In everyday life, lists are like your playlist of favorite songs, your collection of toys, or a list of ingredients for your favorite snack!

Key Features of Lists

  • 1

    Ordered:

    The order of items is preserved, just like beads on a string.

  • 2

    Mutable:

    Unlike strings, lists can be changed after creation! You can add, remove, or modify items.

  • 3

    Heterogeneous:

    Lists can contain different types of items - numbers, strings, and even other lists!

Examples

# A list of numbersnumbers = [369, 630, 999]# A mixed data type listmixed_list = [369, "Tesla", 3.14]

What's happening here?

In these examples, we've created different types of lists. The first contains only numbers, while the second contains a number, a string, and a decimal number - all in one list!

Lists vs. Strings

Lists

[
3
6
9
,
6
3
0
,
9
9
9
]
  • Mutable (can be changed)
  • Can contain different data types
  • Enclosed in square brackets []
  • Items separated by commas

Strings

"
T
e
s
l
a
"
  • Immutable (cannot be changed)
  • Only contains text characters
  • Enclosed in quotes " or '
  • Each character has an index

Remember: Both lists and strings can be indexed and sliced in the same way!my_list[0] andmy_string[0] both give you the first element.