Python—列表比较、逻辑、连接、重复运算符

1. 比较运算符

  • 当列表中只有一个元素时,比较两个列表中元素即可,如:

>>>list1 = [123]

>>>list2 = [456]

>>>list1 < list2

Ture

  • 当列表中有多个元素时,比较索引号0位置上的元素,元素大的列表则该列表大;若list1[0]=list2[0],则比较下一个位置的元素。如:

>>>list1 = [123,456,789]

>>>list2 = [456,789]

>>>list1 > list2

False

>>>list1 = [123,456]

>>>list2 = [123,789]

>>>list1 < list2

扫描二维码关注公众号,回复: 12555767 查看本文章

Ture

2. 逻辑运算符

and

or

not

3. 连接运算符

+可以用来连接两个列表

>>>list1 = [123]

>>>list2 = ['hello world']

>>>list3 = list1 + list2

>>>print(list3)

[123, 'hello world']

但是不能使用+向列表中添加元素,会报错,如:

>>>list3+"中国"

TypeError: can only concatenate list (not "str") to list

4. 重复操作符

>>> list1=[123]

>>> list2=list1*10

>>> list2

[123, 123, 123, 123, 123, 123, 123, 123, 123, 123]

猜你喜欢

转载自blog.csdn.net/weixin_43217427/article/details/107400259