wlyyb521 发表于 2018-8-15 08:40:23

5.Python入门到精通

  从列表中获取元素
  跟数组一样,我们可以通过元素的索引值(index)从列表获取单个元素,注意,列表索引值是从 0 开始的。
  ['u', 'a', 'b', 'c', 'd', 'e', 'k', 'f']
  >>> member
  'u'
  >>> member
  'a'
  >>>
  >>> temp = member
  >>> member = member
  >>> member
  ['a', 'a', 'b', 'c', 'd', 'e', 'k', 'f']
  >>> member = temp
  >>> member
  ['a', 'u', 'b', 'c', 'd', 'e', 'k', 'f']
  >>>
  从列表删除元素
  remove()
  >>> member.remove('b')
  >>> member
  ['a', 'u', 'c', 'd', 'e', 'k', 'f']
  >>>
  del
  >>> del member
  >>> member
  ['u', 'c', 'd', 'e', 'k', 'f']
  >>>
  pop()
  >>> member.pop()
  'f'
  >>> member
  ['u', 'c', 'd', 'e', 'k']
  >>> name=member.pop()
  >>> name
  'k'
  >>> member.pop(2)
  'd'
  >>>
  列表分片(Slice)
  利用索引值,每次我们可以从列表获取一个元素,但是我们总是贪心的,如果一次性需要获取多个元素,有没有办法实现呢?利用列表分片,我们可以简单的实现这个要求。
  >>> member
  ['c', 'e']
  >>>
  >>> member
  ['u', 'c', 'e']
  >>> member[:3]
  ['u', 'c', 'e']
  >>> member[:]
  ['u', 'c', 'e']
  >>> member
  ['c', 'e']
  >>>
  列表的一些常用操作符
  比较操作符
  >>> list1 =
  >>> list2 =
  >>> list1 > list2
  False
  >>> list1 =
  >>> list2 =
  >>> list1 > list2
  False
  >>> list3=
  逻辑操作符
  >>> (list1 < list2) and (list1 == list3)
  True
  连接操作符
  >>> list4 = list1 + list2
  >>> list4
  
  >>> list1 + 'x'
  Traceback (most recent call last):
  File "<pyshell#79>", line 1, in <module>
  list1 + 'x'
  TypeError: can only concatenate list (not "str") to list
  重复操作符
  >>> list3 * 3
  
  >>> list3 *= 3
  >>> list3
  
  >>> list3 *= 5
  >>> list3
  
  >>>
  成员关系操作符
  >>> 123 in list3
  True
  >>> 'd' not in list3
  True
  >>> 123 not in list3
  False
  >>>
  >>> list5 = ,456]
  >>> 'd' in list5
  False
  >>> 'd' in list5
  True
  >>> list5
  'u'
  >>>
  列表的小伙伴们
  >>> dir(list)
  ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  >>> list3.count(123)
  15
  >>> list3.index(123)
  0
  >>> list3.index(123,3,7)
  4
  >>> list3.reverse()
  >>> list3
  
  >>> list6 =
  >>> list6.sort()
  >>> list6
  
  >>> list6.sort(reverse=True)
  >>> list6
  
  >>>
  关于分片“拷贝”概念的补充
  >>> list7 = list6[:]
  >>> list7
  
  >>> list8 = list6
  >>> list8
  
  >>> list6.sort()
  >>> list6
  
  >>> list7
  
  >>> list8
  
  >>>

  元组:戴上了枷锁的列表
  由于和列表是近亲关系,所以元组和列表在实际使用上是非常相似的。
  我们这节课主要通过讨论元组和列表到底有什么不同来学习元组,酱紫大家就不会觉得老是重复一样的内容。
  我们主要从以下几个点来讨论学习:
  创键和访问一个元组
  >>> temp=(1)
  >>> temp
  1
  >>> type(temp)
  <class 'int'>
  >>> temp2=2,3,4
  >>> type(temp2)
  <class 'tuple'>
  >>> temp = []
  >>> type (temp)
  <class 'list'>
  >>> temp = ()
  >>> type(temp)
  <class 'tuple'>
  >>> temp = (1,)
  >>> type(temp)
  <class 'tuple'>
  >>>
  >>> temp = 1,
  >>> type(temp)
  <class 'tuple'>
  >>> 8 * (8)
  64
  >>> 8 * (8,)
  (8, 8, 8, 8, 8, 8, 8, 8)
  >>>
  更新和删除一个元组
  >>> temp = ('d','c','a','b')
  >>> temp = temp[:3] + ('f',) + temp
  >>> temp
  ('d', 'c', 'a', 'f', 'b')
  >>> del temp
  >>> temp
  Traceback (most recent call last):
  File "<pyshell#148>", line 1, in <module>
  temp
  NameError: name 'temp' is not defined
  >>>
  元组相关的操作符
  >>> str1 = 'I love Python'
  >>> str1[:6]
  'I love'
  >>> str1
  'I love Python'
  >>> str1
  ' '
  >>> str1
  'e'
  >>> str1[:6] + 'inserstr' + str1
  'I loveinserstr Python'
  >>> str1
  'I love Python'
  >>>
  字符串:各种奇葩的内置方法

  
  >>> str2 = 'ivwdcwso'
  >>> str2.capitalize()
  'Ivwdcwso'
  >>> str2='IvwDcwSo'
  >>> str2.casefold()
  'ivwdcwso'
  >>> str2
  'IvwDcwSo'
  >>> str2.center(40)
  '                IvwDcwSo                '
  >>> str2.count('w')
  2
  >>> str2.endswith('w')
  False
  >>> str2.endswith('o')
  True
  >>> str3 = 'I\tlove\tPython!'
  >>> str3.expandtabs()
  'I       love    Python!'
  >>> str3.find('efc')
  -1
  >>> str3.find('o')
  3
  >>>
  >>> str3
  'I\tlove\tPython!'
  >>> str3.istitle()
  False
  >>> str3.join('123')
  '1I\tlove\tPython!2I\tlove\tPython!3'
  >>>
  >>> str6 = '    I love Python!'
  >>> str6.lstrip()
  'I love Python!'
  >>> str6 = 'i love python'
  >>> str6.partition('ov')
  ('i l', 'ov', 'e python')
  >>> str6
  'i love python'
  >>> str6.replace('python','Python')
  'i love Python'
  >>>
  >>> str6.split()
  ['i', 'love', 'python']
  >>>
  >>> str6.split('i')
  ['', ' love python']
  >>>
  >>> str7 = '    aaaa   '
  >>> str7.strip()
  'aaaa'
  >>> str7=str7.strip()
  >>> str7
  'aaaa'
  >>>
  
页: [1]
查看完整版本: 5.Python入门到精通