• 元组内置函数


    # -*- coding: utf-8 -*-
    # @Time:
    # @Auther: kongweixin
    # @File:
    # 元组(Tuple)(不可变的列表)
    # 含义元组是有序且不可更改的集合。在 Python 中,元组是用圆括号编写的。


    # 创建元组:
    # thistuple = ("apple", "banana", "cherry")
    # print(thistuple)

    # 索引 进行访问元组
    # thistuple = ("apple", "banana", "cherry")
    # 打印元组中的第二个项目:
    # print(thistuple[1])

    # 负索引(说过了!!!)
    # 负索引表示从末尾开始,-1 表示最后一个项目,-2 表示倒数第二个项目
    # thistuple = ("apple", "banana", "cherry")
    # 打印元组的最后一个项目:
    # print(thistuple[-1])
    # print(thistuple[2])

    # 索引范围
    # 可以通过指定范围的起点和终点来指定索引范围。
    # 指定范围后,返回值将是带有指定项目的新元组。


    # thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
    # 返回第三、第四、第五个项目:从索引 2(包括)开始,到索引 5(不包括)结束。
    # print(thistuple[2:5])

    # 负索引范围: 如果要从元组的末尾开始搜索,请指定负索引:
    # thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
    # 此例将返回从索引 -4(包括)到索引 -1(排除)的项目:
    # print(thistuple[-4:-1])


    # 更改元组值:创建元组后,您将无法更改其值。元组是不可变的,或者也称为恒定的。
    # 但是有一种解决方法。您可以将元组转换为列表,更改列表,然后将列表转换回元组。


    # x = ("apple", "banana", "cherry")
    # y = list(x) #把元组转换为列表即可进行更改:
    # y[1] = "kwx"
    # x = tuple(y)
    # print(x)

    #遍历元组:可以使用 for 循环遍历元组项目。

    # thistuple = ("apple", "banana", "cherry")
    # 遍历项目并打印值:
    # for x in thistuple:
    # print(x)

    # 检查项目是否存在: 要确定元组中是否存在指定的项,使用 in 关键字:
    # thistuple = ("apple", "banana", "cherry")
    # if "apple" in thistuple:#检查元组中是否存在 "apple":
    # print("Yes, 'apple' is in the fruits tuple")


    # 元组长度: 要确定元组有多少项,使用 len() 方法:

    # thistuple = ("apple", "banana", "cherry")
    # 打印元组中的项目数量
    # print(len(thistuple))

    # 添加项目: 元组一旦创建,您就无法向其添加项目。元组是不可改变的。

    # thistuple = ("apple", "banana", "cherry")
    # thistuple[3] = "orange" # 会引发错误
    # print(thistuple)
    # 所以无法向元组添加项目:


    # 创建有一个项目的元组: 如需创建仅包含一个项目的元组,您必须在该项目后添加一个逗号,否则 Python 无法将变量识别为元组。

    # thistuple = ("apple",)# 单项元组,别忘了逗号:
    # print(type(thistuple))


    #不是元组 而是字符串
    # thistuple = ("apple")
    # print(type(thistuple))


    # 删除项目(不存在的!!): 无法删除元组中的项目。
    # 元组是不可更改的,因此无法从中删除项目,但可以完全删除元组:

    # thistuple = ("apple", "banana", "cherry")
    # del thistuple #del 关键字可以完全删除元组:
    # print(thistuple) # 这会引发错误,因为元组已不存在。


    # 合并两个元组: 如需连接两个或多个元组,可以使用 + 运算符:

    # tuple1 = ("a", "b" , "c")
    # tuple2 = (1, 2, 3)
    # tuple3 = tuple1 + tuple2 #合并这个元组:
    # print(tuple3)


    # tuple() : 可以使用 tuple() 创建元组。

    # thistuple = tuple(("apple", "banana", "cherry")) # 请注意双括号
    # print(thistuple)

    """
    方法 描述
    count() 返回元组中指定值出现的次数。
    index() 在元组中搜索指定的值并返回它被找到的位置。
    """
    # count() 返回元组中指定值出现的次数。

    # 定义和用法: count() 方法返回指定值在元组中出现的次数。
    # 语法: tuple.count(value)
    # 参数值
    # 参数 描述
    # value 必需。要检索的项目。

    # 实例1:返回值 5 在元组中出现的次数:
    # thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
    # x = thistuple.count(5)
    # print(x)


    # index() 在元组中搜索指定的值并返回它被找到的位置。

    # 定义和用法: index() 方法查找指定值的第一次出现。如果未找到该值,index() 方法将引发异常。
    # 语法: tuple.index(value)
    # 参数值
    # 参数 描述
    # value 必需。要检索的项目。

    # 实例1: 检索首次出现的值 8,并返回其位置:
    # thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
    # x = thistuple.index(8)
    # print(x)




  • 相关阅读:
    vue自定义指令使用注意事项
    es6新增方法---实用
    webpack和gulp的区别
    OSI 5层协议,socket,从协议角度看网络通信
    网络通信流程
    数据相关的模块
    一些模块
    面向对象
    ATM作业
    XML模块增删改查基本操作
  • 原文地址:https://www.cnblogs.com/kwkk978113/p/13184237.html
Copyright © 2020-2023  润新知