首页 > 脚本编程 > python学习(二)
您的足迹
  • 你没有浏览过任何文章或者你没有开启cookies。

python学习(二)

1.列表

>>> family = [‘mom’,’dad’,’sister’]

>>> family[1]’dad

‘>>> family[-2]’dad’

从右往左从0开始,从左往右数从-1开始的哦。

2.添加删除等

>>> family = ['mom','dad','sister']
>>> family[1]
'dad'
>>> family[-2]
'dad'
>>> expample = [0,1,2,3,4,5,6,7,8,9]

>>> expample[4:7]
[4, 5, 6]
>>> expample[4:19]
[4, 5, 6, 7, 8, 9]
>>> expample[4:]
[4, 5, 6, 7, 8, 9]
>>> expample[-1:]
[9]
>>> expample[-5:-1]
[5, 6, 7, 8]
>>> expample[-5:0]
[]
>>> expample[:7]
[0, 1, 2, 3, 4, 5, 6]
>>> expample[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> expample[1:8:2]
[1, 3, 5, 7]
>>> expample[::-2]
[9, 7, 5, 3, 1]
>>>
>>>
>>> expample[9:0:-2]
[9, 7, 5, 3, 1]
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'sheriy' *4
'sheriysheriysheriysheriy'
>>>
>>> name = ['doudou','sheriy','moon']
>>> 'aaa' in name
False
>>> 'doudou' in name
True
>>> x = 'sunny'
>>> 's' in name
False
>>> 's' in x
True
>>> len(name)
3
>>> min(name)
'doudou'
>>> max(name)
'sheriy'
>>> x = 'i love doudou'
>>> list (x)
['i', ' ', 'l', 'o', 'v', 'e', ' ', 'd', 'o', 'u', 'd', 'o', 'u']
>>> min(x)
' '
>>> max(x)
'v'
>>> name[1]
'sheriy'
>>> del name[1]
>>> name [1]
'moon'

>>> face = [1,2,3,4,5]
>>> face.append (44)
>>> face
[1, 2, 3, 4, 5, 44]
>>> face.count(44)
1
>>> face.append (44)
>>> face.count(44)
2
>>> one = [1,2,3]
>>> two = [4,5,6]
>>> one + two
[1, 2, 3, 4, 5, 6]
>>> one.exten(two)

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    one.exten(two)
AttributeError: 'list' object has no attribute 'exten'
>>> one.extend(two)
>>> one
[1, 2, 3, 4, 5, 6]
>>> two
[4, 5, 6]
>>> two.extend (one)
>>> two
[4, 5, 6, 1, 2, 3, 4, 5, 6]
>>> say = ['I','love','u','good','haha']
>>> say.index('u')
2
>>> say.index('haha')
4
>>> say.insert(1,'aaa')
>>> say
['I', 'aaa', 'love', 'u', 'good', 'haha']
>>> say.pop('1')

Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    say.pop('1')
TypeError: an integer is required
>>> say
['I', 'aaa', 'love', 'u', 'good', 'haha']
>>> say.pop(1)
'aaa'
>>> say
['I', 'love', 'u', 'good', 'haha']
>>> say.pop()
'haha'
>>> say
['I', 'love', 'u', 'good']
>>> say.remove('good')
>>> say
['I', 'love', 'u']
>>> new =[22,1.4,3,2,55,53]
>>> new.sort()
>>> new
[1.4, 2, 3, 22, 53, 55]
>>> new =['a','b','c','d']
>>> new.reverse()
>>> new
['d', 'c', 'b', 'a']
  1. 还没有评论
评论提交中, 请稍候...

留言

可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks
Feed