How to use tiles? - Slice | Python

简介: The Code method from Python book (2th Edition) on page 41 Example 2.1.6

The Code method from Python book (2th Edition) on page 41 Example 2.1.6


Grammar Description

Python slice operation support for string, list and tupts.


#### Format: How to use?

Variable name[ Start : End : Step ]

Disxription

  • Ignore "Start" --- Default start with the subscript 0.
  • Ignore "End" --- Default the end of the last character.
  • Ignore "Step" --- Default step is 1.
  • Ignore all items -- Default get all value of this Variable, But must keep : .

Code Practice

#### 1. Defined list of number

number = [0, 1, 2, 3, 4, 5, 6, 7]

#### 2. Get character from number under identity 0 to 2

print(number[0:3]) 

Compilation results:

[0, 1, 2]

#### 3. Get character from subscript 2 to the end

print(number[2:]) 

Compilation results:

[2, 3, 4, 5, 6, 7]

#### 4. Get character from subscript is 0 to the when subscript is 1 end

print(number[:2])

Compilation results:

[0, 1]

#### 5. Get all value of list number

print(number[:])

Compilation results:

[0, 1, 2, 3, 4, 5, 6, 7]

#### 6. From start to finish, get character when step length is 2

print(number[::2])

Compilation results:

[0, 2, 4, 6]

#### 7. Get character from subscript 1 begin to the penultimate ( subscript is -1 )

print(number[1:-1])

Compilation results:

[1, 2, 3, 4, 5, 6]

#### 8. Get character from subscript 0 to 5 and step length is 3

print(number[0:6:3])

Compilation results:

[0, 3]
如有侵权,请联系作者删除
目录
相关文章
|
Python Windows
【错误记录】Mac 中 Python 报错 ( ERROR: Could not build wheels for numpy which use PEP 517 | 问题未解决 | 问题记录 )(一)
【错误记录】Mac 中 Python 报错 ( ERROR: Could not build wheels for numpy which use PEP 517 | 问题未解决 | 问题记录 )(一)
1976 0
【错误记录】Mac 中 Python 报错 ( ERROR: Could not build wheels for numpy which use PEP 517 | 问题未解决 | 问题记录 )(一)
|
5月前
|
自然语言处理 Python
【Python】已解决:Resource punkt not found. Please use the NLTK Downloader to obtain the resource:
【Python】已解决:Resource punkt not found. Please use the NLTK Downloader to obtain the resource:
423 1
|
5月前
|
自然语言处理 Java 开发工具
【Python】已解决Resource averaged_perceptron_tagger not found. Please use the NLTK Downloader to obtain t
【Python】已解决Resource averaged_perceptron_tagger not found. Please use the NLTK Downloader to obtain t
200 0
|
5月前
|
自然语言处理 Python
【Python】已解决:Resource stopwords not found. Please use the NLTK Downloader to obtain the resource:
【Python】已解决:Resource stopwords not found. Please use the NLTK Downloader to obtain the resource:
247 0
|
5月前
|
监控 开发者 Python
【Python】已解决:WARNING: This is a development server. Do not use it in a production deployment. Use a p
【Python】已解决:WARNING: This is a development server. Do not use it in a production deployment. Use a p
874 0
|
7月前
|
计算机视觉 Python
ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly
ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly
152 2
|
Python
Python报错ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Python报错ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
1622 1
|
7月前
|
关系型数据库 Python
Python小姿势 - Python Tips: How to Use Context Managers
Python小姿势 - Python Tips: How to Use Context Managers
|
Python
Python 缩进问题-inconsistent use of tabs and spaces in indentation.原因及解决方法
Python 缩进问题-inconsistent use of tabs and spaces in indentation.原因及解决方法
6139 0
Python 缩进问题-inconsistent use of tabs and spaces in indentation.原因及解决方法
|
编解码 JSON 数据格式
Python常见问题 - requests请求参数包含中文报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: 小明 is not valid Latin-1. Use body.encode('utf-8')
Python常见问题 - requests请求参数包含中文报错:UnicodeEncodeError: 'latin-1' codec can't encode characters in position 13-14: 小明 is not valid Latin-1. Use body.encode('utf-8')
1932 0