Home > Backend Development > Python Tutorial > Python variable types-list operations and interceptions (practical combat and analysis)

Python variable types-list operations and interceptions (practical combat and analysis)

乌拉乌拉~
Release: 2018-08-13 15:35:04
Original
2760 people have browsed it

List (List) is the most frequently used data type in Python. Lists can complete the data structure implementation of most collection classes. It supports characters, numbers, strings and can even contain lists (i.e. nested). Lists are marked with [ ] and are the most common composite data type in Python.

The variable [head subscript: tail subscript] can also be used to cut the values ​​in the list, and the corresponding list can be intercepted. The index from left to right starts with 0 by default, and the index from right to left. The default is -1 to start, and the subscript can be empty to indicate getting to the beginning or end.

Python variable types-list operations and interceptions (practical combat and analysis)

#For specific application methods, please refer to the picture above.

The same list also has the and * sign, is the list connection operator, and the asterisk * is the repeat operation. The following example:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
 
print list               # 输出完整列表
print list[0]            # 输出列表的第一个元素
print list[1:3]          # 输出第二个至第三个元素 
print list[2:]           # 输出从第三个开始至列表末尾的所有元素
print tinylist * 2       # 输出列表两次
print list + tinylist    # 打印组合的
Copy after login

The result of the example is as follows:

['runoob', 786, 2.23, 'john', 70.2]
runoob
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['runoob', 786, 2.23, 'john', 70.2, 123, 'john']
Copy after login

The above content is a list type among the standard numeric types in python. This part is not difficult to understand. Use The method is not difficult to understand. I hope this article can be helpful to you who are learning python.

The above is the detailed content of Python variable types-list operations and interceptions (practical combat and analysis). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template