DEV Community

Cover image for Python Programming for Beginners – Day 8
augustineowino357-design
augustineowino357-design

Posted on

Python Programming for Beginners – Day 8

Lists in Python

In the previous lesson, we learned about Functions and how they help organize and reuse code efficiently. Today, we will learn about Lists, one of the most commonly used data structures in Python.

Lists are important because they allow programmers to store multiple values in a single variable.


What is a List?

A List is a collection of multiple items stored in a single variable.

Lists are:

  • Ordered
  • Changeable (Mutable)
  • Allow duplicate values

Lists are created using square brackets "[]".

Example

languages = ["Python", "Java", "C++"]
Enter fullscreen mode Exit fullscreen mode

Accessing List Items

Each item in a list has an index number.

Python indexing starts from "0".

Example

languages = ["Python", "Java", "C++"]

print(languages[0])
print(languages[1])
Enter fullscreen mode Exit fullscreen mode

Output

Python
Java
Enter fullscreen mode Exit fullscreen mode

Negative Indexing

Python allows accessing items from the end using negative indexes.

Example

languages = ["Python", "Java", "C++"]

print(languages[-1])
Enter fullscreen mode Exit fullscreen mode

Output

C++
Enter fullscreen mode Exit fullscreen mode

Changing List Items

Lists are mutable, meaning their values can be changed.

Example

languages = ["Python", "Java", "C++"]

languages[1] = "JavaScript"

print(languages)
Enter fullscreen mode Exit fullscreen mode

Output

['Python', 'JavaScript', 'C++']
Enter fullscreen mode Exit fullscreen mode

Adding Items to a List

Python provides several methods for adding items.


append()

The "append()" method adds an item to the end of the list.

Example

languages = ["Python", "Java"]

languages.append("C++")

print(languages)
Enter fullscreen mode Exit fullscreen mode

Output

['Python', 'Java', 'C++']
Enter fullscreen mode Exit fullscreen mode

insert()

The "insert()" method adds an item at a specific position.

Example

languages = ["Python", "Java"]

languages.insert(1, "C++")

print(languages)
Enter fullscreen mode Exit fullscreen mode

Output

['Python', 'C++', 'Java']
Enter fullscreen mode Exit fullscreen mode

Removing Items from a List

Python provides different methods to remove items.


remove()

The "remove()" method removes a specific item.

Example

languages = ["Python", "Java", "C++"]

languages.remove("Java")

print(languages)
Enter fullscreen mode Exit fullscreen mode

Output

['Python', 'C++']
Enter fullscreen mode Exit fullscreen mode

pop()

The "pop()" method removes an item using its index.

Example

languages = ["Python", "Java", "C++"]

languages.pop(1)

print(languages)
Enter fullscreen mode Exit fullscreen mode

Output

['Python', 'C++']
Enter fullscreen mode Exit fullscreen mode

Looping Through a List

Lists can be accessed using loops.

Example

languages = ["Python", "Java", "C++"]

for language in languages:
    print(language)
Enter fullscreen mode Exit fullscreen mode

Output

Python
Java
C++
Enter fullscreen mode Exit fullscreen mode

List Length

The "len()" function returns the number of items in a list.

Example

languages = ["Python", "Java", "C++"]

print(len(languages))
Enter fullscreen mode Exit fullscreen mode

Output

3
Enter fullscreen mode Exit fullscreen mode

List Slicing

List Slicing allows accessing a range of items.

Example

numbers = [10, 20, 30, 40, 50]

print(numbers[1:4])
Enter fullscreen mode Exit fullscreen mode

Output

[20, 30, 40]
Enter fullscreen mode Exit fullscreen mode

Sorting Lists

The "sort()" method arranges list items in ascending order.

Example

numbers = [5, 2, 8, 1]

numbers.sort()

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output

[1, 2, 5, 8]
Enter fullscreen mode Exit fullscreen mode

Reversing Lists

The "reverse()" method reverses the order of list items.

Example

numbers = [1, 2, 3, 4]

numbers.reverse()

print(numbers)
Enter fullscreen mode Exit fullscreen mode

Output

[4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

Nested Lists

A Nested List is a list inside another list.

Example

students = [
    ["Augustine", 21],
    ["Brian", 22]
]

print(students[0])
Enter fullscreen mode Exit fullscreen mode

Output

['Augustine', 21]
Enter fullscreen mode Exit fullscreen mode

Why Lists are Important

Lists help programmers:

  • Store multiple values
  • Organize data
  • Process collections of items
  • Build dynamic applications
  • Work with loops efficiently

Lists are widely used in real-world applications and databases.


Conclusion

Lists are one of the most useful and powerful data structures in Python programming. They allow storing, modifying, and managing collections of data efficiently.

Understanding Lists is important because they are heavily used in Python applications, data analysis, automation, and software development.

Practice creating and manipulating lists to strengthen your Python programming skills.

Python #PythonForBeginners #CodingJourney #LearnPython #Programming

Top comments (0)