第十五节,基本数据类型,元组tuple

时间:2023-03-09 00:09:30
第十五节,基本数据类型,元组tuple

元组和列表的区别

  元组和列表几乎是一样的

  不一样的地方就是元组创建后元组的元素不可以修改,比如(添加,拓展,移除等修改功能,但是元组里的元素的元素是可以修改的)

基本操作:
  索引
  切片
  循环
  长度
  包含

创建元组

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
a = ("lyh", "guixiu", "xioum")
#或者
b = tuple(("lyh", "guixiu", "xioum"))

tuple转换元组
"""(转换成元组,需要转换的可迭代数据变量) 注意:能转换成元组的必须是可迭代的,也就是可以被for循环的"""
字符串,字典,列表 > 都可以转换成元组,转换成元组都是可以被for循环的,for循环每次循环到的数据就是元组的一个元素

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
a = "小鸡炖蘑菇"
b = tuple(a)
print(b)
#输出 ('小', '鸡', '炖', '蘑', '菇')

索引

  格式:元组变量加[索引下标] 的方式

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
a = ("lyh", "guixiu", "xioum")
print(a[1])
#打印出 guixiu 打印出元素下标为1的元素

切片
  格式:元组变量加[起始下标:结束下标]

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
a = ("lyh", "guixiu", "xioum")
print(a[0:3])
#打印出 ('lyh', 'guixiu', 'xioum') 打印出元素下标0到3的元素

  len(p_object)

  """(统计元组里的元素数量)"""

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
a = ("lyh", "guixiu", "xioum")
print(len(a))
#打印出 3 统计出元组里有3个元素

  while循环

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
#while循环
a = ("lyh", "guixiu", "xioum")
b = 0
while b < len(a):
print(a[b])
b += 1
#循环出元组里的所有元素

  for循环

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
#for循环
a = ("lyh", "guixiu", "xioum")
for b in a: #b为自定义循环变量
print(b)
#循环出元组里的所有元素

  count(self, value)

  """(计算元素在元组里出现的次数)"""要计算的元素

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
a = ("lyh", "guixiu", "xioum")
print(a.count("guixiu"))
#打印出 1 guixiu元素在元组里出现一次

  index(self, value, start=None, stop=None)

  """(获取指定元素在元组里的索引位置)"""要查找的元素,起始位置,结束位置

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
a = ("lyh", "guixiu", "xioum")
print(a.index("guixiu"))
#打印出 1 guixiu元素在元组里的索引位置是1,默认从0开始计算
lass tuple(object):
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
"""
def count(self, value): # real signature unknown; restored from __doc__
""" T.count(value) -> integer -- return number of occurrences of value """
return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
T.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0 def __add__(self, y): # real signature unknown; restored from __doc__
""" x.__add__(y) <==> x+y """
pass def __contains__(self, y): # real signature unknown; restored from __doc__
""" x.__contains__(y) <==> y in x """
pass def __eq__(self, y): # real signature unknown; restored from __doc__
""" x.__eq__(y) <==> x==y """
pass def __getattribute__(self, name): # real signature unknown; restored from __doc__
""" x.__getattribute__('name') <==> x.name """
pass def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass def __getnewargs__(self, *args, **kwargs): # real signature unknown
pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__
"""
x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported.
"""
pass def __ge__(self, y): # real signature unknown; restored from __doc__
""" x.__ge__(y) <==> x>=y """
pass def __gt__(self, y): # real signature unknown; restored from __doc__
""" x.__gt__(y) <==> x>y """
pass def __hash__(self): # real signature unknown; restored from __doc__
""" x.__hash__() <==> hash(x) """
pass def __init__(self, seq=()): # known special case of tuple.__init__
"""
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object.
# (copied from class doc)
"""
pass def __iter__(self): # real signature unknown; restored from __doc__
""" x.__iter__() <==> iter(x) """
pass def __len__(self): # real signature unknown; restored from __doc__
""" x.__len__() <==> len(x) """
pass def __le__(self, y): # real signature unknown; restored from __doc__
""" x.__le__(y) <==> x<=y """
pass def __lt__(self, y): # real signature unknown; restored from __doc__
""" x.__lt__(y) <==> x<y """
pass def __mul__(self, n): # real signature unknown; restored from __doc__
""" x.__mul__(n) <==> x*n """
pass @staticmethod # known case of __new__
def __new__(S, *more): # real signature unknown; restored from __doc__
""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
pass def __ne__(self, y): # real signature unknown; restored from __doc__
""" x.__ne__(y) <==> x!=y """
pass def __repr__(self): # real signature unknown; restored from __doc__
""" x.__repr__() <==> repr(x) """
pass def __rmul__(self, n): # real signature unknown; restored from __doc__
""" x.__rmul__(n) <==> n*x """
pass def __sizeof__(self): # real signature unknown; restored from __doc__
""" T.__sizeof__() -- size of T in memory, in bytes """
pass

tuple

元组里的元素的元素追加
元组的元素是不可修改和和追加的,也就是元组的子级不可修改,元组的元素的元素也就是孙级是可以修改的

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
#元组里的元素的元素追加
#元组的元素是不可修改和和追加的,也就是元组的子级不可修改,元组的元素的元素也就是孙级是可以修改的
#追加方式
#列1
a = (11,22,["guixiu",{"k1":"k2"}])
b = {"k3":"k4"}
a[2][1].update(b)#索引到元组里字典时,将b元组最佳进去
print(a)
#输出 (11, 22, ['guixiu', {'k1': 'k2', 'k3': 'k4'}]) #列2
a = (11,22,["guixiu",{"k1":"k2"}])
c = a[2][1]#索引到元组里的字典
c["k3"] = "k4"
print(a)
#输出 (11, 22, ['guixiu', {'k1': 'k2', 'k3': 'k4'}])

元组的功能

转换列表
索引
切片
for循环
长度
反转
排序
索引位置
统计元素个数