List

What is a List?

A list is a Python data structure used to store multiple values in one variable. Lists:

  • Are ordered

  • Are changeable

  • Can hold different data types (e.g., strings, numbers)

  • Use square brackets.[]


Create List

name = ["KT", "Baz", "Sashi", "Samu"]
  • You created a list of strings.

  • name[0] β†’ "KT", name[1] β†’ "Baz", and so on.


print(name)              # Shows the whole list
print(type(name))        # Tells you it's a <class 'list'>
print(len(name))         # Counts the number of items (4)

Accessing List Items

Slicing Lists

Slicing is powerful when you want part of a list.


Adding an Item

  • Adds "Venu" to the end of the list.

  • Now the list is:["KT", "Baz", "Sashi", "Samu", "Venu"]


Removing an Item

  • Removes the first item (index 0), which is "KT".

  • Now the list becomes:["Baz", "Sashi", "Samu", "Venu"]


Why Use Lists?

  • To store and manage multiple values

  • To loop over elements and process them

  • To dynamically add, update, or remove items

  • Useful in real-world tasks like to-do lists, cart items, student records, etc.

Last updated