Python Tutorial: Lists, Dictionaries, and Tuples

Lists

Lists are ordered, mutable collections of items. They allow duplicate elements and support various operations like indexing, slicing, appending, and deleting elements.

# Creating a list
fruits = ['apple', 'banana', 'cherry']
print(fruits)

# Indexing from the beginning (0-based)
print(fruits[0])  # apple
print(fruits[1])  # banana
print(fruits[2])  # cherry

# Indexing from the end (-1-based)
print(fruits[-1])  # cherry
print(fruits[-2])  # banana
print(fruits[-3])  # apple
        

Output:

['apple', 'banana', 'cherry']
apple
banana
cherry
cherry
banana
apple
        

Q: What is the index of 'banana' in the list fruits?

A: The index of 'banana' is 1 when indexed from the beginning, and -2 when indexed from the end.

print(fruits[1])  # banana
print(fruits[-2])  # banana
            

Output:

banana
banana
            

Lists are mutable, meaning you can change their content without changing their identity.

# Appending an item to the list
fruits.append('orange')
print(fruits)
        

Output:

['apple', 'banana', 'cherry', 'orange']
        
# Deleting an item from the list
del fruits[1]
print(fruits)
        

Output:

['apple', 'cherry', 'orange']
        
# Altering an item in the list
fruits[1] = 'blueberry'
print(fruits)
        

Output:

['apple', 'blueberry', 'orange']
        

Q: How can you find the length of a list in Python?

A: You can find the length of a list using the len() function.

# Finding the length of the list
print(len(fruits))
            

Output:

3
            

Dictionaries

Dictionaries are unordered, mutable collections of key-value pairs. They are indexed by keys, which can be of any immutable type.

# Creating a dictionary
person = {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(person)
        

Output:

{'name': 'Alice', 'age': 25, 'city': 'New York'}
        

Dictionaries are mutable, so you can change, add, or remove items.

# Adding a key-value pair
person['email'] = 'alice@example.com'
print(person)
        

Output:

{'name': 'Alice', 'age': 25, 'city': 'New York', 'email': 'alice@example.com'}
        
# Deleting a key-value pair
del person['age']
print(person)
        

Output:

{'name': 'Alice', 'city': 'New York', 'email': 'alice@example.com'}
        
# Altering a value
person['city'] = 'Los Angeles'
print(person)
        

Output:

{'name': 'Alice', 'city': 'Los Angeles', 'email': 'alice@example.com'}
        

Q: In the dictionary person, which are the keys and which are the values?

A: In the dictionary person:

Q: How can you check if a key exists in a dictionary?

A: You can check if a key exists in a dictionary using the in keyword.

# Checking if a key exists
print('name' in person)
print('age' in person)
            

Output:

True
False
            

Tuples

Tuples are ordered, immutable collections of items. They allow duplicate elements and support indexing and slicing.

# Creating a tuple
colors = ('red', 'green', 'blue')
print(colors)
        

Output:

('red', 'green', 'blue')
        

Tuples are immutable, so you cannot change their content after creation.

# Accessing tuple elements
print(colors[1])
        

Output:

green
        

Q: How can you find the index of an element in a tuple?

A: You can find the index of an element in a tuple using the index() method.

# Finding the index of an element
print(colors.index('blue'))
            

Output:

2
            

Q: What happens if you try to alter a tuple?

A: Since tuples are immutable, attempting to alter their content will result in a TypeError.

# Attempting to alter a tuple
try:
    colors[1] = 'yellow'
except TypeError as e:
    print(e)
            

Output:

'tuple' object does not support item assignment