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]
如有侵权,请联系作者删除