# python-元组

1. 元组

  • 元组(Tuple):与列表类似,也是一个序列数据结构。主要区别在于元组中的元素不能修改。

  • 格式:

    名称= (“元素1”,“元素2”,...)
    
  • 基本操作

    • 定义元组:

      computer = ("主机","显示器","鼠标","键盘")
      
    • 使用命令
      image

      同样支持切片,与列表使用方法一样。

2. 案例

#!/usr/bin/env python3
# _*_ coding: utf-8 _*_
# Author:shichao
# File: .py

computer = ("主机","显示器","鼠标","键盘","音响","内存","cpu","主机")


# 查询
Computer_index = computer.index("内存")          # 查询索引的位置
Computer_count = computer.count("主机")          # 查询索引的位置

print(computer[Computer_index])
print(Computer_count)
posted @ 2022-12-26 11:36  七月流星雨  阅读(11)  评论(0编辑  收藏  举报