How to use tiles? - Slice | Python

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]

如有侵权,请联系作者删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小饅頭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值