Working with Lists
In this section, you will learn about lists in Python. Lists are ordered, mutable collections of elements. We will cover topics such as creating lists, accessing elements in a list, list methods, list comprehension, and common operations performed on lists.
Creating Lists
In Python, you can create lists by enclosing elements in square brackets ([]). Here are some examples:
fruits = ["apple", "banana", "cherry"] numbers = [1, 2, 3, 4, 5] mixed = [1, "apple", True]
Accessing Elements in a List
You can access individual elements in a list using indexing. Python uses zero-based indexing, where the first element is at index 0. Here’s an example:
fruits = ["apple", "banana", "cherry"] print(fruits[0]) # Output: apple print(fruits[1]) # Output: banana
List Methods
Python provides many built-in methods to manipulate lists. Here are a few commonly used list methods:
fruits = ["apple", "banana", "cherry"] fruits.append("orange") # Add element to the end fruits.insert(1, "mango") # Insert element at specific index fruits.remove("banana") # Remove element by value fruits.pop(2) # Remove element by index fruits.sort() # Sort elements in ascending order fruits.reverse() # Reverse the order of elements fruit_count = fruits.count("apple") # Count occurrences of an element print(fruits) # Output: ['apple', 'mango', 'cherry', 'orange'] print(fruit_count) # Output: 1
List Comprehension
List comprehension is a concise way to create lists based on existing lists or other iterable objects. Here’s an example:
numbers = [1, 2, 3, 4, 5] squared_numbers = [num ** 2 for num in numbers] print(squared_numbers) # Output: [1, 4, 9, 16, 25]
Common List Operations
Here are some common operations performed on lists:
fruits = ["apple", "banana", "cherry"] length = len(fruits) # Get the length of the list index = fruits.index("banana") # Get the index of an element is_present = "apple" in fruits # Check if an element is present is_empty = len(fruits) == 0 # Check if the list is empty sliced = fruits[1:3] # Slice a portion of the list print(length) # Output: 3 print(index) # Output: 1 print(is_present) # Output: True print(is_empty) # Output: False print(sliced) # Output: ['banana', 'cherry']
Working with Tuples
In this section, you will learn about tuples in Python. Tuples are ordered, immutable collections of elements. We will cover topics such as creating tuples, accessing elements in a tuple, tuple packing and unpacking, tuple methods, and the differences between tuples and lists.
Creating Tuples
In Python, you can create tuples by enclosing elements in parentheses (). Here are some examples:
fruits = ("apple", "banana", "cherry") numbers = (1, 2, 3, 4, 5) mixed = (1, "apple", True)
Accessing Elements in a Tuple
You can access individual elements in a tuple using indexing. Python uses zero-based indexing, where the first element is at index 0. Here’s an example:
fruits = ("apple", "banana", "cherry") print(fruits[0]) # Output: apple print(fruits[1]) # Output: banana
Tuple Packing and Unpacking
Tuple packing is the process of creating a tuple by assigning values to it. Tuple unpacking is the process of extracting values from a tuple into separate variables. Here’s an example:
coordinates = 3, 4 x, y = coordinates print(x) # Output: 3 print(y) # Output: 4
Tuple Methods
Python provides a few built-in methods that can be used with tuples. Here are a couple of examples:
fruits = ("apple", "banana", "cherry") print(len(fruits)) # Output: 3 print(fruits.index("banana")) # Output: 1
Differences Between Tuples and Lists
While tuples and lists are both used to store collections of elements, they have a few differences. Tuples are immutable, meaning their elements cannot be modified once the tuple is created. Lists, on the other hand, are mutable and allow modifications. Tuples are typically used for fixed collections of related values, while lists are used for collections that can change in size or need to be modified frequently.