首页 > 编程语言 >python数据容器(二)元组

python数据容器(二)元组

时间:2024-07-06 21:02:44浏览次数:18  
标签:容器 index python t1 str print 元组 my

1. 数据容器:tuple(元组)

(1)定义

t1 = (1, "Hello", True)
t2 = ()
t3 = tuple()
print(f"t1的类型是:{type(t1)},内容是:{t1}")
print(f"t2的类型是:{type(t2)},内容是:{t2}")
print(f"t3的类型是:{type(t3)},内容是:{t3}")

运行结果:

(2)定义单个元素的元素

t1 = ("hello")
print(f"t1的类型是{type(t1)},内容是:{t1}")
t2 = ("hello",)
print(f"t2的类型是{type(t2)},内容是:{t2}")

运行结果:

 

(3)元组的嵌套

t1 = ((1, 2, 3),(4, 5, 6))
print(t1[0][0])
print(t1[0][1])
print(t1[0][2])
print(t1[1][0])
print(t1[1][1])
print(t1[1][2])

运行结果:

 

(4)元组的操作:index查找方法

(5)元组的操作:count统计方法

(6)元组的操作:len获取长度

t1 = (1, 2, 3, 'hello', 3.4, 3, 6)
print(t1[2])

# index()
print(f"3.4在t1中的下标是:{t1.index(3.4)}")

# count()
print(f"3在t1中出现的次数是:{t1.count(3)}")

# len()
print(f"t1的长度是:{len(t1)}")

运行结果:

 

(7)元组的遍历

①while
t1 = (1, 2, 3, 'hello', 3.4, 3, 6)
index = 0
while index < len(t1):
    print(t1[index])
    index += 1

运行结果:

 

②for
t1 = (1, 2, 3, 'hello', 3.4, 3, 6)
index = 0
for index in t1:
    print(index)

运行结果:

 

(8)修改元组内容

无法修改!

t1 = (1, 2, 3, 'hello', 3.4, 3, 6)
t1[0] = 10
print(t1)

运行结果:

 

嵌套list:可以修改! 

t1 = (1, 2, ['itheima', 'python'])
t1[2][1] = 'java'
print(t1)

运行结果:

· 练习:元组的基本操作

定义一个元组,内容是:t1 = ('周杰伦', 11, ['football', 'music']),记录的是一个学生的信息(姓名、年龄、爱好)

请通过元组的功能(方法),对其进行:

1.查询其年龄所在的下标位置

2.查询学生的姓名

3.删除学生爱好中的football

4.增加爱好:coding到爱好list内

t1 = ('周杰伦', 11, ['football', 'music'])
print(f"年龄所在的下标位置是:{t1.index(11)}")
print(f"学生的姓名是:{t1[0]}")
del t1[2][0]
print(t1)
t1[2].append('coding')
print(t1)

运行结果:

 

2. 数据容器:str(字符串)

(1)下标(索引)

name = "itheima"
print(name[0])
print(name[-1])

运行结果:

(2)查找下标索引值

my_str = "itcast and itheima"
print(my_str.index("and"))

运行结果:

(3)替换

my_str = "itcast and itheima"
print(my_str.replace("it", "程序"))

运行结果:

(4)分割

my_str = "hello python itheima itcast"
my_str_list = my_str.split(" ")
print(my_str)
print(my_str_list)
print(type(my_str_list))

运行结果:

(5)规整

① 去前后空格
my_str = " itheima and itcast "
new_my_str = my_str.strip()
print(new_my_str)

运行结果:

② 去前后指定字符串
my_str = "12itheima and itcast"
new_my_str = my_str.strip("12")
print(new_my_str)

运行结果:

(6)统计出现次数

my_str = "itheima and itcast"
print(my_str.count("t"))

运行结果:

(7)统计字符串长度

my_str = "itheima and itcast"
print(len(my_str))

运行结果:

· 练习:分割字符串

给定一个字符串:“itheima itcast boxuegu”

1.统计字符串内有多少个“it”字符

2.将字符串内的空格,全部替换为字符:“|”

3.并按照“|”进行字符串分割,得到列表

my_str = "itheima itcast boxuegu"
num = my_str.count("it")
print(num)
new_my_str = my_str.replace(" ", "|")
print(new_my_str)
print(new_my_str.split("|"))

运行结果:

标签:容器,index,python,t1,str,print,元组,my
From: https://blog.csdn.net/m0_54447321/article/details/140235033

相关文章

  • opencv环境搭建-python
    最近遇到了一些图像处理的需求,所以需要学习一下opencv,来记录一下我的学习历程。安装numpypipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplenumpy安装matplotlibpipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplematplotlib安装opencvpipin......
  • python数据容器(一)列表list
    思维导图代码1.数据容器入门2.数据容器:list(列表)name_list=['itheima','itcast','python']print(name_list)print(type(name_list))运行结果: name_list=['itheima',666,True]print(name_list)print(type(name_list))运行结果: name_l......
  • Python基础语法
    #1.注释-代码中不参与编译执行的部分就是注释。(不会被翻译成机器码,它的存在不会影响程序的功能)#1)注释的作用#a.对代码进行注解和说明,提高代码的可读性(对人)#b.取消代码的功能##2)添加注释的方法#a.单行注释-在一行注释内容前加#(添加和取消单行注......
  • python和pycharm安装
    一、python和pycharm的作用Python是一种跨平台的计算机程序语言。Python是我们进行项目开发而使用的一门计算机语言,通俗来说就是编写代码,编写完代码之后,我们就需要运行,不然代码是死的,机器是无法识别的,这时我们需要运行Python代码的运行环境和工具。PyCharm带有一整套......
  • python: list
     #去重A=['geovindu','刘杰','江山','河水','刘杰','geovindu','张三','李五']B=[]foriinA:ifinotinB:B.append(i)print(B)C=set(A)......
  • Python爬虫获取视频
    验证电脑是否安装python        1.win+r输入cmd    2.在黑窗口输入python.exe         3.不是命令不存在就说明python环境安装完成抓取快手视频    1.在phcharm应用中新建一个项目    3.新建一个python文件 ......
  • python3.7报错ModuleNotFoundError: No module named 'importlib.metadata'
    1.问题今天在使用一个项目的时候遇到以下的问题:ModuleNotFoundError:Nomodulenamed'importlib.metadata'个人的情况:python3.7conda环境中安装了importlib-meta的工具包,版本为6.7.0在引入的过程中,还是出现了问题。这是由于,包的引入方式和包名不一致引起的。具体如下......
  • 小白学python的第一周总结
    一、常用的cmd指令cmd指令cls清屏cdcd..返回上一级;cd.当前目录;D:(把冒号前方的字符更改为盘符名称表示切换盘符)dir列出当前目录下的文件和子目录ipconfig显示网络配置信息;查看电脑的ip地址 二、运算符    运算符包括:算数运算符、赋值运算符、比较运......
  • Python运算符详细介绍
    在Python编程中,运算符是非常基础且重要的概念。它们用于执行各种运算操作。本文将详细介绍Python中的六种主要运算符:算数运算符、赋值运算符、比较运算符、逻辑运算符、位运算符和成员运算符。1.算数运算符算数运算符用于执行基本的数学运算。以下是Python中常用的算数运算......
  • Python分支结构详解
    在编程中,控制流结构是至关重要的,它决定了程序的执行顺序。Python提供了多种控制流结构,其中分支结构是基础且常用的。本文将详细介绍Python中的分支结构,包括顺序结构、选择结构、单分支、双分支、多分支、分支嵌套以及pass关键字的使用。1.顺序结构、选择结构顺序结构是最简......