Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 8 Lists in

Updated
2 min read
S

Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey

Building skills in Python and full-stack development, with a focus on web apps and system design

Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering

From Python basics to full-stack projects, sharing my progress as I grow into a developer

Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development

01. What is a List?

  • A list is a data structure that stores multiple values in order.

  • Use square brackets [ ] and separate values with commas.

fruits = ["Apple", "Banana", "Grape"]
numbers = [10, 20, 30, 40]
mixed = [1, "Hello", True, 3.14]

👉 Key features:

  • Ordered (index starts at 0)

  • Mutable (values can be changed)


02. Indexing and Slicing

fruits = ["Apple", "Banana", "Grape", "Strawberry"]

print(fruits[0])    # Apple
print(fruits[2])    # Grape
print(fruits[-1])   # Strawberry (last element)

print(fruits[1:3])  # ['Banana', 'Grape'] (index 1 to 2)
print(fruits[:2])   # ['Apple', 'Banana']
print(fruits[2:])   # ['Grape', 'Strawberry']

03. Modifying Lists

fruits = ["Apple", "Banana", "Grape"]

fruits[1] = "Mango"     # Change value
print(fruits)           # ['Apple', 'Mango', 'Grape']

04. Common List Methods

numbers = [3, 1, 4]

numbers.append(5)       # Add to the end
print(numbers)          # [3, 1, 4, 5]

numbers.insert(1, 100)  # Insert at index 1
print(numbers)          # [3, 100, 1, 4, 5]

numbers.remove(1)       # Remove the value 1
print(numbers)          # [3, 100, 4, 5]

numbers.pop()           # Remove the last element
print(numbers)          # [3, 100, 4]

print(numbers.index(100))   # 1 (index of the value)
print(numbers.count(4))     # 1 (count of the value)

numbers.sort()          # Sort ascending
print(numbers)          # [3, 4, 100]

numbers.reverse()       # Reverse the order
print(numbers)          # [100, 4, 3]

05. Lists with Loops

fruits = ["Apple", "Banana", "Grape"]

for f in fruits:
    print(f)

# Print index and value together
for i, f in enumerate(fruits):
    print(i, f)

06. 2D Lists

A list can contain other lists (useful for matrices or tables).

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

print(matrix[0][1])  # 2 (row 0, column 1)
print(matrix[2][2])  # 9

07. Practice Examples

Example 1: Replace an Item

fruits = ["Apple", "Banana", "Grape"]
fruits[1] = "Strawberry"
print(fruits)

Example 2: Sum of Numbers

numbers = [10, 20, 30, 40]
total = 0
for n in numbers:
    total += n
print("Sum:", total)

Example 3: Find Maximum and Minimum

numbers = [3, 7, 2, 9, 5]
print("Maximum:", max(numbers))
print("Minimum:", min(numbers))

Python Basics

Part 8 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 9 Strings in Python

01. What is a String? A string is a sequence of characters (text data). Defined using either single quotes ' ' or double quotes " ". text1 = "Hello" text2 = 'Python' print(text1, text2) 02. Indexing & Slicing Strings Strings are ordered data t...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.