Learn Python: The Fundamentals

Now that you’ve worked with numbers and strings, it’s time to dive into one of Python’s most flexible tools — lists. Lists let you store multiple values together, whether it’s numbers, words, or even a mix of both. And the best part? You can change them anytime — add items, remove them, or rearrange the whole thing.

 

Creating Lists

Lists are written using square brackets [], and the items inside are separated by commas. You can keep it simple or mix things up — Python doesn’t mind if you combine different types.

 

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True, 3.14]
print(fruits)

 

Accessing Elements

Each item in a list has a position — starting at 0. You can grab items by index, slice sections, or even count backwards from the end using negative numbers.

 

fruits = ["apple", "banana", "cherry"]
print(fruits[0])     # 'apple'
print(fruits[-1])    # 'cherry'
print(fruits[0:2])  # ['apple', 'banana']

 

Adding and Removing Items

Lists aren’t set in stone. You can grow them using append() or insert(), and shrink them with remove() or pop(). This makes lists perfect for programs where things change over time.

 

fruits = ["apple", "banana"]
fruits.append("cherry")      # add at the end
fruits.insert(1, "orange")   # insert at index
fruits.remove("banana")      # remove by value
popped = fruits.pop()        # remove last item
print(fruits)

 

Looping Through Lists

Since lists usually contain more than one item, you’ll often need to go through them one by one. A for loop does just that — it helps you work with every item in the list.

 

fruits = ["apple", "orange", "cherry"]
for f in fruits:
    print(f)

 

Common List Functions

Python gives you built-in tools to get useful info from your lists. You can count how many items there are, find the total, or figure out which number is the biggest or smallest — all with just one word.

 

numbers = [10, 20, 30, 40]
print(len(numbers))       # length
print(sum(numbers))       # sum of elements
print(max(numbers))       # maximum
print(min(numbers))       # minimum

 

List Comprehensions

List comprehensions are a short and elegant way to create new lists. Instead of writing a loop, you can do the same thing in one clean line — whether you’re applying a rule, filtering values, or generating a pattern.

 

squares = [x**2 for x in range(5)]
print(squares)   # [0, 1, 4, 9, 16]

 

Interactive Exercise: Manage a Shopping List

Let’s put your list skills to the test. Add an item to your list, remove something, then print the list’s length and its contents.

 


shopping = ["bread", "milk", "eggs"]

# 1) Add "butter"
# 2) Remove "milk"
# 3) Print length of the list
# 4) Loop through and print all items

shopping.append("butter")
shopping.remove("milk")
print("Length:", len(shopping))

for item in shopping:
print(item)

Use .append(), .remove(), len(), and a for loop.

 

Interactive Exercise: Build a List of Even Numbers

Try creating a list of even numbers from 1 to 20 using a list comprehension. This shows how Python can filter values for you automatically.

 


# Use list comprehension and range
evens = [x for x in range(1, 21) if x % 2 == 0]
print(evens)

 
Remember x % 2 == 0 checks for even numbers.
 

Lists are one of the most useful tools in Python. Whether you’re working with data, organizing items, or just trying to keep track of things, mastering lists will make your programs far more powerful and flexible. Next up — we’ll explore dictionaries, which let you store information in pairs (like a name and phone number).