Table of contents | |
Introduction | |
Creating Lists | |
Accessing List Elements | |
Modifying List Elements | |
List Methods |
Lists are one of the most commonly used data structures in Python. They allow you to store and organize multiple values in a single variable. Lists are versatile and provide several built-in methods to manipulate and access their elements. This article will introduce you to the concept of lists in Python and provide simple code examples to help you understand their usage.
You can create a list in Python by enclosing comma-separated values within square brackets '[ ]'. Here's an example:
fruits = ['apple', 'banana', 'orange', 'grape']
In the example above, we created a list named 'fruits' that contains four string values. Lists can hold values of any data type, including numbers, strings, and even other lists.
You can access individual elements in a list using their index. In Python, list indices start from '0' for the first element and increment by '1' for each subsequent element. Here's how you can access elements in a list:
fruits = ['apple', 'banana', 'orange', 'grape']
print(fruits[0]) # Output: apple
print(fruits[2]) # Output: orange
In the example above, 'fruits[0]' returns the first element of the list ('apple'), and 'fruits[2]' returns the third element ('orange').
Lists are mutable, which means you can modify their elements by assigning new values to specific indices. Here's an example:
fruits = ['apple', 'banana', 'orange', 'grape']
fruits[1] = 'pear'
print(fruits) # Output: ['apple', 'pear', 'orange', 'grape']
In the example above, we modified the second element of the 'fruits' list from 'banana' to 'pear'. The 'print()' function displays the updated list.
Let's see these methods in action:
fruits = ['apple', 'banana', 'orange']
fruits.append('grape')
print(fruits) # Output: ['apple', 'banana', 'orange', 'grape']
fruits.insert(1, 'pear')
print(fruits) # Output: ['apple', 'pear', 'banana', 'orange', 'grape']
fruits.remove('banana')
print(fruits) # Output: ['apple', 'pear', 'orange', 'grape']
removed_fruit = fruits.pop(2)
print(removed_fruit) # Output: orange
print(fruits) # Output: ['apple', 'pear', 'grape']
print(len(fruits)) # Output: 3
Problem 1: Create a list called 'numbers' that contains the integers from 1 to 5. Print the second element of the list.
numbers = [1, 2, 3, 4, 5]
print(numbers[1]) # Output: 2
Problem 2: Add the number 6 to the end of the 'numbers' list. Print the updated list.
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
print(numbers) # Output: [1, 2, 3, 4, 5, 6]
Problem 3: Remove the number 3 from the 'numbers' list. Print the updated list.
numbers = [1, 2, 3, 4, 5]
numbers.remove(3)
print(numbers) # Output: [1, 2, 4, 5]
Problem 4: Create a list called 'fruits' that contains the strings 'apple', 'banana', and 'orange'. Replace the second element with 'grape'. Print the updated list.
fruits = ['apple', 'banana', 'orange']
fruits[1] = 'grape'
print(fruits) # Output: ['apple', 'grape', 'orange']
Lists are a fundamental data structure in Python that allow you to store and manipulate collections of values. Understanding how to create, access, and modify list elements, as well as use built-in methods, is essential for writing efficient and organized code. With the knowledge gained from this article, you can now confidently work with lists in Python.
49 videos|38 docs|18 tests
|
|
Explore Courses for EmSAT Achieve exam
|