python list
时间: 2023-11-04 10:06:25 浏览: 39
A list in Python is a collection of ordered and changeable elements, enclosed in square brackets []. Each element in the list is separated by a comma. Lists can contain any type of data, including numbers, strings, and even other lists.
Here is an example of a Python list:
fruits = ["apple", "banana", "cherry", "orange"]
This list contains four elements: "apple", "banana", "cherry", and "orange". We can access the elements in the list using their index, which starts at 0. For example, fruits[0] would return "apple".
We can also change the elements in the list by assigning a new value to their index. For example, fruits[1] = "kiwi" would change the second element in the list from "banana" to "kiwi".
We can add new elements to a list using the append() method. For example, fruits.append("pear") would add "pear" to the end of the list.
We can also remove elements from a list using the remove() method. For example, fruits.remove("cherry") would remove "cherry" from the list.
Lists can be used in many ways in Python, including as a way to store and organize data, to iterate through data using loops, and to manipulate data using list methods and functions.