In Pytho2
In Pytho2
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.
1. Ordered: The items have a defined order, which will not change
unless explicitly modified (e.g., through sorting or reversing).
4. Indexed: Elements in a list are indexed, with the first element having
an index of 0, the second 1, and so on. Negative indexing starts from -
1 for the last item.
List Creation:
You can create a list using square brackets or the list() constructor.
python
Copy code
my_list = [1, 2, 3, 'hello', [4, 5]] # List with integers, a string, and a nested
list
python
Copy code
Modifying Lists:
Since lists are mutable, you can change their elements, append new items,
or remove them.
Changing elements:
python
Copy code
Adding elements:
python
Copy code
Removing elements:
python
Copy code
python
Copy code
my_list.append(10)
python
Copy code
my_list.insert(2, 'new') # Insert 'new' at index 2
python
Copy code
my_list.remove(3)
python
Copy code
python
Copy code
len(my_list)
6. Sorting a list: sort() sorts the list in ascending order (for comparable
elements).
python
Copy code
my_list.sort() # Only works if all elements are of the same type (e.g.,
numbers or strings)
Example:
python
Copy code
# Creating a list
fruits.append('orange')
fruits.remove('banana')
Summary:
A list in Python is a versatile and powerful collection type that can store a
sequence of elements. Its ability to handle different data types and allow for
various operations (like adding, removing, and modifying items) makes it a
fundamental data structure in Python.