官术网_书友最值得收藏!

Understanding lists

Python supports a data structure called list, which is a mutable and ordered sequence of elements. Each element in that list is called as item. Lists are defined by inserting values between square brackets [ ]. Each element of list is given a number, which we call as a position or index. The index starts from zero; that is, the first index is zero, the second index is 1, and so on. We can perform the following operations on lists: indexing, slicing, adding, multiplying, and checking for membership.

Python's built-in length function returns the length of that list. Python also has function for finding the largest and smallest item of list. Lists can be numbered lists, string lists, or mixed list.

The following is the code for creating a list:

l = list()
numbers = [10, 20, 30, 40]

animals = ['Dog', 'Tiger', 'Lion']
list1 = ['John', 5.5, 500, [110, 450]]

Here, we've created three lists: the first is numbers, the second is animals, and the third is list1. A list within another list is called as nested list. Our list1 is a nested list. A list containing no elements is called an empty list; you can create one with empty brackets, [].

As you might expect, you can assign list values to variables:

>>> cities = ['Mumbai', 'Pune', 'Chennai']
>>> numbers_list = [75, 857]
>>> empty_list = []
>>> print (cities, numbers_list, empty_list)
['Mumbai', 'Pune', 'Chennai'] [75, 857] []
主站蜘蛛池模板: 万山特区| 广河县| 安化县| 丁青县| 百色市| 长春市| 县级市| 屯留县| 南宁市| 乃东县| 女性| 都匀市| 洛宁县| 常山县| 东方市| 图们市| 清镇市| 河北区| 连云港市| 霍山县| 色达县| 玛多县| 金山区| 永康市| 滨州市| 珠海市| 鲜城| 兰州市| 彰化县| 磐安县| 深州市| 左云县| 林州市| 莒南县| 吴旗县| 桃园市| 海安县| 涟水县| 乐山市| 措勤县| 克什克腾旗|