A list is a collection of items that are ordered and changeable. In Python,
a list is created by enclosing a comma-separated sequence of items in square
brackets [].
For example, you can create a list of integers as follows:
my_list =
[1, 2, 3, 4, 5]
print(my_list) # [1, 2, 3, 4, 5]
You can also create a list of strings:
my_list =
['apple', 'banana', 'cherry']
A list can contain items of different types, such as integers, strings, and
even other lists:
my_list =
[1, 'apple', [3, 4, 5]]
You can access the items in a list by referring to their index number, which
starts at 0 for the first item. For example, to access the first item in the
list above, you can use the following code:
first_item = my_list
[0]
print(first_item) # 1
You can also use negative indices to access items from the end of the list.
For example, the index -1 refers to the last item, -2 refers to the second last
item, and so on.
My_list = [44,55,66,77]
last_item = my_list
[-1]
print(last_item) #77
second_last_item = my_list
[-2]
print(second_last_item
) # 66
You can use the len()
function to find the number of items in a list. For example:
My_list = [1,2,3,4,5,6]
num_items =
len(my_list)
print(num_items) # 6
You can modify the items in a list by assigning a new value to them. For
example, to change the second item in the list to 'orange', you can use the
following code:
My_list = [“red”,”green”,”blue”]
my_list[
1] =
'orange'
print(my_list) # [“red”,”orange”,”blue”]
You can add new items to a list by using the append()
method. For example, to add a new item to the
end of the list, you can use the following code:
my_list.
append(
'black')
print(my_list) # [“red”,”orange”,”blue”,”black”]
You can also insert an item at a specific position using the insert()
method. For example, to insert
a new item at the second position, you can use the following code:
my_list.
insert(
1,
'yellow')
print(my_list) # [“red”,”yellow”,”orange”,”blue”,”black”]
To remove an item from a list, you can use the remove()
method and specify the value of the item to be
removed. For example, to remove the item 'apple' from the list, you can use the
following code:
my_list.
remove(
'black')
print(my_list) # [“red”,”yellow”,”orange”,”blue”]
You can also remove an item by its index using the pop()
method. For example, to remove the
last item from the list, you can use the following code:
my_list
.pop()
print(my_list) # [“red”,”yellow”,”orange”]
You can also use the pop()
method to remove an item at a specific position by specifying its index. For
example, to remove the second item from the list, you can use the following
code:
my_list
.pop(
1)
print(my_list) # [“red”,”orange”]
You can use the sort()
method to sort the items in a list in ascending order. For example:
my_list = [7,1,6,4]
my_list.
sort()
print(my_list) # [1,4,6,7]
Amelioration
This
article was researched and written with the help of ChatGPT, a language
model developed by OpenAI."
"Special thanks to ChatGPT for
providing valuable information and examples used in this article."
No comments:
Post a Comment