3.9.1 - Lists in Python

# Python List Examples

a = [] #Empty List

b = [0, 1, 2, 3, 4] #List with 5 items

c = [3.14, "abc", [1, 2, 3, 4]]  #List with a nested list

d = list('Python')  # List of iterable items

e = list(range(0, 10))  # List of integers from 0-9

len(b)
len(c[2])

b + c
c + b

b * 4


3.14 in c

b[2] = 9 # mutable


temp_string = "Python"

temp_string[2] = "a" # error inmutable


c[2][0]

c[2][-1]


b[1:]
b[:2]
b[1:3]



运行结果如下:

a = [] #Empty List

b = [0, 1, 2, 3, 4] #List with 5 items

c = [3.14, "abc", [1, 2, 3, 4]] #List with a nested list

d = list('Python') # List of iterable items

d

// Result: ['P', 'y', 't', 'h', 'o', 'n'] //

e = list(range(0, 10)) # List of integers from 0-9

e

// Result: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] //

len(b)

// Result: 5 //

len(c[2])

// Result: 4 //

b + c

// Result: [0, 1, 2, 3, 4, 3.14, 'abc', [1, 2, 3, 4]] //

c + b

// Result: [3.14, 'abc', [1, 2, 3, 4], 0, 1, 2, 3, 4] //

b * 4

// Result: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] //

3.14 in c

// Result: True //

b[2] = 9 # mutable

b

// Result: [0, 1, 9, 3, 4] //

c[2][0]

// Result: 1 //

c[2][-1]

// Result: 4 //

b[1:]

// Result: [1, 9, 3, 4] //

b[:2]

// Result: [0, 1] //

b[1:3]

// Result: [1, 9] //



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值